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
Lisa HorneLisa 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;

}
Abhi_TripathiAbhi_Tripathi
Hi Lisa,

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