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
akkkakkk 

how to Trigger perform after insert after update?

Hi All,

How to Trigger Perform after insert after update if the EXISTING record only go and update if the new then create New Records ?

Thanks
akkk
ANUTEJANUTEJ (Salesforce Developers) 
Hi Akk,

If I get the question correct and if you want to perform the trigger only if a new record is inserted then you can use either before insert or after insert.

Before insert is generally used when you want to modify the field data before inserting the record and after insert is used when you want to use the id of the record that is being inserted in a related record.

To get more details on before vs after trigger you can have a look at below article: https://www.sfdc99.com/2014/01/25/use-vs-triggers/

For More information regarding the context variables you can check the below documentations: 
>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables_considerations.htm

>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
akkkakkk
Hi ANUTEJ,

This is not answer of the Question
the Question is that
How to Trigger Perform after insert after update if the all reday EXISTING record only go and update if the new then create New Records ?

Thanks
 
ANUTEJANUTEJ (Salesforce Developers) 
Sorry, for the delayed response:

>> https://www.mstsolutions.com/technical/preventing-duplicate-records-based-on-multiple-fields-in-salesforce/

In the above link there is an example that checks the lastname and email before inserting, coming to update part if there is a part you want to execute only when there is an update scenario you can simply use 

if(trigger.isafter || trigger.isupdate)
{
//code block.
}

This above lines would check if the scenario is an after update scenario and execute only that part.
 
trigger AvoidDuplicate on Lead (before insert)

{    

    set<string> newNameSet = new set<string>();newNameSet = new set<string>();

    set<string> newEmailSet = new set<string>();

    set<string> dbNameSet = new set<string>();

    set<string> dbEmailSet = new set<string>();

    for(lead newLead : trigger.new){

        newNameSet.add(newLead.LastName);

        newEmailSet.add(newLead.Email);

    }

    for(Lead dbLead : [select id, LastName, email from lead where email IN: newEmailSet OR LastName IN: newNameSet]){newNameSet]){

        dbNameSet.add(dbLead.LastName);

        dbEmailSet.add(dbLead.Email);

    }

    for(lead newLead : trigger.new){

        if(dbNameSet.contains(newLead.LastName) && dbEmailSet.Contains(newLead.Email))

            newLead.addError('You are inserting Duplicate lead');

    }

}

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.