0

I have an external system sending objectname, fields names of it and DML operation type to perform i.e., insert or update or upsert.

Insert can be done easily.

Given that external system sends records with fields specified to update/upsert (except Id as they do not have the reference),should I take the following approach or is there a better way to do it.

for every record sent, query and fetch by filtering with these fields , build a map of Id, that object updated with new values and final upsert or is there a better approach

1
  • You should really look at implementing some form of external Id. I would assume the external system has some unique identifier that they can send with the data. If they do, you can use that field to do an upset on – Eric Mar 2 '17 at 15:25
1

If I am getting you right you don't need to query records first to upsert them.

Simply create new instance and put the Id for update like:

// Assuming you already have id   
Account acc = new Account(id="id_got_previously", Field1__c="update value if any");
update acc;

Or you can use Database class like this:

Database.SaveResult[] = Database.upsert(list_of_record, false);

This will succeed even few records failed. And youcan det detail for failed record in Database.SaveResult[] for next process if any.

2
  • I am not getting the Id field value from the external system. Instead I get the list of fields using which I should identify the existence of records – Ashu NCS Feb 12 '15 at 9:46
  • @Ashu updated my answer. – Ashwani Feb 12 '15 at 9:54
0

To be short:

Database.UpsertResult[] upsert(recordsToUpsert, 
                               external_ID_Field, 
                               opt_allOrNone);

If you need to merge your incoming and existing objects (not override the existing with the incoming object) then you will need to add some more logic depending on how you would like to merge each field.

Have a look at this to understand how to do bulk exception handling.

Hope this helps!

0

If you know the object name and you need to insert or update data dynamically, the first thing you need to do is creation of sObject, then population of the data inside of the object and for the last calling of the insert/update functionality.

Here is the example of the insert functionality.

String name_of_object = 'Customers__c';
sObject obj = Schema.getGlobalDescribe().get(name_of_object).newSObject();
obj.put('Name', 'Sample Name');
obj.put('Qty__c', '5');
insert obj;

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.