You need to sign in to do that
Don't have an account?

Adding contact roles
Hi
I am using this app from the app exchange (https://appexchange.salesforce.com/listingDetail?listingId=a0N300000016cbDEAQ) to ensure that a contact role is added to an opportunity when a certain stage is selected on the opportunity. This works perfectly when updating an existing opportunity but I dont want it to apply when creating new opportunities.
I thought I could just add NOT(IsNew()) to the custom field, but it gives an error of IsNew can't be used in this type of formula.
Any ideas of how I can exclude this trigger from firing when creating a new opportunity would be appreciated?
Custom Field
IF( DATEVALUE( CreatedDate ) >= DATE(2017,09,28), CASE( StageName, "Stage to be Required 1",1, "Define",1, "Meeting / Proposal",1, "Negotiation",1, "Verbal Agreement/Contract",1, "Closed Won",1, 0 ), 0 )
Apex Class
//This is provided as a sample to require a contact on opportunities it is provided without warranty and support. trigger opportunity_contact_required on Opportunity (before insert, before update) { //map to keep track of the contact_required = 1 Map<String, Opportunity> oppy_contact = new Map<String, Opportunity>(); //Trigger.new is an array of opportunities //and adds any that have Contact_Required = 1 to the oppy_contact Map for (Integer i = 0; i < Trigger.new.size(); i++) { System.debug('*****Required? ' + Trigger.new[i].contact_required__c); if (Trigger.new[i].contact_required__c == 1) { oppy_contact.put(Trigger.new[i].id,Trigger.new[i]); } } //map to keep track of the opportunity contact roles map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>(); //select OpportunityContactRoles for the opportunities with contact role required List<OpportunityContactRole> roles = [select OpportunityId, IsPrimary from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppy_contact.keySet())]; for (OpportunityContactRole ocr : roles) { //puts the contact roles in the map with the Opportunity ID as the key oppycontactroles.put(ocr.OpportunityId,ocr); } // Loop through the opportunities and check if they exists in the contact roles map or contact role isn't required for (Opportunity oppy : system.trigger.new) { //system.debug('List oppy Id - '+oppy.id); if (oppy.contact_required__c ==1 && !oppycontactroles.containsKey(oppy.id)) { oppy.addError('No Primary Contact has been added to the opportunity. Please go to the Contact Roles and select a primary contact.'); } } //for }
Apex Test Class
public class test_opportunity_contact_required { // test to ensure an opportunity can be added public static testMethod void testoppyrequired0() { //create oppty List<Opportunity> oppy = new List<Opportunity>(); //add 10 opportunites without a contact, and with the condition contact required = 0 for (Integer i = 0; i < 10; i++) { oppy.add(new Opportunity(Name='nick_test'+i,StageName='Perception Analysis',CloseDate=System.Today())); } insert oppy; map<Id, Opportunity> oppy_map = new map<Id, Opportunity>(); for (Integer i = 0;i<10;++i){ oppy_map.put(oppy[i].Id,oppy[i]); } //for System.assert([SELECT count() FROM Opportunity WHERE Id IN :oppy_map.keySet()] == 10); } //testoppyrequired = 0 //test to go from a not required value to a required value public static testMethod void testoppyrequired1() { //create oppty List<Opportunity> oppy2 = new List<Opportunity>(); //add 10 opportunites without a contact, and with the condition contact required = 0 for (Integer i = 0; i < 10; i++) { oppy2.add(new Opportunity(Name='nick_test'+i,StageName='Qualification',CloseDate=System.Today())); } insert oppy2; for (Integer i = 0; i < 10; i++) { oppy2[i].StageName='Negotiation/Review'; } Test.startTest(); try { update oppy2; Opportunity sampleTest = [Select Id, Contact_Required__c From Opportunity where Id = :oppy2[0].id]; System.debug('*****SAMPLE' + sampleTest); System.assert(false, 'This update should have failed.'); } catch(System.DmlException e) { System.assert(e.getMessage().contains('No Primary Contact Exists.')); } Test.stopTest(); } //testoppyrequired = 1 public static testMethod void testoppyrequired1woprimary() { //create oppty List<Opportunity> oppy = new List<Opportunity>(); //add 10 opportunites for (Integer i = 0; i < 10; i++) { oppy.add(new Opportunity(Name='nick_test'+i,StageName='Qualification',CloseDate=System.Today())); } insert oppy; //add 10 contacts List<Contact> c = new List<Contact>(); for (Integer i = 0; i < 10; i++) { c.add(new Contact(LastName='nick_test'+i)); } insert c; for (Integer i = 0; i < 10; i++) { oppy[i].StageName='Negotiation/Review'; } //add 10 opporunity contact roles associated to the opportunities and contacts above List<OpportunityContactRole> ocr = new List<OpportunityContactRole>(); for (Integer i = 0; i < 10; i++) { ocr.add(new OpportunityContactRole(Role='Business User',OpportunityId=oppy[i].id,ContactId=c[i].id)); } insert ocr; boolean caughtException = false; Test.startTest(); try { update oppy; } catch(System.DmlException e) { System.assert(e.getMessage().contains('No Primary Contact Exists.')); caughtException = true; } Test.stopTest(); System.assert(caughtException); } //testoppyrequired = 1 public static testMethod void testoppyrequired1primary() { //create oppty List<Opportunity> oppy = new List<Opportunity>(); //add 10 opportunites for (Integer i = 0; i < 10; i++) { oppy.add(new Opportunity(Name='nick_test'+i,StageName='Qualification',CloseDate=System.Today())); } insert oppy; map<Id, Opportunity> oppy_map = new map<Id, Opportunity>(); for (Integer i = 0;i<10;++i){ oppy_map.put(oppy[i].Id,oppy[i]); } //for //add 10 contacts List<Contact> c = new List<Contact>(); for (Integer i = 0; i < 10; i++) { c.add(new Contact(LastName='nick_test'+i)); } insert c; //add 10 opporunity contact roles associated to the opportunities and contacts above List<OpportunityContactRole> ocr = new List<OpportunityContactRole>(); for (Integer i = 0; i < 10; i++) { ocr.add(new OpportunityContactRole(Role='Business User',OpportunityId=oppy[i].id,ContactId=c[i].id,IsPrimary=True)); } insert ocr; for (Integer i = 0; i < 10; i++) { oppy[i].StageName='Negotiation/Review'; } try { update oppy; System.assert([SELECT count() FROM Opportunity WHERE Id IN :oppy_map.keySet()] == 10); } catch(System.DmlException e) { System.assert(false); } } //testoppyrequired = 1 and primary contact = true } //test class
Shawn
All Answers
Shawn