- Home
- Technical Library
- Boards
- Cookbook
- Code Share
- Blogs
- Partners
-
More
-
Services
- Training & Certification
- Support
-
Galleries
- Force.com Sites Gallery
- Chatter Challenge Entries
-
Other Web Sites
- Salesforce.com
- Database.com
- AppExchange
- CRM Community
-
Discussions
- Announcements
- General Development
- Schema Development
- New to Cloud Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules Discussion
- Security
- Mobile
- Force.com Sites
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby Development
- Adobe Flash Builder for Force.com
- Desktop Integration
- REST API Integration
- Streaming API
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- Excel Connector
- AJAX Toolkit & S-controls
- Force.com Builder & Native Apps
- AppExchange Directory & Packaging
- Force.com Labs Projects
- Open Source
- Site.com
- Jobs Board - Administrators
- Jobs Board - Developers
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- how to update the contact fields once opportunity...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
how to update the contact fields once opportunit y added using trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-24-2012 10:14 PM
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..................
Re: how to update the contact fields once opportunit y added using trigger
[ Edited ]
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-25-2012 06:35 AM - last edited on 02-25-2012 06:45 AM
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!
--dirk
++
If any of my replies/answers work for you, do mark them as 'Solution' as it may help others.
++
Re: how to update the contact fields once opportunit y added using trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-27-2012 02:54 AM
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............................
Re: how to update the contact fields once opportunit y added using trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-27-2012 04:34 AM
Will this work for you?!:
if(opp.ContractServiceDate__c != null && opp.ContractServiceDate__c < c.ContractServiceDate__c){
c.ContractServiceDate__c = opp.ContractServiceDate__c;
}--dirk
--dirk
++
If any of my replies/answers work for you, do mark them as 'Solution' as it may help others.
++
Re: how to update the contact fields once opportunit y added using trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-27-2012 05:25 AM
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...............
Re: how to update the contact fields once opportunit y added using trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-27-2012 06:55 AM
Can you pls post the complete trigger code here?
--dirk
--dirk
++
If any of my replies/answers work for you, do mark them as 'Solution' as it may help others.
++
Re: how to update the contact fields once opportunit y added using trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-27-2012 06:57 AM
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_
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.............
Re: how to update the contact fields once opportunit y added using trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-27-2012 07:12 AM
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 ;-)
--dirk
++
If any of my replies/answers work for you, do mark them as 'Solution' as it may help others.
++
Re: how to update the contact fields once opportunit y added using trigger
[ Edited ]
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-27-2012 07:28 AM - last edited on 02-27-2012 07:29 AM
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...............
Re: how to update the contact fields once opportunit y added using trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-27-2012 07:32 AM
I get your point!! Let me think about this ....
--dirk
++
If any of my replies/answers work for you, do mark them as 'Solution' as it may help others.
++

