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
DIEHARDDIEHARD 

Trigger to conditionally add or update Lead

I would like to develop a Trigger that first checks if a Lead record is already in the system.  If it is not  then go ahead and allow the system to create a new Lead with new campaign.  Otherwise update the existing Lead by including it in a new campaign.

Bhawani SharmaBhawani Sharma
You can write a after insert trigger. What it will do is, It will take the new lead Id and will check if there is any other lead with the same Name. If yes, then update the existing one and delete the new one.
sushant sussushant sus

hi ,

 

i think in this code you trying to check first duplicate then create campaign


this is a  sample code

 

trigger leadDuplicatePreventer on Lead
                               (before insert, before update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();
    for (Lead lead : System.Trigger.new) {
		
        // Make sure we don't treat an email address that  
    
        // isn't changing during an update as a duplicate.  
    
        if ((lead.Email != null) &&
                (System.Trigger.isInsert ||
                (lead.Email != 
                    System.Trigger.oldMap.get(lead.Id).Email))) {
		
            // Make sure another new lead isn't also a duplicate  
    
            if (leadMap.containsKey(lead.Email)) {
                lead.Email.addError('Another new lead has the '
                                    + 'same email address.');
            } else {
                leadMap.put(lead.Email, lead);
            }
       }
    }
	
    // Using a single database query, find all the leads in  
    
    // the database that have the same email address as any  
    
    // of the leads being inserted or updated.  
    
    for (Lead lead : [SELECT Email FROM Lead
                      WHERE Email IN :leadMap.KeySet()]) {
if(lead!=null)
{ Lead newLead = leadMap.get(lead.Email); newLead.Email.addError('A lead with this email ' + 'address already exists.');
}

else {
insert leadmap ;
after that create compaign object and insert data .
} } }