function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jacob W LandisJacob W Landis 

If existig do update in trigger

Hi,

we have a scheduled job running every hour which import a list data of a custom object from other system to salesforce. We want to have a trigger to check whether the data is existing or not, if it is existed, we want to update the existing record. how should I implement this?

Jingli
Suraj GharatSuraj Gharat
Trigger would fire after insert/update. But you need to check the data first, if it is existing then update else insert. Hence you have to do in your scheduled Apex code not in Trigger.
Santosh Kumar Sriram_2310Santosh Kumar Sriram_2310
Hi Jingli,

Try using the Upsert functionality in Salesforce. If you are storing Salesforce ID or any form of External ID, then upsert does exactly as you want.
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_upsert.htm

The scheduled job should not insert/update and should do an upsert with a secific field as external ID. This could be a workflow populated field with combination of field values as well.

Hope this helps!