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
udayar_jayamudayar_jayam 

How to Insert Account Contact roles?

Hi All
we have a requirement to perform one contact with multiple Accounts scenario. To perform this logic I have a custom object called "Data Feed".We upload the data in that Data feed object periodically then what we need to do is we need to run the batch apex instantly to pass the Primary Id to Account Lookup under Contact object and at the same time we have to create one entry in Contact Role object by passing the Account ID. I am able to create "Contact" records using Batch Apex class.Its working fine .How can create a entry in Contact role as well at the same time.Please advice me on the same.

global class ScheduleContact implements Database.Batchable<SObject> {
global Database.queryLocator start(Database.BatchableContext ctx) {

    String query = 'Select Name,Account_ID__c,Primary_Account_ID__c, Deleted__c, First_Name__c, Internal_User_ID__c,Language_Preference__c,Language_Preference_Code__c,Phone__c,Primary_Contact__c,Speaks_English__c from Data_Feeds__c';


    return Database.getQueryLocator(query);

}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
    List<Data_Feeds__c> DFeedList = (List<Data_Feeds__c>)scope;


    List<Contact> conts = new List<Contact>();

    for (Data_Feeds__c d : DFeedList) {
        Contact cont = new Contact();
        cont.FirstName = d.First_Name__c;
        cont.LastName = d.Name;
        cont.AccountId= d.Primary_Account_ID__c;
        conts.add(cont);
    }

    insert conts;
}
global void finish(Database.BatchableContext BC) {

}
}
nbknbk
Can you use following code after contact insertion (insert conts statment)

List<ContactRole> contRoles= new List<ContactRole>();
List<Contact> lstcontact =[select id,lastname,accountid from contact];
for (Contact con: lstcontact)
{
    ContactRole cr = new ContactRole();
    cr.contactid = con.id;
    controles.add(cr);
}
insert controles;
    
** if you want to compare account id and insert contact roles you need to use the map to store the data and map accordingly (mapaccount.containskey(accountid))
udayar_jayamudayar_jayam
@nbk thanks for reply i have add your code and run in developer console and i got an error,line 34 is insert controles
EXCEPTION_THROWN [34]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [AccountId]: [AccountId].i want to compare accountid and insert contact roles code please advice me

nbknbk
I gave you sample block of code to execute. currently the account id field is not included in script.
check the api names and insert accordingly. 

for (Contact con: lstcontact)
{
    ContactRole cr = new ContactRole();
    cr.contactid = con.id;
    cr.accountid = con.accountid;
    controles.add(cr);
}

udayar_jayamudayar_jayam
@nbk thanks for your help  modify your code but im getting this error
First error: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ContactId]: [ContactId].AccContRole__c is Contact lookup,dont mistake me please hlep me out this.

cont.AccContRole__c=d.Account_ID__c;

List<AccountContactRole> contRoles= new List<AccountContactRole>();
//List<Contact> lstcontact =[select id,lastname,AccContRole__c,accountid from contact];
List<Contact> lstcontact =[select id,lastname,AccContRole__r.id,accountid from contact];

for (Contact con: lstcontact)
{
   AccountContactRole cr = new AccountContactRole();
    cr.accountid =con.accountid ;
    cr.contactid = con.AccContRole__r.id;
   //cr.contactid = con.AccContRole__c;
    //cr.contactid = con.id;
    cr.role='Other';
    controles.add(cr);

when i use cr.contactid = con.id; its woking fine create contact and account contact role but when i run developer console in 2nd time im getting this error
First error: Insert failed. First exception on row 2; first error: DUPLICATE_VALUE, Contact has already been added in that Contact Role: [ContactId, Role] .how can i insert more than one.