2

Actually 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 .

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) {

}
}
3
  • You are aware that you are always inserting here so if the same d. First_Name__c, d.Name and d.Primary_Account_ID__c come up in more than one Data_Feeds__c you will have more than one Contact object containing the same data? Perhaps you can guarantee that will never happen.
    – Keith C
    Commented May 15, 2014 at 14:46
  • Create a Map with a FirstName LastName combination for the Contacts. Make another map by AccountId. Use the combination of the 2 after you insert contacts in order to create the associated roles.
    – dphil
    Commented May 16, 2014 at 14:19
  • dphil-thanks for reply can you please give example code.
    – user7292
    Commented May 16, 2014 at 14:31

1 Answer 1

2

First off, welcome to the forums!

You can just cycle through the contacts you just created and use the data you've populated on those records...

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;

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

    for (Contact cont : conts) {
        AccountContactRole role = new AccountContactRole();
        role.AccountId = cont.AccountId;
        role.ContactId = cont.Id;
        // any other fields you want to specify
        roles.add(role);
    }

    insert roles;

}
global void finish(Database.BatchableContext BC) {

}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .