You need to sign in to do that
Don't have an account?
sumit d
how to write a test class of this batch?
public class BatchAccountUpdate implements Database.Batchable<sObject>, Database.Stateful {
public Database.QueryLocator start(Database.BatchableContext BC){
String query = ' Select Id, Name,ParentId'
+ ' from Account '
+ ' where Account.Recordtype.DeveloperName = \'IndustriesIndividual\' '
+ ' and ParentId = Null ';
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext bc, List<Account> accounts ){
Map<Id, Account> accountToUpdateMap = new Map<Id, Account>();
Map<Id, Id> mapAccountIndvAccIdToHouseholdAccId = new Map<Id, Id>();
// fetch primary contact for individual account
// and inner query to fetch house hold account contact relation
for(Contact con : [SELECT Id, AccountId,Account.ParentId,
( SELECT Id, ContactId, AccountId
FROM AccountContactRelations
WHERE Account.RecordType.DeveloperName = 'IndustriesHousehold'
)
FROM Contact
WHERE AccountId in: accounts
] ){
for( AccountContactRelation acr : con.AccountContactRelations ) {
con.Account.ParentId = acr.AccountId;
mapAccountIndvAccIdToHouseholdAccId.put( con.AccountId, acr.AccountId );
}
}
for(Account acc : [ SELECT Id, ParentId
FROM Account
WHERE Id IN: accounts
]){
if( acc.ParentId == Null && mapAccountIndvAccIdToHouseholdAccId.containsKey( acc.Id )){
Account accToUpdate = new Account( Id = acc.Id,
ParentId = mapAccountIndvAccIdToHouseholdAccId.get( acc.Id ));
accountToUpdateMap.put( accToUpdate.Id, accToUpdate );
}
}
if( accountToUpdateMap.size() > 0 ){
update accountToUpdateMap.values();
}
}
public void finish(Database.BatchableContext bc){
}
}
public Database.QueryLocator start(Database.BatchableContext BC){
String query = ' Select Id, Name,ParentId'
+ ' from Account '
+ ' where Account.Recordtype.DeveloperName = \'IndustriesIndividual\' '
+ ' and ParentId = Null ';
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext bc, List<Account> accounts ){
Map<Id, Account> accountToUpdateMap = new Map<Id, Account>();
Map<Id, Id> mapAccountIndvAccIdToHouseholdAccId = new Map<Id, Id>();
// fetch primary contact for individual account
// and inner query to fetch house hold account contact relation
for(Contact con : [SELECT Id, AccountId,Account.ParentId,
( SELECT Id, ContactId, AccountId
FROM AccountContactRelations
WHERE Account.RecordType.DeveloperName = 'IndustriesHousehold'
)
FROM Contact
WHERE AccountId in: accounts
] ){
for( AccountContactRelation acr : con.AccountContactRelations ) {
con.Account.ParentId = acr.AccountId;
mapAccountIndvAccIdToHouseholdAccId.put( con.AccountId, acr.AccountId );
}
}
for(Account acc : [ SELECT Id, ParentId
FROM Account
WHERE Id IN: accounts
]){
if( acc.ParentId == Null && mapAccountIndvAccIdToHouseholdAccId.containsKey( acc.Id )){
Account accToUpdate = new Account( Id = acc.Id,
ParentId = mapAccountIndvAccIdToHouseholdAccId.get( acc.Id ));
accountToUpdateMap.put( accToUpdate.Id, accToUpdate );
}
}
if( accountToUpdateMap.size() > 0 ){
update accountToUpdateMap.values();
}
}
public void finish(Database.BatchableContext bc){
}
}
https://trailhead.salesforce.com/en/modules/asynchronous_apex/units/async_apex_batch
under the Testing Batch Apex section.
But the basic gist is that you would prepare the test class as normal, and then execute the test class using