+ Start a Discussion
K@SK@S 

please help me on this i didn't get o/p

 In opportunity object create a new Lookup – Deal Lead which is a lookup to Lead object.
Create new field Opportunity status and Lead status respectively in Opportunity and Lead.
 Both pick list should contain below values: Open, In progres,Closed
When opportunity status is getting modified modify corresponding lead status also.
Assume that you can associate one lead with only one opportunity
trigger Update_status_Trigger on Opportunity (after update) {
   Set<Id> oppId =new Set<Id>();
   List<Lead> updList =new List<Lead>();
   for(opportunity opps : Trigger.new){
       if(opps.status__c!=Trigger.oldMap.get(opps.id).status__c){
           oppId.add(opps.Id);
       }
   }
   for(Lead led : ([select id,name,status__c FROM Lead WHERE oppId IN : oppId ])){
       if(!Trigger.newMap.get(led.oppId).status__c.contains(led.status__c)){
           updList.add(led);
       }
   }
   try{
       if(updList .size()>0){
           Database.update(updList ,false);
       }
      }catch(Exception e){
       System.debug('########'+e.getMessage());
       }
}
Best Answer chosen by K@S
ManojjenaManojjena
HI Kae,

Try with below code and do update by chaging the status of one opportunity with Deal_Lead__c  look up field value .
 
trigger Update_status_Trigger on Opportunity (after update) {
   List<Lead> leadListToupdate =new List<Lead>();
   for(opportunity opps : Trigger.new){
       if(opps.status__c!=Trigger.oldMap.get(opps.id).status__c){
	       if(opps.Deal_Lead__c != null){
			   Lead led=new Lead(id=opps.Deal_Lead__c,status__c=opps.status__c);
			   leadListToupdate.add(led);
		   }
       }
   }
   try{
        if(leadListToupdate.size()>0){
          System.debug('***************************'+leadListToupdate.size());
           update leadListToupdate;
        }
   }Catch(DmlException de){
      System.debug('*********************'+de);
   }
}

Try to set a debug log.
I am for sure it will work .Incase any issue please send me the debug log to may mail .

manojjena202gmail.com .

Thanks 
Manoj
 

All Answers

sandeep sankhlasandeep sankhla
Hi Kae,

oppId is field on Lead ?? what is the dattype and what it contains ?

Thanks,
Sandeep
sandeep sankhlasandeep sankhla
Hi Kae,

Then you should correct this query..

 for(Lead led : ([select id,name,status__c FROM Lead WHERE oppId IN : oppId ])){

what you can do is, you can first collect all leadId's from opportunity as opporty is child and lead is parent

 for(opportunity opps : Trigger.new){
       if(opps.status__c!=Trigger.oldMap.get(opps.id).status__c){
           oppId.add(opps.Id);
       }
   }

here insetad of collecting opportunity Id you can collect Lead Id with that lookup field...

then that set you can us ein another query to get all leads 

for(Lead led : ([select id,name,status__c FROM Lead WHERE Id IN : LeadId ])){

Please check with these and let me know if it helps you..

Thanks,
Sandeep
sandeep sankhlasandeep sankhla
Hi,

Your code will look like this
 
trigger Update_status_Trigger on Opportunity (after update) {
   Set<Id> LeadId =new Set<Id>();
   List<Lead> updList =new List<Lead>();
   for(opportunity opps : Trigger.new){
       if(opps.status__c!=Trigger.oldMap.get(opps.id).status__c){
           LeadId .add(opps.Lead__c);
       }
   }
   for(Lead led : ([select id,name,status__c FROM Lead WHERE Id IN : LeadId ])){
       if(!Trigger.newMap.get(led.oppId).status__c.contains(led.status__c)){
           updList.add(led);
       }
   }
   try{
       if(updList .size()>0){
           Database.update(updList ,false);
       }
      }catch(Exception e){
       System.debug('########'+e.getMessage());
       }
}

Thanks,
Sandeep
K@SK@S
I got the error Compile Error: Invalid field oppId for SObject Lead at line 11 column 31
 
K@SK@S
Hi Sandeep,
this is also error pls solve the issuse.
trigger Update_status_Trigger on Opportunity (after update) {
   Set<Id> LeadId =new Set<Id>();
   List<Lead> updList =new List<Lead>();
   Map<Id,Opportunity> opportunity = new Map<Id,Opportunity>();
   for(opportunity opps : Trigger.new){
       if(opps.status__c!=Trigger.oldMap.get(opps.id).status__c){
           LeadId.add(opps.Id);
       }
   }
   for(Lead led : ([select id,name,status__c FROM Lead WHERE Id IN : LeadId])){
     if(Trigger.newMap.get(led.opportunity).status__c != led.status__c){
        updList.add(led);
       }
   }
   try{
       if(updList .size()>0){
           Database.update(updList ,false);
       }
      }catch(Exception e){
       System.debug('########'+e.getMessage());
       }
}
sandeep sankhlasandeep sankhla
Yes, that's why I was asking in my old comment, that what is the data type and what it is...

Thanks
sandeep sankhlasandeep sankhla
What is the error, you need to just provide the correct API name and then it will work...let me know what is the error..I haev shared the code for example only..you can provide the correct API name to make it run without error..

Please check and let m eknow if you still facing any issue..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
ManojjenaManojjena
Hi Kae,

Try with below code it will help !
 
trigger Update_status_Trigger on Opportunity (after update) {
   List<Lead> leadListToupdate =new List<Lead>();
   for(opportunity opps : Trigger.new){
       if(opps.status__c!=Trigger.oldMap.get(opps.id).status__c){
           Lead led=new Lead(id=opps.Lead__c,status__c=opps.status__c);
		   leadListToupdate.add(led);
       }
   }
   try{
     update leadListToupdate;
   }Catch(DmlException de){
      System.debug(de);
   }
}

Let me know if it helps .

Thanks
Manoj

 
sandeep sankhlasandeep sankhla
HI Kae,

I didnt notice , yes you can use above code , because in that you dont need to query because you are updating the lead..you just need to provide the correct API name ..

Please go to setup adn check the correct API name for lookup field on opportunity for lead..and give the correct API name then it will work for you..

Thanks,
Sandeep
ManojjenaManojjena
Hi Kae,

Please modify the code accordingly it wil help .
 
trigger Update_status_Trigger on Opportunity (after update) {
   List<Lead> leadListToupdate =new List<Lead>();
   for(opportunity opps : Trigger.new){
       if(opps.Staus field Api from opprortunity!=Trigger.oldMap.get(opps.id).Staus field Api from opprortunity){
           Lead led=new Lead(id=opps.Lead__c,Staus field Api from Lead=opps.Staus field Api from opprortunity);
		   leadListToupdate.add(led);
       }
   }
   try{
     update leadListToupdate;
   }Catch(DmlException de){
      System.debug(de);
   }
}

I have addes few language in code you need to modify accordingly it will help !

Thanks
Manoj
K@SK@S
Hi Manoj,
I was changed save the code but didn't get the out put.

trigger Update_status_Trigger on Opportunity (after update) {
   List<Lead> leadListToupdate =new List<Lead>();
   
   for(opportunity opps : Trigger.new){
       if(opps.status__c!=Trigger.oldMap.get(opps.id).status__c){
           Lead led=new Lead(id=opps.Deal_Lead__c(look up field),status__c=opps.status__c);
           leadListToupdate.add(led);
       }
   }
   try{
        if(leadListToupdate.size()>0){
            Database.update(leadListToupdate,false);
        }
   }Catch(DmlException de){
      System.debug(de);
   }
}
ManojjenaManojjena
HI Kae,

Try with below code and do update by chaging the status of one opportunity with Deal_Lead__c  look up field value .
 
trigger Update_status_Trigger on Opportunity (after update) {
   List<Lead> leadListToupdate =new List<Lead>();
   for(opportunity opps : Trigger.new){
       if(opps.status__c!=Trigger.oldMap.get(opps.id).status__c){
	       if(opps.Deal_Lead__c != null){
			   Lead led=new Lead(id=opps.Deal_Lead__c,status__c=opps.status__c);
			   leadListToupdate.add(led);
		   }
       }
   }
   try{
        if(leadListToupdate.size()>0){
          System.debug('***************************'+leadListToupdate.size());
           update leadListToupdate;
        }
   }Catch(DmlException de){
      System.debug('*********************'+de);
   }
}

Try to set a debug log.
I am for sure it will work .Incase any issue please send me the debug log to may mail .

manojjena202gmail.com .

Thanks 
Manoj
 
This was selected as the best answer
K@SK@S
Hi Manoj,Sandeep
I got it o/p.
thank you very munch......