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
Supriyo Ghosh 9Supriyo Ghosh 9 

value not updating with trigger

Hi,

I have written one trigger.

trigger CopyleadvaluetoOpp on Opportunity (after 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> ();
   
   for (Lead Ld : [Select Id,Status,Customer_Code__c From Lead Where Customer_Code__c IN :OppotunityCustId])
   {
    matchingLeadMap.put(Ld.Status, Ld);
   }
   
   List <Opportunity> opportunityToUpdate = new List <Opportunity> ();
   
   for(Opportunity Opp : trigger.New)
{
    if (matchingLeadMap.get(Opp.Customer_ID__c) != null)
    {
        // we found a mathing one
        Opp.Lead_Exception__c = matchingLeadMap.get(Opp.Lead_Exception__c).Id;

        // add it to a separate list and update it
        opportunityToUpdate.add(Opp);
    }

update OpportunityToUpdate;
}

but that Status form lead is not coping in opportunity lead exception field.Please help.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Supriyo,

Can you elaborate on the scenario you are trying to address so that we can try the same in our orgs and search for the same,

Looking forward to your response.

Thanks.
Supriyo Ghosh 9Supriyo Ghosh 9
I will take lead status form lead obj and put that value into opportunity obj lead_exception__c field after create a new opportunity.For both lead and opportunity for a same account master.So account master having a Lead.in that we have lead status.then when we create a opportunity for that same account then lead status should copy to lead_exception field.please help.
Maharajan CMaharajan C
Hi Supriyo,

No need of after trigger... There is no need of Update also...

Please use the code:
 
trigger CopyleadvaluetoOpp on Opportunity (before insert) {

	Set <String> OppotunityCustId = new Set <String> ();
	Map <String, Lead> matchingLeadMap = new Map <String, Lead> ();

	for(Opportunity Opp : trigger.New)
	{
		OppotunityCustId.add(Opp.Customer_ID__c);
	}

	for (Lead Ld : [Select Id,Status,Customer_Code__c From Lead Where Customer_Code__c IN :OppotunityCustId])
	{
		matchingLeadMap.put(Ld.Customer_Code__c, Ld);
	}

	for(Opportunity Opp : trigger.New)
	{
		if (matchingLeadMap.containsKey(Opp.Customer_ID__c))
		{
			Opp.Lead_Exception__c = matchingLeadMap.get(Opp.Customer_ID__c).Status;
		}
	} 
}

Thanks,
Mahrajan.C
Supriyo Ghosh 9Supriyo Ghosh 9
Opp.Lead_Exception__c = matchingLeadMap.get(Opp.Customer_ID__c).Status;

This one is not working.Please help.