You need to sign in to do that
Don't have an account?
Lisa Horne
Trigger to add Contact Role Record to Opportunity
The Contact Role object can not have fields added to it. In order to get around this I have created a custom object with an Object Name= OpportunityContactRoles and gave it a child relationship to the opportunity object. The custom object has the two fields that are on the Contact Role object, ContactID and Object Name Parts (same as Role), but also has additional fields on it.
What I would like to have happen is every time a Contact name and Parts has been entered into the custom object and saved, a new standard Contact Role is automatically created on the opportunity with the same Contact name and Role (Parts). Can anyone help me with a trigger and class for this? So far I have this but I know it's not even close.
trigger new_Contact_Role_to_Standard on Contact Role
{
for(Contact Role c : Trigger.New)
{
OpportunityContactRole o = new OpportunityContactRole;
o.ContactId = set the contact id here;
o.OpportunityId = set the Opportunity id here;
insert o;
}
}
What I would like to have happen is every time a Contact name and Parts has been entered into the custom object and saved, a new standard Contact Role is automatically created on the opportunity with the same Contact name and Role (Parts). Can anyone help me with a trigger and class for this? So far I have this but I know it's not even close.
trigger new_Contact_Role_to_Standard on Contact Role
{
for(Contact Role c : Trigger.New)
{
OpportunityContactRole o = new OpportunityContactRole;
o.ContactId = set the contact id here;
o.OpportunityId = set the Opportunity id here;
insert o;
}
}
trigger New_Contact_Role_to_Standard on Contact_Role__c {
for (Contact_Role__c cr : trigger.new) {
(trigger.isInsert || (trigger.isUpdate && trigger.oldmap.get(cr.Id).Contact_Name__c == null || trigger.oldmap.get(cr.Id).Parts__c == null))) {
ocr.OpportunityId = cr.Opportunity__c;
ocr.ContactID = cr.Contact_Name__c;
ocr.Role = cr.Parts__c;
lstOCRs.add(ocr);
}
All Answers
trigger New_Contact_Role_to_Standard on Contact_Role__c {
for (Contact_Role__c cr : trigger.new) {
(trigger.isInsert || (trigger.isUpdate && trigger.oldmap.get(cr.Id).Contact_Name__c == null || trigger.oldmap.get(cr.Id).Parts__c == null))) {
ocr.OpportunityId = cr.Opportunity__c;
ocr.ContactID = cr.Contact_Name__c;
ocr.Role = cr.Parts__c;
lstOCRs.add(ocr);
}
Thanks!