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.
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.
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
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???????
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:
Test Class:
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
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:
Test Class:
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
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
I also need positive test class(When account is created without any contacts)
Negative class(When account is created with contacts)
Null values
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???????