+ Start a Discussion
kavya M 28kavya M 28 

Write a batch class to create contact for every account which are not having contacts along with test class having 100% code coverage

Best Answer chosen by kavya M 28
Khan AnasKhan Anas (Salesforce Developers) 
Hi Kavya,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Batch Apex:
global class Batch_AddConToAcc implements Database.Batchable <sObject> {
    
    List<contact> lstCon = new List<Contact>();
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'SELECT Id, Name FROM Account WHERE Id NOT IN(SELECT AccountId FROM Contact)';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc,List<Account> batch) {
        for (Account a : batch) {
            Contact c =  new Contact();
            c.LastName = a.Name;
            c.AccountId = a.Id;
            lstCon.add(c);
        }
        INSERT lstCon;
    }
    
    global void finish(Database.BatchableContext bc) {
        //Do Nothing.
    }
}

Test Class:
@isTest
public class Test_Batch_AddConToAcc {
    
    static testMethod void testMethod1() {
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <200;i++) {
            Account acc = new Account();
            acc.Name ='Name'+i;
            lstAccount.add(acc);
        }
        
        INSERT lstAccount;
        
        Test.startTest();
        
        Batch_AddConToAcc obj = new Batch_AddConToAcc();
        DataBase.executeBatch(obj); 
        
        Test.stopTest();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Kavya,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Batch Apex:
global class Batch_AddConToAcc implements Database.Batchable <sObject> {
    
    List<contact> lstCon = new List<Contact>();
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'SELECT Id, Name FROM Account WHERE Id NOT IN(SELECT AccountId FROM Contact)';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc,List<Account> batch) {
        for (Account a : batch) {
            Contact c =  new Contact();
            c.LastName = a.Name;
            c.AccountId = a.Id;
            lstCon.add(c);
        }
        INSERT lstCon;
    }
    
    global void finish(Database.BatchableContext bc) {
        //Do Nothing.
    }
}

Test Class:
@isTest
public class Test_Batch_AddConToAcc {
    
    static testMethod void testMethod1() {
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <200;i++) {
            Account acc = new Account();
            acc.Name ='Name'+i;
            lstAccount.add(acc);
        }
        
        INSERT lstAccount;
        
        Test.startTest();
        
        Batch_AddConToAcc obj = new Batch_AddConToAcc();
        DataBase.executeBatch(obj); 
        
        Test.stopTest();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
kavya M 28kavya M 28
Khan,
Could you please write comments for the test class?
I am new to test classes
It will help me in understanding if you write the test classes.

Thanks,
Kavya Sree Mareedu
kavya M 28kavya M 28
I want system.assert statements for the test class.
I also need positive test class(When account is created without any contacts)
Negative class(When account is created with contacts)
Null values
kavya M 28kavya M 28
Please help me in guiding.
Dnyanesh SableDnyanesh Sable
I have executed batch apex start() method is working fine but execute method is not working. following is my code
 
public class accountBatch implements database.Batchable <sObject>{
     
    public database.QueryLocator start(database.BatchableContext bc ){
        String query = 'SELECT Id, Name FROM Account';
        System.debug('query::>> '+query);
        return database.getQueryLocator(query);
    }
    
    public void execute(database.BatchableContext bc, list<Account> scope){
        System.debug('executed');
        for(Account a : scope){
            System.debug('Account ::>> '+a.Name);    
        } 
    }
    
    public void finish(database.BatchableContext bc){
        
    } 
}

 
Prakhyat sapraPrakhyat sapra
Hiii Khan Anas,
can you help me to solve this problem,
write a batch class in which the contacts of all the accounts should be only 2.if more than 2 then it bocomes 2, or if less then it becomes2???????