// Get AdWordsUser from "~/adwords.properties".
AdWordsUser user = new AdWordsUser();
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService =
user.getService(AdWordsService.V201306.ADGROUP_AD_SERVICE);
// Get validateOnly version of the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdValidationService =
user.getValidationService(AdWordsService.V201306.ADGROUP_AD_SERVICE);
long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
// Create text ad that violates an exemptable policy. This ad will only
// trigger an error in the production environment.
TextAd exemptableTextAd = new TextAd();
exemptableTextAd.setHeadline("Mars " + System.currentTimeMillis() + "!!!");
exemptableTextAd.setDescription1("Visit the Red Planet in style.");
exemptableTextAd.setDescription2("Low-gravity fun for everyone!");
exemptableTextAd.setDisplayUrl("www.example.com");
exemptableTextAd.setUrl("http://www.example.com/");
// Create ad group ad.
AdGroupAd exemptableAdGroupAd = new AdGroupAd();
exemptableAdGroupAd.setAdGroupId(adGroupId);
exemptableAdGroupAd.setAd(exemptableTextAd);
// Create operations.
AdGroupAdOperation exemptableOperation = new AdGroupAdOperation();
exemptableOperation.setOperand(exemptableAdGroupAd);
exemptableOperation.setOperator(Operator.ADD);
// Create text ad that violates an non-exemptable policy.
TextAd nonExemptableTextAd = new TextAd();
nonExemptableTextAd.setHeadline("Mars Cruise with too long of a headline.");
nonExemptableTextAd.setDescription1("Visit the Red Planet in style.");
nonExemptableTextAd.setDescription2("Low-gravity fun for everyone.");
nonExemptableTextAd.setDisplayUrl("www.example.com");
nonExemptableTextAd.setUrl("http://www.example.com/");
// Create ad group ad.
AdGroupAd nonExemptableAdGroupAd = new AdGroupAd();
nonExemptableAdGroupAd.setAdGroupId(adGroupId);
nonExemptableAdGroupAd.setAd(nonExemptableTextAd);
// Create operations.
AdGroupAdOperation nonExemptableOperation = new AdGroupAdOperation();
nonExemptableOperation.setOperand(nonExemptableAdGroupAd);
nonExemptableOperation.setOperator(Operator.ADD);
AdGroupAdOperation[] operations =
new AdGroupAdOperation[] {exemptableOperation, nonExemptableOperation};
try {
// Validate the ads.
AdGroupAdReturnValue result = adGroupAdValidationService.mutate(operations);
} catch (ApiException e) {
Set<Integer> indicesToRemove = new HashSet<Integer>();
for (ApiError error : e.getErrors()) {
if (error instanceof PolicyViolationError) {
PolicyViolationError policyVioloationError = (PolicyViolationError) error;