// Get the CustomFieldService.
CustomFieldServiceInterface customFieldService =
user.getService(DfpService.V201302.CUSTOM_FIELD_SERVICE);
// Get the LineItemService.
LineItemServiceInterface lineItemService =
user.getService(DfpService.V201302.LINEITEM_SERVICE);
// Set the IDs of the custom fields, custom field option, and line item.
Long customFieldId = Long.parseLong("INSERT_STRING_CUSTOM_FIELD_ID_HERE");
Long dropDownCustomFieldId = Long.parseLong("INSERT_DROP_DOWN_CUSTOM_FIELD_ID_HERE");
Long customFieldOptionId = Long.parseLong("INSERT_CUSTOM_FIELD_OPTION_ID_HERE");
Long lineItemId = Long.parseLong("INSERT_LINE_ITEM_ID_HERE");
// Get the custom field.
CustomField customField = customFieldService.getCustomField(customFieldId);
// Get the drop-down custom field.
DropDownCustomField dropDownCustomField =
(DropDownCustomField) customFieldService.getCustomField(dropDownCustomFieldId);
// Get the line item.
LineItem lineItem = lineItemService.getLineItem(lineItemId);
// Create custom field values.
List<BaseCustomFieldValue> customFieldValues = new ArrayList<BaseCustomFieldValue>();
TextValue textValue = new TextValue();
textValue.setValue("Custom field value");
CustomFieldValue customFieldValue = new CustomFieldValue();
customFieldValue.setCustomFieldId(customFieldId);
customFieldValue.setValue(textValue);
customFieldValues.add(customFieldValue);
DropDownCustomFieldValue dropDownCustomFieldValue = new DropDownCustomFieldValue();
dropDownCustomFieldValue.setCustomFieldId(dropDownCustomFieldId);
dropDownCustomFieldValue.setCustomFieldOptionId(customFieldOptionId);
customFieldValues.add(dropDownCustomFieldValue);
// Only add existing custom field values for different custom fields than
// the ones you are setting.
if (lineItem.getCustomFieldValues() != null) {
for (BaseCustomFieldValue oldCustomFieldValue : lineItem.getCustomFieldValues()) {
if (!oldCustomFieldValue.getCustomFieldId().equals(customFieldId)
&& !oldCustomFieldValue.getCustomFieldId().equals(dropDownCustomFieldId)) {
customFieldValues.add(oldCustomFieldValue);
}
}
}
lineItem.setCustomFieldValues(customFieldValues.toArray(new BaseCustomFieldValue[]{}));
// Update the line item on the server.
LineItem[] lineItems = lineItemService.updateLineItems(new LineItem[] {lineItem});
if (lineItems != null) {
for (LineItem updatedLineItem : lineItems) {
List<String> customFieldValueStrings = new ArrayList<String>();
for (BaseCustomFieldValue baseCustomFieldValue : lineItem.getCustomFieldValues()) {