1

Code novice here! I am trying to pass the OpportunityContactRole's Opportunity ID from a trigger to a class to a Flow. I would happily use a Process Builder to trigger the Flow but OCR's are yet to run by a PB.

The trigger will run whenever an OCR is added or edited. It should pass the OCR's Opportunity Id to the Class which should in turn pass the OCR's OpportunityId to a flow.

As you can see below, I haven't got very far, but here's what I have pieced together by reading other posts and code

Trigger:

Trigger Default on OpportunityContactRole after insert, after update){
 AddPrimaryContactToOpportunity.runFlow();
}

Class:

public class AddPrimaryContactToOpportunity { 
    public static void runFlow() {
        OpportunityContactRole currentRecord;

        public OpportunityContactRole getcurrentRecord(){
            return currentRecord;
        }

        Flow.Interview flow = new Flow.Interview.Update_Primary_Contact_lookup(new map<String,Object> 
                                                    {'recordId' => currentRecord.OpportunityId});     
        flow.start();
}

Appreciate any help here!

Edit - trigger syntax

  • I have some questions, your trigger code says it runs on the Account object. However, you're trying to get an Opportunity or OpportunityContactRole Id. Can you clarify a little bit or maybe you have to edit your code snippets – Bryan Anderson Nov 20 '19 at 18:44
  • 1
    From the account trigger you can pass the list of accounts as a parameter to the run flow method (Triggers are designed for bulkification so the correct methodology is to assume there is always more then one account being inserted at once and handle them all at once) AddPrimaryContactToOpportunity.runFlow(Trigger.new); public static void runFlow(List<Accounts> acctList) {...} however you will have to query for the related OpportunityContactRole in the runFlow method using that Account Id of the accounts from the list – Calvin OKeefe Nov 20 '19 at 19:09
  • @BryanAnderson sorry, I got the trigger object incorrect. The trigger runs on the Opportunity Contact Role object. – Uninvited Guest Nov 21 '19 at 2:24
  • @CalvinOKeefe sorry, I got the trigger object incorrect. The trigger runs on the Opportunity Contact Role object. – Uninvited Guest Nov 21 '19 at 2:25
1

So the issue I see here is that calling a flow is not a bulkified process, but to get you a little further, here is you code reworked:

public class AddPrimaryContactToOpportunity { 
    public static void runFlow() {

        //I admit, this is not bulkified
        for(OpportunityContactRole ocr : Trigger.new)
        {
            Flow.Interview flow = new Flow.Interview.Update_Primary_Contact_lookup(new map<String,Object> 
                                                        {'recordId' => ocr.OpportunityId});     
            flow.start();
        }
}

I would also review the governor limits for Flows and see if your business process needs to be reworked https://developer.salesforce.com/docs/atlas.en-us.212.0.salesforce_vpm_guide.meta/salesforce_vpm_guide/vpm_admin_flow_limits.htm

  • Thank you @BryanAnderson! I made some small tweaks and that works. I will read up on the governor limits for Flows. – Uninvited Guest Nov 21 '19 at 20:57
  • Thanks @UninvitedGuest, can you mark this as solved if it fixed your issue – Bryan Anderson Nov 21 '19 at 20:58
2

If anyone needs this in the future, here's the code:

Trigger

Trigger OppContactRoleTrigger on OpportunityContactRole (after insert, after update){
AddPrimaryContactToOpportunity.runFlow();
}

Class

public class AddPrimaryContactToOpportunity { 
public static void runFlow() {

    for(OpportunityContactRole ocr : (List<OpportunityContactRole>) Trigger.new){
        Flow.Interview flow = new Flow.Interview.Update_Primary_Contact_lookup(new map<String,Object> 
                                                    {'recordId' => ocr.OpportunityId});     
        flow.start();
    }
}

Test Class

@isTest
public class TestOppContactRoleTrigger {
static testMethod void insertAllRecords() {
    // Create Account
    Account acc                = new Account();      
    acc.Name                   = 'Test Acc';
    insert acc;

    // Create Contact
    Contact con                = new Contact();
    con.LastName               = 'Lasttest';
    insert con;

    // Create Opportunity
    Opportunity opp            = new Opportunity();
    opp.Name                   = 'Test Opp';
    opp.AccountId              = acc.id;
    opp.StageName              = 'Prospecting';
    opp.CloseDate              = System.today() + 5;
    insert opp;

    // Create OpportunityContactRole
    OpportunityContactRole ocr = new OpportunityContactRole();
    ocr.OpportunityId          = opp.id;
    ocr.ContactId              = con.id;
    ocr.isPrimary              = TRUE;
    insert ocr;
}

}

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.