// Get the CustomFieldService.
CustomFieldServiceInterface customFieldService =
dfpServices.get(session, CustomFieldServiceInterface.class);
// Create a statement to select a custom field.
StatementBuilder statementBuilder = new StatementBuilder()
.where("WHERE id = :id")
.orderBy("id ASC")
.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
.withBindVariableValue("id", customFieldId);
// Default for total result set size.
int totalResultSetSize = 0;
do {
// Get custom fields by statement.
CustomFieldPage page =
customFieldService.getCustomFieldsByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (CustomField customField : page.getResults()) {
System.out.printf(
"%d) Custom field with ID \"%d\" will be deactivated.\n", i++, customField.getId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of custom fields to be deactivated: %d\n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.dfp.axis.v201302.DeactivateCustomFields action =
new com.google.api.ads.dfp.axis.v201302.DeactivateCustomFields();
// Perform action.
UpdateResult result = customFieldService.performCustomFieldAction(
action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf("Number of custom fields deactivated: %d\n", result.getNumChanges());
} else {
System.out.println("No custom fields were deactivated.");