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
aklkkaklkk 

how to upsert performance with trigger ?

Hi Please support

how to upsert performance with trigger ?(it means old record found then update some fields else not old record found then create new Record)
Raj VakatiRaj Vakati

In upsert operation Salesforce internally validates the data based on Object's Id or External Id. 

Using the upsert operation, you can either insert or update an existing record in one call based on ID or external Id . To determine whether a record already exists, the upsert statement or Database method uses the record’s ID as the key to match records, a custom external ID field, or a standard field with the idLookup attribute set to true.

If the key is not matched, then a new object record is created.
If the key is matched once, then the existing object record is updated.
If the key is matched multiple times, then an error is generated and the object record is neither inserted or updated.


So you need to write the trigger that wil invoke on both the insert and update which is equal to upsert 



https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_upsert.htm
 
trigger ContextExampleTrigger on Account (before insert, before update) {
    if (Trigger.isInsert) {
        if (Trigger.isBefore) {
           // Means insert trigger 
		 //  No action needed simply insert 
		   
		}
    }
   
   
   if (Trigger.isUpdate) {
        if (Trigger.isBefore) {
           // Means update  trigger 
		}
    }
	
	
}



 
aklkkaklkk
Hi Raj Vakati

Can you explane the full trigger because when i am wrting then showing error ?



Thanks
akki