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

how to add the minimum date field using trigger.

i updated the  contact date field based on opportunity close date field using trigger.

i . i  created contact record after i created 3 opportuinty records for contact. now automatically date field updated in contact .based on opportunity close date. i need get the minimum date from opportunity then update the datefield in contact object. pls help me ...................




































Your trigger should be like this:
trigger Opportunitydate on Opportunity(after insert)
list<contact> Con=new list<contact>();
for(contact ac:[select id, abcPackage__CustomDate__c from contact where[0].accountid])//and closeddate != null order by closeddate desc limit 1];
if(ac.abcPackage__CustomDate__c == null)
ac.abcPackage__CustomDate__c =[0].closedate;
system.debug('@@@@@@@@@@@@' +[0].closedate +'old-----------' + ac.abcPackage__CustomDate__c);
if(trigger.isinsert &&[0].closedate < ac.abcPackage__CustomDate__c )

ac.abcPackage__CustomDate__c =[0].closedate;

update con;
Please make changes accordingly for update and delete.

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.



Thanks for your post Navatar. My Task is  Update the Contact Fields based on Opportunity using Trigger.currently Contact is updated based on Opportutnity using trigger working fine but issue is i need update the minimum date field in contact based on Opportunity.


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...............




       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: {
            opp.put(,o); //to update contact
            oppid.add(; // to update contact


Map<ID,ID> oppmap = new Map<ID,ID>();
        for(OpportunityContactRole ocr:[Select id,ContactId,OpportunityId from OpportunityContactRole where  OpportunityId in:oppid]){
        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(;
            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;
            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 =;
            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;
             c.Original_Service_Start_Date__c  = o.CloseDate;
            if(c.Amount__c !=null)
            c.Amount__c = Amt+c.Amount__c;
            c.Amount__c = Amt;
            if(c.Provider__c !=null)
            c.Provider__c = o.Provider__c; //+ c.Provider__c;
            c.Provider__c  = o.Provider__c;
        update conlist;
    } //End of Trigger.isInsert


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