I am writing a batch, which essentially iterates over account records. following steps.
- iterate over account based on specific conditions
- if the account has a linked account to it(lookup to account), then fetch the contacts details(phone, email) of that account and send to web service.
- Also, send the account Number and Name of the 1st account(step 1). I am getting the records in debug but when fetching it I get errors. Pls suggest
logic: -
global class AccountCalloutsSOAPBatch implements Database.Batchable<sObject>, Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id, Owner.Email, Name, AccountNumber,Partner_Name__c FROM Account WHERE Account_for_syncing__c = true and Partner_Name__c!= null';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> accList){
set<Id> accId = new Set<Id>();
List<Contact> contactList = new List<Contact>();
Map<Id, Contact> merchCodeVsProductMap = new Map<Id, Contact>();
for(Account acc : accList){
accId.add(acc.Partner_Name__c);
}
system.debug(accId);
contactList =[select Id,Email,AccountId, Phone, FirstName, LastName from Contact where Active__c= true and Primary_Contact__c=true and AccountId IN:accId ORDER BY LastModifiedDate DESC LIMIT 1];
system.debug(contactList);
for( Contact productIterrator : contactList ){
if(!merchCodeVsProductMap.containsKey(productIterrator.AccountId)) {
merchCodeVsProductMap.put(productIterrator.AccountId, productIterrator);
}
}
system.debug(contactList);
system.debug(merchCodeVsProductMap);
for(Account acc: accList){
//need suggestion here
//1. how to get account details from step 1
//2. how to fetch the contacts of linked account step 2
system.debug(merchCodeVsProductMap.values());
system.debug(merchCodeVsProductMap);
system.debug(acc);
//system.debug(acc.contacts[0].Email);
//system.debug(merchCodeVsProductMap.get(acc.Partner_Name__r.contacts[0].Email));// **getting error while fetching the contact info and account info**
}
}
global void finish(Database.BatchableContext BC){
}
}
{}
button or wrap each code block in triple backticks (```).