1

I am using the following coarse grained REST endpoint to insert multiple values into a custom object. But the customer object has references to another object. For example, it has a field Contact__c which references a contact while the JSON object for input has only an externalContactID field(which is one of the custom fields in the standard object 'Contacts'). Of course the following code below is not going to do that. How can I incorporate this into the apex code without a processing overhead. (NOTE: I intentionally do not want to do this using BULK API)

@RestResource(urlMapping='/MultiTicket/*')
global with sharing class MultiTicketRestController{
   global class RequestBody {
       global List<Ticket__c> tickets;
   }

    @HttpPost   
    global static List<Ticket__c> createBulk(MultiTicketRestController.RequestBody req) {
        insert req.tickets; 
        return req.tickets;
    }

}
1

You will have to set the value of the linked objects so a high level / coarse grained example:

Instead of insert req.tickets

get the value of the externalContactID field in the request body then

req.tickets.Contact__r = New Contact(EXTERNALIDFIELD__c = externalContactID);
insert req.tickets;
  • 1
    note that you don't have to use upsert to use external Ids to resolve Foreign Keys fields, they work on insert/update as well. – superfell Oct 25 '15 at 23:06
  • @superfell - Yes you are correct. Updated to reflect that – Eric Oct 25 '15 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.