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
thylakthylak 

how to update the contact fields once opportunity added using trigger

how can update the contact fields once opportuity added using trigger.

1. i created two fields in Opportunity object i.e Contract Length and Service Date

2. i  created two fields in Contact obejct. i.e Contract Length and Service Date.

 

Now  i fills the value in contaract length and Service Date field in Opportunity object. then automatically  update the Contract Length and Service Date fields in contact object . how can do this task . i am new in apex codeing .pls help me..................

Dirk GronertDirk Gronert

Hi, I've tried to wrote the concept down .... I didn' check the syntaxt so it could be the case that some misspells are in the code ... but you will see how it works!

 

trigger SetContractdetailsOnContact on Opportunity (after insert, after update) { 
     // extract the contact id's fom the opportunities and store them in a list 
     List contactIDs = new List(); 
     for (Opportunity opp : trigger.new){ 
          if(opp.YOURCONTACTFIELD__c != null){ 
               contactIDs.add(opp.YOURCONTACTFIELD__c); 
           } 
     } // now query the contacts and put them in a Map 
   
     Map contactMap = new Map(); 
     for(Contact contact : [SELECT ID, ContractLength__c, ContractServiceDate__c from Contact Where id in :contactIDs]){ 
           contactMap.put(contact.id, contact); 
     } 
     // iterate over the opportunities and update the contact records 
     for(Opportunity opp : trigger.new){ 
         // only proceed if the CONTACTFIELD is not null - else skip the opportunity 
         if(opp.YOURCONTACTFIELD__c != null){ 
              // get the contact from the map 
              Contact c = contactMap.get(opp.YOURCONTACTFIELD__c); 
              // double check if the contact is really there - if not skip the record 
              if(c != null){ 
                    // check if the trigger is in the insert mode 
                    if(trigger.isInsert){ 
                            // if contractLength is set update the corresponding field on the contact record 
                            if(opp.ContractLength__c != null){ 
                                 c.ContractLength__c = opp.ContractLength__c; 
                            } 
                            // if ContractServiceDate is set update the corresponding field on the contact record 
                            if(opp.ContractServiceDate__c != null){ 
                                 c.ContractServiceDate__c = opp.ContractServiceDate__c; 
                            } 
                     }else if(trigger.isUpdate){ 
                            // if the trigger is in the context of updating oppty records 
                            // if contractLength is set AND changed update the corresponding field on the contact record 
                            if(opp.ContractLength__c != null && trigger.oldMap.get(opp.ID).ContractLength__c != opp.ContractLength__c){ 
                                  c.ContractLength__c = opp.ContractLength__c; 
                            } 
                            // if ContractServiceDate is set AND changed update the corresponding field on the contact record 
                            if(opp.ContractServiceDate__c != null && trigger.oldMap.get(opp.ID).ContractServiceDate__c != opp.ContractServiceDate__c){ 
                                  c.ContractServiceDate__c = opp.ContractServiceDate__c; 
                            } 
                      } 
               } 
         } 
     } 
     upsert contactMap.values(); 
}

  If this answer your question pls mark the entry as resolved!

thylakthylak

Thanks  for your post Dirk Gronert.i am using your code after just i modified that code that's working fine . but here one issuei.e 

    example :  one contact contain 3 opportunitys that time Contactservicedate update only today date. i need Contactservicedate update the minimum date field associated with Opportunity . how can solve this problem .pls help me............................

Dirk GronertDirk Gronert

Will this work for you?!:

 

if(opp.ContractServiceDate__c != null && opp.ContractServiceDate__c < c.ContractServiceDate__c){ 
                                 c.ContractServiceDate__c = opp.ContractServiceDate__c; 
                            }

--dirk

thylakthylak

Thanks for your post  Dirk Gronert.. this code not working Dirk. My Task

1. i created contact.

                                 i. for this contact i created one opportunity  that time i choose date field is 29/2/2012. now contact populated this date.

 

                              ii. for this same contact i created second opportunity that time i choose date field is 3/3/2012 . now Contact populated this date.  this happend currently

 

but here  minimum date is  29/2/2012  i need this date is populated in contact how can solve this problem . pls help me...............

Dirk GronertDirk Gronert

Can you pls post the complete trigger code here?

--dirk

thylakthylak

Ok  Dirk

 

Code

=====

List<ID> oppcon = new List<ID>();
        List<ID> oppcon1 = new List<ID>();
        List<OpportunityContactRole> oppconlist = new List<OpportunityContactRole>();
        List<Contact> conlist = new List<Contact>();
        List<ID> oppid = new List<ID>();
        Map<ID,Opportunity> opp = new Map<ID,Opportunity>();
        Map<ID,Opportunity> omap = new Map<ID,Opportunity>();

   

  for(Opportunity o: Trigger.new) {
            System.debug('Opportunity:'+o);
            opp.put(o.id,o); //to update contact
            oppid.add(o.id); // to update contact

}

Map<ID,ID> oppmap = new Map<ID,ID>();
        for(OpportunityContactRole ocr:[Select id,ContactId,OpportunityId from OpportunityContactRole where  OpportunityId in:oppid]){
            oppconlist.add(ocr);
            oppmap.put(ocr.ContactId,ocr.OpportunityId);
            oppcon1.add(ocr.Contactid);
            oppcon.add(ocr.Opportunityid);
            system.debug('Testing'+oppcon);
        }
        
        List<Date> datelist = new List<Date>();
        for(Contact c:[Select id,Contract_Length_Months__c,Provider__c,Original_Service_Start_Date__c ,Amount__c from Contact where id in :oppcon1 and closedate !=null]){
            Id oid =oppmap.get(c.id);
            Opportunity o = new Opportunity();
            o =opp.get(oid);
            if(c.Contract_Length_Months__c != null)
            c.Contract_Length_Months__c = o.Contract_Length__c+c.Contract_Length_Months__c;
            else
            c.Contract_Length_Months__c = o.Contract_Length__c;
            
        /*  if(c.Original_Service_Start_Date__c !=null)              // this code working for updated latest date.
            c.Original_Service_Start_Date__c = Date.today();
            else
            c.Original_Service_Start_Date__c = o.CloseDate;  */
            
          if(c.Original_Service_Start_Date__c!= null && o.CloseDate< c.Original_Service_Start_Date__c) // this code for Minimum date updated
             c.Original_Service_Start_Date__c  = o.CloseDate;
             else
             c.Original_Service_Start_Date__c  = o.CloseDate;
            if(c.Amount__c !=null)
            c.Amount__c = Amt+c.Amount__c;
            else
            c.Amount__c = Amt;
            if(c.Provider__c !=null)
            c.Provider__c = o.Provider__c; //+ c.Provider__c;
            else
            c.Provider__c  = o.Provider__c;
            conlist.add(c);
        }
        system.debug('Testing'+conlist);
        update conlist;
        //*******************************************************************************
        
    } //End of Trigger.isInsert
}

 

Pls help me how can solve this problem.............

Dirk GronertDirk Gronert

Sure .. The answer is easy ...

 

if(c.Original_Service_Start_Date__c!= null && o.CloseDate< c.Original_Service_Start_Date__c) // this code for Minimum date updated
             c.Original_Service_Start_Date__c  = o.CloseDate;
else
             c.Original_Service_Start_Date__c  = o.CloseDate;

 

You are setting c.Original_Service_Start_Date__c with the same value o.CloseDate in if and else ....!

 

Something like this would do the trick:

 

if(c.Original_Service_Start_Date__c!= null && o.CloseDate< c.Original_Service_Start_Date__c) // this code for Minimum date updated
             c.Original_Service_Start_Date__c  = o.CloseDate; 

 

Without the else ;-)


thylakthylak

Hi Dirk Gronert this code is not working.

 

 

if(c.Original_Service_Start_Date__c!= null && o.CloseDate< c.Original_Service_Start_Date__c) // this code for Minimum date updated
             c.Original_Service_Start_Date__c  = o.CloseDate;
else
             c.Original_Service_Start_Date__c  = o.CloseDate;

 

You are setting c.Original_Service_Start_Date__c with the same value o.CloseDate in if and else ....!

 

Something like this would do the trick:

 

if(c.Original_Service_Start_Date__c!= null && o.CloseDate< c.Original_Service_Start_Date__c) // this code for Minimum date updated
             c.Original_Service_Start_Date__c  = o.CloseDate;

 

 

 

1. i created contact.

                                 i. for this contact i created one opportunity  that time i choose date field is 13/2/2012. now contact populated this date.

 

                              ii. for this same contact i created second opportunity that time i choose date field is 24/2/2012 . now Contact populated this date.  this happend currently

 

but here  minimum date is  13/2/2012  i need this date is populated in contact how can solve this problem . pls help me...............

Dirk GronertDirk Gronert

I get your point!! Let me think about this ....

Dirk GronertDirk Gronert

At the end of your last posted code there is this comment: //End of Trigger.isInsert

 

Would mean the code is just for isInsert ... what about the isUpdate part?"

 

Can you pls post your complete trigger!

 

THX,

 

--dirk