+ Start a Discussion

How to create a AccountContactRole using trigger ?

Hi All 

I am newbie to Salesforce. I was wondering if anyone can help me ?

How to create a AccountContactRole using trigger ? (When Account and its corresponding Contact is created that contact should become AccountContactRole) 

Your help is much appreciated 
NagendraNagendra (Salesforce Developers) 
Hi Ram,

Please find the sample code to insert AccountContactRole. Keep in mind that accountId and Contact Id are mandatory fields to create accountcontactrole.
Account accObj = new Account(name = 'TestAccount');
insert accObj;

Contact conObj = new Contact(lastname = 'Test Contact');
insert conObj;

AccountContactRole role = new AccountContactRole();
role.AccountId = accObj.id;
role.ContactId = conObj.Id;
// any other fields you want to specify
insert role;
Refer to the documentation for more  information Hope this helps

Mark this as solved if it's resolved.

David HalesDavid Hales
HI Ram,

Contact is the required field on AccountContactRole object.So that you have to insert the Contact object or write query for the contact object.
I have write a sample code for your problem
trigger AccountContactRoleUpdate on Account (after insert,after update) {

    List<AccountContactRole> lstinsertAccountContactRole = new List<AccountContactRole>();

    Contact ConTemp = new Contact();
    ConTemp.LastName = 'TestContact';

    insert ConTemp;

    for( Account Acc: Trigger.new )
            AccountContactRole AccConRole = New AccountContactRole();
            AccConRole.ContactId = ConTemp.Id;
            AccConRole.AccountId = Acc.Id;
            AccConRole.IsPrimary = true;
            AccConRole.Role = 'Economic Decision Maker';
    if( lstinsertAccountContactRole != null && lstinsertAccountContactRole.size() > 0 )
        insert lstinsertAccountContactRole;

Please mark it's best answer if your problem has been solved.

Thanks & Regards
David Hales(1021)
Glyn Anderson 3Glyn Anderson 3
If I understand your question, you want a trigger that creates an AccountContactRole when the Contact is created (the Account has to be created before a Contact can be created for it).

I should point out that if you enable "Allow users to relate a contact to multiple accounts" under Setup | Feature Settings | Sales | Account Settings, then Salesforce will automatically create an AccountContactRelation record for every Contact in an Account, with the "isDirect" field set true.  The advantage of AccountContactRelation is that the Roles field is a multi-select picklist and you can have a Start Date and End Date on the relationship.  Contacts will show in the "Related Contacts" related list on the Account record.

trigger CreateAccountContactRole on Contact ( after insert )
    List<AccountContactRole> acrs = new List<AccountContactRole>();
    for ( Contact contact : Trigger.new )
        (   new AccountContactRole
            (   ContactId = contact.Id
            ,   AccountId = contact.AccountId
            ,   Role = 'Insert Your Role Here'
            ,   isPrimary = true
    insert acrs;
Glyn Anderson 3Glyn Anderson 3
Sorry for my typo.  Line 6 should read, "acrs.add".
Glyn Anderson 3Glyn Anderson 3
Ram_2017,  Did any of these answers solve your problem?  If so, please mark the question as "Solved".  If not, let us know.  Thanks!