function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael MinnekeerMichael Minnekeer 

Creating Opportunity Contact Role declaritevly with Person Accounts

Hi All

I want to be able to create a click not code method of creating an Opportunity Contact Role for all opportunities when they create which is required in order to sync them to Pardot. We use person accounts however so they do not auto create and I have hit a couple of limitations while trying to solve this task.

First is you cannot create a contact role within the process builder but you can with a flow and reference it in the process builder (credit: https://judisohn.com/2015/04/06/using-salesforce-process-builder-flow-with-opportunity-contact-roles/).

Second however that stops this working for me is that no Account.PersonContact fields can be referenced within the Process builder from an object that is not Account to pass the ID as a variable for the flow. https://help.salesforce.com/HTViewSolution?id=000232840&language=en_US

So I thought the solution would be to create a custom contact lookup field and put the PersonContactID and populate it on account create so that I could reference it in future from process builder if necessary. I am still running into the issues on the opportunity process that it is passing a null value from the custom lookup field I have created? Relvant error report line: varContactID = {!myVariable_current.Account.PersonContactID__c} (null)

Is there really no way of doing this with out code? If not it's a another dissapointing instance where Person Accounts fall short within Salesforce.
Michael MinnekeerMichael Minnekeer
I just ended up writing code for this
 
public static void CreateContactRole(Opportunity[] opps){
        List<OpportunityContactRole> newcrolelist = new List<OpportunityContactRole>();
        Set<ID> accountIds = new Set<ID>();
        for(Opportunity opp : opps){
            accountIds.add(opp.AccountId);
        }
        
        //need to query the PersonContactId
        list<Opportunity> opplist = new list<Opportunity>();
        opplist = [select Id, AccountId, Account.PersonContactId from Opportunity where Id in: opps 
                   AND Account.isPersonAccount = true];
        
        for(Opportunity opp : opplist){
            OpportunityContactRole oppcrole = new OpportunityContactRole();
            oppcrole.OpportunityId = opp.Id;
            oppcrole.ContactId = opp.Account.PersonContactId;
            newcrolelist.add(oppcrole);
        }
        insert newcrolelist;
    }


 
Ioannis XanthopoulosIoannis Xanthopoulos
shouldnt you also create oppcrole.Role ?