You need to sign in to do that
Don't have an account?
Lisa Horne
Help with a test class for this trigger
The Contact Role object cannot 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 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).
Could someone confirm that this code will do that and also assist in creating a Class for it?
trigger New_Contact_Role_to_Standard on Contact_Role__c {
List<OpportunityContactRole> lstOCRs = new List<OpportunityContactRole>();
for (Contact_Role__c cr : trigger.new) {
if ((cr.Contact_Name__c != null && cr.Parts__c != null) &&
(trigger.isInsert || (trigger.isUpdate && trigger.oldmap.get(cr.Id).Contact_Name__c == null || trigger.oldmap.get(cr.Id).Parts__c == null))) {
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.OpportunityId = cr.Opportunity__c;
ocr.ContactID = cr.Contact_Name__c;
ocr.Role = cr.Parts__c;
lstOCRs.add(ocr);
}
}
update lstOCRs;
}
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).
Could someone confirm that this code will do that and also assist in creating a Class for it?
trigger New_Contact_Role_to_Standard on Contact_Role__c {
List<OpportunityContactRole> lstOCRs = new List<OpportunityContactRole>();
for (Contact_Role__c cr : trigger.new) {
if ((cr.Contact_Name__c != null && cr.Parts__c != null) &&
(trigger.isInsert || (trigger.isUpdate && trigger.oldmap.get(cr.Id).Contact_Name__c == null || trigger.oldmap.get(cr.Id).Parts__c == null))) {
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.OpportunityId = cr.Opportunity__c;
ocr.ContactID = cr.Contact_Name__c;
ocr.Role = cr.Parts__c;
lstOCRs.add(ocr);
}
}
update lstOCRs;
}
Yes your code looks good, for test class you can go to this post
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html
Regards,
Abhi Tripathi
SFDC Developer