// Get the CustomFieldService.
CustomFieldServiceInterface customFieldService =
dfpServices.get(session, CustomFieldServiceInterface.class);
// Get the LineItemService.
LineItemServiceInterface lineItemService =
dfpServices.get(session, LineItemServiceInterface.class);
// Get the drop-down custom field ID.
long dropDownCustomFieldId =
customFieldService.getCustomFieldOption(customFieldOptionId).getCustomFieldId();
// Get the line item.
LineItem lineItem = lineItemService.getLineItem(lineItemId);
// Create number custom field value.
NumberValue numberValue = new NumberValue();
numberValue.setValue("12345");
CustomFieldValue numberCustomFieldValue = new CustomFieldValue();
numberCustomFieldValue.setCustomFieldId(numberCustomFieldId);
numberCustomFieldValue.setValue(numberValue);
// Create drop-down custom field value.
DropDownCustomFieldValue dropDownCustomFieldValue = new DropDownCustomFieldValue();
dropDownCustomFieldValue.setCustomFieldId(dropDownCustomFieldId);
dropDownCustomFieldValue.setCustomFieldOptionId(customFieldOptionId);
// Create a combined custom field value list of existing different custom
// field values and new ones.
List<BaseCustomFieldValue> combinedCustomFieldValues = Lists.newArrayList();
if (lineItem.getCustomFieldValues() != null) {
for (BaseCustomFieldValue existingCustomFieldValue : lineItem.getCustomFieldValues()) {
if (!existingCustomFieldValue.getCustomFieldId().equals(numberCustomFieldId)
&& !existingCustomFieldValue.getCustomFieldId().equals(dropDownCustomFieldId)) {
combinedCustomFieldValues.add(existingCustomFieldValue);
}
}
}
combinedCustomFieldValues.addAll(
Arrays.asList(numberCustomFieldValue, dropDownCustomFieldValue));
// Set the combined custom field values.
lineItem.setCustomFieldValues(combinedCustomFieldValues.toArray(new BaseCustomFieldValue[]{}));
// Update the line item on the server.
LineItem[] lineItems = lineItemService.updateLineItems(new LineItem[] {lineItem});
for (LineItem updatedLineItem : lineItems) {
// Get a string representation of the custom field values.
List<String> customFieldValueStrings = Lists.transform(
Arrays.asList(updatedLineItem.getCustomFieldValues()),