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
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.
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
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.
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;
}
}