Is it possible – I have a use case – When a user creates a new opportunity record, they will not be able to save it until they have added one Contact Role. Any Suggestions??
[SalesForce] Trigger to make Contact Roles required on Opportunities
Related Solutions
Opportunity Contact Roles (OCR) by definition cannot be created prior to the opportunity existing.
Thus, when a user saves a new opportunity, there will never be a related OCR.
You could do this if you wanted to prevent opportunity updates without a related OCR:
- Create a read-only integer field on your opportunity
- Create a before-update trigger on the opportunity that populates this field with a count of related OCRs
- Create a validation rule that prevents saving an opportunity when the number field is not greater than 0.
Pseudo code for trigger:
trigger updateCount on Opportunity (before update) {
map <id, list<opportunityContactRole>> myMap = new map <id, list<opportunityContactRole>> ();
for (opportunitycontactrole OCR: [SELECT id, opportunityid, contactid, role from opportunityContactRole WHERE opportunityid in :trigger.old]) {
if (myMap.containsKey(OCR.opportunityid)) {
myMap.get(OCR.opportunityid).add(OCR);
} else {
myMap.put(OCR.opportunityid, new list <opportunitycontactrole> {OCR});
}
}
list <opportunity> oppsToUpdate = new list <opportunity> ();
for (opportunity oppID: myMap.keySet()) {
oppsToUpdate.add( new opportunity(id=oppID, OCRcount=myMap.get(oppID).size()))
}
update oppsToUpdate;
}
Welcome to StackExchange, Patrick!
It's really difficult to cut off all avenues for creating an opportunity. If someone were to use DataLoader, or Salesforce for Outlook, or etc, they could create an opp without hitting any of the roadblocks!
I'd go back to the business and question the assumption: Do they really need to prevent opportunities from being created without a contact? Would it be good enough (or better) to prevent opportunities from moving past a certain stage without a contact assigned? An argument in favor of the latter: If a sales rep wanted to open an opportunity but didn't have the best contact's info, the rep might create a bogus contact just to get things going. The current business requirement is encouraging that.
If you can go with preventing the opp from moving past a given stage without a contact role assigned, then you can use a before update trigger (or a visual flow) to enforce the rule. Here's one example:
Best Answer
This is a bit of a pain as you can't use a roll up summary field or write a trigger on the OpportunityContactRole object. This is a free, unmanaged, Appexchange product that does this:
https://appexchange.salesforce.com/listingDetail?listingId=a0N300000025Vs1EAE
Note the most helpful negative review:
https://appexchange.salesforce.com/listingDetail?listingId=a0N300000025Vs1EAE&tab=r
The issue is the trigger fires on Opportunity updates so the user will have to cancel out and add a OCR (potentially having to create a Contact first etc).
I have used a modified version of this in combination with a validation rule so that a OCR must be present at a certain stage, but we have actually adopted a different approach. We replaced the New Opportunity screen with a custom VisualForce page that included a section dedicated to making it easy to add an existing Contact as an OCR or creating a brand new Contact all from the same screen.
We decided this was more user friendly as well making all the Opportunities populated with an OCR from the start - so better data quality earlier.