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
Ankit Gupta 290Ankit Gupta 290 

In Lead Object feild i have created lookup of contact object I want to write a trigger on contact on lead source feild so when i select lead source Web my rating should be hot in lead object rating feild and further same if phone ithe rating cold

Best Answer chosen by Ankit Gupta 290
Ankit Gupta 290Ankit Gupta 290
trigger UpdateConRating on Contact (after insert, after update) {
    List<Lead> newLead = new List <Lead>();
    Set<id> conIdSet =new Set<id> ();
    for(Contact c:Trigger.new){
        conIdSet.add(c.id);
        }
        System.debug('conIdSet:'+conIdSet);
        
        for(Lead l : [Select id,rating,Contact__r.LeadSource from lead where Contact__c in:conIdSet]){
            if(l.Contact__r.LeadSource=='Web'){
                l.rating='Hot';
                
            } 
            else if (l.Contact__r.LeadSource=='Phone Inquiry'){
                l.rating='Warm';
            }
            else if (l.Contact__r.LeadSource=='Partner Referral'){
                l.rating='Warm';
            }
            
           else if (l.Contact__r.LeadSource=='Purchased List'){
                l.rating='Cold';
            }
            else if (l.Contact__r.LeadSource=='Other'){
                l.rating='Hot';
            }
   newLead.add(l);
    
   }
       System.debug('newLead:'+ newLead); 
        if(!newLead.IsEmpty()){
            update newLead;
        }     
}

All Answers

Harsh Panot 9Harsh Panot 9
Hey Ankit, 
you need to write Trigger on Contact and store trigger.new into a new list and write a method that will have nested if first for web and second for mobile,  which will check the origin of lead and on the basis of that update the lead field and store it into the list and use an update DML, to update the lead record. hope this will help you.
Ankit Gupta 290Ankit Gupta 290
trigger UpdateConRating on Contact (after insert, after update) {
    List<Lead> newLead = new List <Lead>();
    Set<id> conIdSet =new Set<id> ();
    for(Contact c:Trigger.new){
        conIdSet.add(c.id);
        }
        System.debug('conIdSet:'+conIdSet);
        
        for(Lead l : [Select id,rating,Contact__r.LeadSource from lead where Contact__c in:conIdSet]){
            if(l.Contact__r.LeadSource=='Web'){
                l.rating='Hot';
                
            } 
            else if (l.Contact__r.LeadSource=='Phone Inquiry'){
                l.rating='Warm';
            }
            else if (l.Contact__r.LeadSource=='Partner Referral'){
                l.rating='Warm';
            }
            
           else if (l.Contact__r.LeadSource=='Purchased List'){
                l.rating='Cold';
            }
            else if (l.Contact__r.LeadSource=='Other'){
                l.rating='Hot';
            }
   newLead.add(l);
    
   }
       System.debug('newLead:'+ newLead); 
        if(!newLead.IsEmpty()){
            update newLead;
        }     
}
This was selected as the best answer