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
munna321munna321 

Help for trigger

Hi All,

 

Appreciate if you could help me correct this trigger


When a custom lookup field on opportunity(Contract__c) is updated then some fields from Contract standard object should autofill the fields on opportunity.

 

trigger UpdateOpportunity on Opportunity (after insertafter update) {

            //List opportunity to insert, update

      List<opportunity> insertOPList= new List<Opportunity>();

      List<opportunity> updateOPList= new List<Opportunity>();

 

      for(Contract co:trigger.new){

     //check for the contract lookup on Opprtuinty

      if(trigger.isinsert && trigger.isafter && OP.Contract__c==true){

     OP.add(new pportunity(Contract_Start_Date__c=co.Contract_Start_Date__c, Contract_End_Date__c=co.Contract_End_Date__c));

      }

     //check for the contract lookup update on Opprtuinty

      if(trigger.isupdate && trigger.isafter && OP.Contract__c==true){

     OP.add(new pportunity(Contract_Start_Date__c=co.Contract_Start_Date__c, Contract_End_Date__c=co.Contract_End_Date__c));

      }  

    }

    insert insertOPList;

    update updateOPList;

 

}

 

}





Imran MohammedImran Mohammed

Hi,

 

Try this

 

trigger UpdateOpportunity on Opportunity (after insert, after update) {
            //List opportunity to insert, update
      List<opportunity> insertOPList= new List<Opportunity>();
      List<opportunity> updateOPList= new List<Opportunity>();
  List<ID> contractIDList = new List<ID>();
 
      for(Opportunity o:trigger.new){
       if(Trigger.newMap.get(o.id).Contract__c != Trigger.oldMap.get(o.id).Contract__c)
       {
       contractIDlist.add(o.contract__c);
       }
      }
       Map<ID,Contract__c> coMap = new Map<ID, Contract__c<[select id, Contract_Strat_Date_c, Contract_End_Date__c from Contract__c where id in :contractIDList]>();
       Trigger.newMap.get(= 
 for(Opportunity o:Trigger.new)
 {
     //check for the contract lookup on Opprtuinty
  if(trigger.isinsert && OP.Contract__c==true){
   o.Contract_Start_Date__c = coMap.get(o.Contract__c.Contract_Start_Date__c);
      insertOPList.add(o);
      }
     //check for the contract lookup update on Opprtuinty
      if(trigger.isupdate && trigger.isafter && OP.Contract__c==true){
   o.Contract_Start_Date__c = coMap.get(o.Contract__c.Contract_Start_Date__c);
      updateOPList.add(o);
      }  
    }
    insert insertOPList;
    update updateOPList;
 
}
 
}

 

munna321munna321

Imran,

 

Thank you for the reply and i really appreciate your help...  can you please check these line again...

 

Contract is a standard object and Contract__c is a custom lookup field on opportunity. When this custom lookup is updated with some contract record then some fields from that contract record should auto fill the fields in Opportunity. Just for instance I gave some standard fields in Contract(StartDate and EndDate) which autofill into Opportunity custom fields(Contract_Start_Date__c and Contract_End_Date__c). Please help me with this.. Thanks.!!

 

trigger UpdateOpportunity on Opportunity (after insert, after update) {
            //List opportunity to insert, update
      List<opportunity> insertOPList= new List<Opportunity>();
      List<opportunity> updateOPList= new List<Opportunity>();
  List<ID> contractIDList = new List<ID>();
  
      for(Opportunity o:trigger.new){
       if(Trigger.newMap.get(o.id).Contract__c != Trigger.oldMap.get(o.id).Contract__c)
       {
       contractIDlist.add(o.contract__c);
       }
      }
       Map<ID,Contract__c> coMap = new Map<ID, Contract__c<[select id, Contract_Strat_Date_c, Contract_End_Date__c from Contract__c where id in :contractIDList]>();
       Trigger.newMap.get(= 
 for(Opportunity o:Trigger.new)
 {
     //check for the contract lookup on Opprtuinty
  if(trigger.isinsert && OP.Contract__c==true){
   o.Contract_Start_Date__c = coMap.get(o.Contract__c.Contract_Start_Date__c);
      insertOPList.add(o);
      }
     //check for the contract lookup update on Opprtuinty
      if(trigger.isupdate && trigger.isafter && OP.Contract__c==true){
   o.Contract_Start_Date__c = coMap.get(o.Contract__c.Contract_Start_Date__c);
      updateOPList.add(o);
      }  
    }
    insert insertOPList;
    update updateOPList;
 
}
 
}