0

When creating an opportunity as of now opportunity contactrole is not mandatory.System should display error message "Opportunity Contact Role" is required to create an opportunity if system did not find up the "opportunity contact role" upon creation of the opportunity.You can use triggers and utility class

2
  • Hi Robert, can you please add some more details to your question and post what options you have explored so far.. giving a statement of work / requirement wont help us much unless you can explain / highlight the specific issue you are facing – Vamsi Krishna Gosu Sep 29 '15 at 15:39
  • Actually i am just beginner.I have not worked much on opportunity.I have explored like i have created a custom field contact on opportunity page but not dont know how to link it with contactrole.As contact role is a related list and we cant create trigger on contact role. – Jack vardy Sep 29 '15 at 16:08
1

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:

  1. Create a read-only integer field on your opportunity
  2. Create a before-update trigger on the opportunity that populates this field with a count of related OCRs
  3. 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;
}
1

One solution (requires a bit of work) is to override the New Opportunity button with a VF page and controller

The VF page includes all the fields needed to enter a new Opportunity plus a page block section of related Contacts on the Opportunity's Account with a way to pick the Contact(s) as an OCR, assign the role, and optionally allow creation of a new Contact+role

Then, when the Save button is clicked, your VF controller a) Verifies that a Contact was selected to be the OCR b) Creates a Contact (if specified) by user c) Saves the Opportunity d) Creates OCR(s) from the user-specified contact+role on the VF page

Then, if you want to ensure that when an Oppo is updated that an OCR exists; you can use @krewllobster's solution

A more difficult problem is when a user deletes an OCR - no trigger will fire in this event but you can put a VF page on your Opportunity standard layout that invokes a VF controller that displays an alert - "Missing OCR". When the OCR is deleted, the standard Oppo detail page is displayed and hence the VF component on that page can display the alert that hopefully the user acknowledges

1
  • N.B. with Summer 19, OCR triggers are no possible – cropredy Nov 26 '19 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.