You need to sign in to do that
Don't have an account?
Sana123
public with sharing class HelloFirst {
public static void demo1 (Integer intNum) {
List<Account> listOfAccount = new List<Account>();
for(integer i=1; i<=intNum; i++){
Account acct = new account();
acct.name ='sha'+i;
listOfAccount.add(acct);
}
insert listOfAccount;
system.debug(listOfAccount);
}
public static void demo2 (Integer numAccInsert , Map<Integer,Integer> mapOfContact){
list<Account> listOfAcc = new list<Account>();
list<Contact> listOfCon = new list<Contact>();
for(Integer i = 1; i<=numAccInsert; i++){
Account objAcc = new Account();
objAcc.name = 'Anjana' +i;
listOfAcc.add(objAcc);
}
insert listOfAcc;
system.debug(listOfAcc);
for(Integer j = 1; j< listOfAcc.size(); j++){
if(mapOfContact.containsKey(j))
{
Id accId = listOfAcc[j].id;
for(Integer k=1; k<=mapOfContact.get(j); k++)
{
Contact objCon = new Contact();
objCon.AccountId = accId ;
objCon.lastName = 'Sharma'+k;
listOfCon.add(objCon);
}
}
}
insert listOfCon;
system.debug(listOfCon);
}
public static void demo3(List<Account>listOfAccount , List<Contact>conc){
List<Contact>listOfContact = new List<Contact>();
Database.SaveResult[] srList = Database.insert(listOfAccount, false);
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
for(Contact objconc :conc){
objconc.AccountId = sr.id;
}
// system.debug('conc---'+conc);
Database.upsert(conc, false);
}
else
{
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Contact fields that affected this error: ' + err.getFields());
Error_Log__c a1 = new Error_Log__c(Parent_Record_Name__c = '' , Parent_Record_Id__c = '' ,Error_Details__c = err.getStatusCode() + ': ' + err.getMessage());
database.upsert(a1 , false);
}
}
}
system.debug(listOfContact);
}
}
how can i write test class for dml?
public with sharing class HelloFirst {
public static void demo1 (Integer intNum) {
List<Account> listOfAccount = new List<Account>();
for(integer i=1; i<=intNum; i++){
Account acct = new account();
acct.name ='sha'+i;
listOfAccount.add(acct);
}
insert listOfAccount;
system.debug(listOfAccount);
}
public static void demo2 (Integer numAccInsert , Map<Integer,Integer> mapOfContact){
list<Account> listOfAcc = new list<Account>();
list<Contact> listOfCon = new list<Contact>();
for(Integer i = 1; i<=numAccInsert; i++){
Account objAcc = new Account();
objAcc.name = 'Anjana' +i;
listOfAcc.add(objAcc);
}
insert listOfAcc;
system.debug(listOfAcc);
for(Integer j = 1; j< listOfAcc.size(); j++){
if(mapOfContact.containsKey(j))
{
Id accId = listOfAcc[j].id;
for(Integer k=1; k<=mapOfContact.get(j); k++)
{
Contact objCon = new Contact();
objCon.AccountId = accId ;
objCon.lastName = 'Sharma'+k;
listOfCon.add(objCon);
}
}
}
insert listOfCon;
system.debug(listOfCon);
}
public static void demo3(List<Account>listOfAccount , List<Contact>conc){
List<Contact>listOfContact = new List<Contact>();
Database.SaveResult[] srList = Database.insert(listOfAccount, false);
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
for(Contact objconc :conc){
objconc.AccountId = sr.id;
}
// system.debug('conc---'+conc);
Database.upsert(conc, false);
}
else
{
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Contact fields that affected this error: ' + err.getFields());
Error_Log__c a1 = new Error_Log__c(Parent_Record_Name__c = '' , Parent_Record_Id__c = '' ,Error_Details__c = err.getStatusCode() + ': ' + err.getMessage());
database.upsert(a1 , false);
}
}
}
system.debug(listOfContact);
}
}
Try Below Test Class Please Mark It As Best Answer If It Helps
Thank You!
All Answers
Try Below Test Class Please Mark It As Best Answer If It Helps
Thank You!