0

I have seen lots of people struggle to insert opportunity contact roles as they cannot be inserted using process builder (not a first class object). There are some work around using flows but these do not work on professional edition and are time consuming to make.

My code should take variables from Process builder and then search for an existing opportunity contact role, if it does not exist insert one. It's quite simple but I cannot get it to compile/save! Grrr.

The errors are: Variable does not exist: ContactId Variable does not exist: OpportunityId Variable does not exist: IsPrimary Variable does not exist: Role DML requires SObject or SObject list type: OpportunityContactRole

I have tried rewriting it different ways with no luck!

global class OpportunityContactRoleNew {

global class ActionRequest {
    @InvocableVariable(required=true)
    global ID ContactId;
    @InvocableVariable(required=true)
    global Id OpportunityId;
    @InvocableVariable(required=true)
    global boolean IsPrimary;
    @InvocableVariable(required=true)
    global string Role;
}

@InvocableMethod(label='Insert Opportunity Contact Role' description='Add an opportunity contact role from process builder')
global static void invokeService(List<ActionRequest> request){  

            //Look for existing opporunity contact role
            Id ContactID = [SELECT Id, ContactId FROM OpportunityContactRole WHERE ContactId = :request[0].ContactId].Id;

            //if doesn't exist try and add it 
            If(ContactID == null ){
                try{
                   OpportunityContactRole OCR =  new OpportunityContactRole();
                   OCR.ContactId = request[0].ContactId;
                   OCR.OpportunityId =request[0].OpportunityId;
                   OCR.IsPrimary =request[0].IsPrimary;
                   OCR.Role =request[0].Role;
                   insert OCR; 
                }catch(exception e){}
            }        



}






}
0

I solved it. I don't understand the solution so anyone who does let me know:

Added "Schema" before OpportunityContactRole

  Schema.OpportunityContactRole OCR = new Schema.OpportunityContactRole(ContactId = request[0].ContactId,OpportunityId =request[0].OpportunityId,     IsPrimary =request[0].IsPrimary,    Role =request[0].Role);
                   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.