You need to sign in to do that
Don't have an account?
thylak
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..................
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!
If this answer your question pls mark the entry as resolved!
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............................
Will this work for you?!:
--dirk
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...............
Can you pls post the complete trigger code here?
--dirk
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.............
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 ;-)
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...............
I get your point!! Let me think about this ....
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