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