I am trying to write an API which will accept a list of order records to insert. The order table would be related to the account table, and the account has an external id field.
Unfortunately, when the user posts the request to the api, they don't have the ID of the account to provide when sending the list of orders to insert, only the external id (their account number).
Is there an elegant way, with a list of order records, to pull the account ID across based on the external id (account number) that they have provided.
In pseudo code I would do something like below :
Update tmp_Orders
set account_id = account.id
from account
where account.accountNumber = tmp_orders.accountNumber
What I don't want to do is to have to loop through the list and look up each account id individually as the quantity of orders passed to the api could result in that method hitting the governor limits.
thanks
You don't need to do the lookup yourself, in the insert call you can specify the externalId rather than the regular salesforce account Id, and the service will automatically do the lookup for you. You do this by specifying a nested Account object with the relevant externalId set rather than just setting the accountId field, e.g.
Account a = new Account(extId__c='foo');
Order o = new Order(...);
o.account = a;
insert o;
There's more examples here, including doing to from the web api rather than apex.