2

I know there are many solutions for making the Contact Role required on the Opportunity object, but I couldn't find one that could solve my problem.

I need the Contact Role to be required if the opportunity stage is fourth on the list (“Negotiate Contract”) or higher. Also, I want to prevent deletion of the Contact Role after it’s been selected and the stage is not changed back to the third on the list or lower.

Is there any way this could be done? I have no experience in coding. Till now, I just used apps, triggers, flows and processes which I found on the Internet.

Thanks.

  • 1
    If you're open to using DLRS, it would be simple enough to roll up the count and just validate that the result field equals zero and join that with your picklist condition. – Adrian Larson Feb 8 '18 at 20:00
  • I am not familiar with DLRS, but I will look into it. Thanks for the answer, Adrian. – Ivan Feb 8 '18 at 23:59
  • I tried using DLRS, but I can't finish deploying Apex Trigger and Apex Class. I get the error message: "Error: triggers/dlrs_OpportunityContactRoleTrigger.trigger (Line: 4: Column:9) : SObject type does not allow triggers: OpportunityContactRole" I am not even sure if I am setting the required fields right... – Ivan Feb 9 '18 at 14:41
  • That means you can't use the Real Time calculation mode. Change it to scheduled. You'll get daily updates. If you want to do better than that, you'll need enough coding experience to write your own trigger. You'd have to teach yourself some Apex, Triggers, and SOQL, at a minimum. – Adrian Larson Feb 9 '18 at 15:12
  • I read somewhere that I need to use the scheduled calculation mode. Will try with that for now, but I believe I will need Real Time at some point... Learning coding is something I will definitely need to do. – Ivan Feb 9 '18 at 15:26
0

To achieve this requirement using just your current skill set, you're going to have to live with a compromise. Because of the inability to add an Apex Trigger to OpportunityContactRole, any rollup calculations will have to be scheduled, meaning they'll run daily.

I'll copy my recommendation from the comments:

If you're open to using DLRS, it would be simple enough to roll up the count and just validate that the result field equals zero and join that with your picklist condition.

It sounds like you know where to take it from there. That's your best option until you work your way through several Trailhead modules and master the topics of:

Once you master those topics, you will be able to write a validation using Apex, specifically the SObject.addError method. For what it's worth, your service method to filter records which have an OCR could look something like:

public with sharing class OpportunityService
{
    // @param records - pass in trigger.new
    public static List<Opportunity> getRecordsWithOcr(List<Opportunity> records)
    {
        List<Opportunity> recordsWithOcr = new List<Opportunity>();
        Set<Id> opportunityIdsWithOcr = new Map<Id, AggregateResult>([
            SELECT OpportunityId Id FROM OpportunityContactRole
        ]).keySet();
        for (Opportunity record : records)
        {
            if (opportunityIdsWithOcr.contains(record.Id)
            {
                recordsWithOcr.add(record);
            }
        }
        return recordsWithOcr;
    }
}
  • Once more, thank you for your help Adrian. I will work on the modules and try to solve the problem. – Ivan Feb 10 '18 at 10:26
  • I'll throw a few more links in there later today. – Adrian Larson Feb 10 '18 at 14:31

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.