You need to sign in to do that
Don't have an account?

trigger for three object
Hi,
account obj is parent of Lead and opportunity.I want to copy Lead status from lead obj to opportunity obj with matching of account id.Please help.
account obj is parent of Lead and opportunity.I want to copy Lead status from lead obj to opportunity obj with matching of account id.Please help.
trigger CopyleadvaluetoOpp on Opportunity (after insert) {
List<Account> accounts = new List<Account>();
for (Opportunity Opp: Trigger.new)
{
If(Opp.Lead_Exception__c == null)
{
List<Lead> Ld = [SELECT Id,Status FROM Lead];
List<Account> Acc = [Select Id FROM Account];
for(Lead L: Ld)
{
If(Opp.Account.Id == Ld.Account.Id)
{
Opp.Id = L.Id;
Opp.Lead_Exception__c = L.Status;
}
}
}
}
}
But it is still not working.Please help me out
Can you try the below trigger and if the only field you are updating is a status field then I would suggest you to use before trigger, using after trigger you would be using a soql query update statement unnecessarily, to check the difference between after and before triggers then you can check these below links:
>> https://salesforce.stackexchange.com/questions/96263/what-is-the-exact-difference-between-before-update-and-after-update-trigger
>> https://www.sfdc99.com/2014/01/25/use-vs-triggers/
>> https://www.brcline.com/blog/whats-the-difference-between-before-and-after-triggers#:~:text=Before%20triggers%20execute%20before%20the%20data%20has%20been%20committed%20into%20the%20database.&text=After%20triggers%20execute%20after%20the,the%20case%20of%20an%20insert.
Please do note that this works for one lead and in case if you want to concatinate the status for multiple leads then you will have to update the soql and logic as per the scenario.
I would suggest you try checking the https://trailhead.salesforce.com/content/learn/modules/apex_triggers trailhead link that could help you get started with the trigger.
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.
trigger CopyleadvaluetoOpp on Opportunity (before insert) {
Set <String> OppotunityCustId = new Set <String> ();
for(Opportunity Opp : trigger.New)
{
OppotunityCustId.add(Opp.Customer_Id__c);
}
//Map <String, Lead> matchingLeadMap = new Map <String, Lead> ();
List <Lead> newLead = new List<Lead> ([Select Id,Status,Customer_Code__c,Lead_No__c From Lead Where Customer_Code__c IN :OppotunityCustId AND Status != 'Converted']);
/*{
matchingLeadMap.put(Ld.Status, Ld);
}*/
List <Opportunity> opportunityToUpdate = new List <Opportunity> ();
for(Opportunity Opp : trigger.New)
{
if (newLead.size()>0)
{
// we found a mathing one
Opp.Lead_Exception__c = true;
//Opp.adderror('Lead already exists for this account. Please convert lead to create account.');
// add it to a separate list and update it
opportunityToUpdate.add(Opp);
}
}
//update OpportunityToUpdate;
}
But if want to update lead no into opportunity filed then what I need to do.Please help.