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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Navatar_DbSupNavatar_DbSup

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

system.debug('=====test=========');
ac.abcPackage__CustomDate__c = trigger.new[0].closedate;

}
con.add(ac);
}
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.

 

thylakthylak

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

 

Trigger

=========

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