You need to sign in to do that
Don't have an account?
jay209
How to Create a child Record by using Batch
I have an objects like Account and Family__c, In family I have a Lookup field like Main_account__Cand family_account__c and in Account object i have fields like Application_Number__c text field and Priority_Number__c is also a text field which contains Comma separated values. Now I want to Create a Record in Family, If they are 5 Account records and out of five 2 records have same Application_Number__c then i want to create a Family Record. and Main_account__C value will be account1 and the family_account__c will be the matched Account(account2).and criteria will be like if Application_Number__c field value matches with any other Account Record Application_Number__c value then i need to Create a new record in Family__c, like that same for Priority_Number__c also
Thanks.
global class AccountFamilygroupBatch implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator('SELECT id,Name,Application_Number__c,Priority_Number__c FROM Account WHERE Application_Number__c !=Null OR Priority_Number__c !=Null'); } global void execute(Database.BatchableContext BC, List<Account> scope) { List<Family__c> family = New List<Family__c>(); final String appNo = scope[0].Application_Number__c; final String priNo = scope[0].Priority_Number__c; for(Account pat : scope) { if(pat.Application_Number__c == appNo || pat.Priority_Number__c ==priNo) { Family__c fly = New Family__c(); fly.Name=pat.Name; fly.Main_account__c=pat.id; fly.Family_account__c=pat.id; family.add(fly); } } Insert family; system.debug('-->>Inserted Family: '+family); } global void finish(Database.BatchableContext BC) { // will be called in end when batch finish. } }Now Record is creating but like expected. please help me to resolve this.
Thanks.
Please try the below code. I tried it in my developer org and is working fine.
global class AccountFamilygroupBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
// collect the batches of records or objects to be passed to execute
String query = 'SELECT Id, Name, Application_Number__c, Priority_Number__c FROM Account where Application_Number__c!=null or Priority_Number__c != null';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> accList) {
// process each batch of records
Map<String,List<Account>> appVal= new Map<String,List<Account>>();
Map<String,List<Account>> prVal=new Map<String,List<Account>>();
List<Family__c> famList= new List<Family__c>();
for(Account acc : accList)
{
if(appVal.containsKey(acc.Application_Number__c)) {
List<Account> accids = appVal.get(acc.Application_Number__c);
accids.add(acc);
appVal.put(acc.Application_Number__c, accids);
} else {
appVal.put(acc.Application_Number__c, new List<Account> { acc });
}
if(prVal.containsKey(acc.Priority_Number__c)) {
List<Account> accids = prVal.get(acc.Priority_Number__c);
accids.add(acc);
prVal.put(acc.Priority_Number__c, accids);
} else {
prVal.put(acc.Priority_Number__c, new List<Account> { acc });
}
}
System.debug('appVal'+appVal);
System.debug('prVal'+prVal);
for(String a:appVal.keySet()){
if(appVal.get(a).size()>1){
System.debug('appVal.get(a)'+appVal.get(a));
Family__c fam=new Family__c();
fam.Name=(appVal.get(a))[0].Name;
fam.Main_account__c=appVal.get(a)[0].id;
fam.family_account__c=appVal.get(a)[1].id;
}
}
for(String a:appVal.keySet()){
if(appVal.get(a).size()>1){
System.debug('appVal.get(a)'+appVal.get(a));
Family__c fam=new Family__c();
fam.Name=(appVal.get(a))[0].Name;
fam.Main_account__c=appVal.get(a)[0].id;
fam.family_account__c=appVal.get(a)[1].id;
famList.add(fam);
}
}
for(String p:prVal.keySet()){
// appList.add(appVal.get(a));
if(prVal.get(p).size()>1){
System.debug('prVal.get(p)'+prVal.get(p));
Family__c fam=new Family__c();
fam.Name=(prVal.get(p))[0].Name;
fam.Main_account__c=prVal.get(p)[0].id;
fam.family_account__c=prVal.get(p)[1].id;
famList.add(fam);
}
}
system.debug('famList'+famList);
try {
insert famList;
} catch(Exception e) {
System.debug(e);
}
}
global void finish(Database.BatchableContext BC) {
// execute any post-processing operations
}
}
Thanks,
Sowmya.