0% found this document useful (0 votes)
19 views6 pages

New 2

The document contains test classes that test different account and contact manipulation methods. Specifically, it: 1) Creates test account and contact records and inserts them. 2) Defines methods to make mandatory fields on account, set default values, link accounts to contacts, add errors, and restrict the number of child contacts based on an account field. 3) Tests the above methods by querying the inserted records and passing them to the methods.

Uploaded by

Chinna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

New 2

The document contains test classes that test different account and contact manipulation methods. Specifically, it: 1) Creates test account and contact records and inserts them. 2) Defines methods to make mandatory fields on account, set default values, link accounts to contacts, add errors, and restrict the number of child contacts based on an account field. 3) Tests the above methods by querying the inserted records and passing them to the methods.

Uploaded by

Chinna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

test classes

public static void makemandataryfieldstatus(list<account> acc1 ){


system.debug('acc1---->'+acc1);
for(account acc2 :acc1){
if(acc2.status__c ==null || acc2.status__c == ''){
acc2.adderror('please select any value');
}
}
}
public static void defaultdescription(list<account> acc1){
for(account acc2 :acc1)
{
if(acc2.Description==null){
acc2.Description='please enter any description';

}
}
}
public static void defaultstatus(list<account> acc){
for(account acc1 : acc){
if(acc1.status__c ==null){
acc1.status__c ='created';
}
}

}
public static void changedescrption(list<account> acc1){
for (account acc2 : acc1){
if (acc2.status__c != null && acc2.status__c =='created'){
acc2.Description='the account is created';

}else if(acc2.status__c != null && acc2.status__c =='closed'){


acc2.Description='the accounnt is closed';
}

}
}

@isTest
public class test22 {
@testSetUp
public static void method1(){
list <Account> accc = new list <Account>();
Account acc=new Account();
acc.name= 'chinnareddy';
acc.status__c = null;
//acc.adderror('please select any value');
accc.add(acc) ;
Account acc1 =new Account();
acc1.name= 'chinna1';
acc1.status__c ='created' ;
accc.add(acc1) ;
Account acc2 =new Account();
acc2.name= 'chinna2';
acc2.status__c ='Closed' ;
accc.add(acc2) ;
Account acc4 =new Account();
acc4.name= 'chinna3';
acc4.Description=null;
acc4.Description='please enter any description';
accc.add(acc4) ;
Account acc5 =new Account();
acc5.name= 'chinna5';
acc5.city__c='Anantapur';
accc.add(acc5) ;
Account acc6 =new Account();
acc6.name= 'chinna5';
acc6.AnnualRevenue=1;
accc.add(acc6) ;
insert accc;

}
@isTest
public static void method2(){
List<Account> accList=[select Id,Name,status__c, Description,phone from
Account];
list<contact> conlist=[select AccountId from contact ];

//accountexample1.accounttocontact(accList);
//accountexample1.accounttocontactupdated(accList);
//accountexample1.througherrorinaccount(accList);

accountexample1.makemandataryfieldstatus(accList);
accountexample1.defaultdescription(accList);
accountexample1.defaultstatus(accList);
accountexample1.changedescrption(accList);

}
}

the above four are executed

public static void accounttocontact(list<account> acc1){

list <contact> conlist = new list <contact> ();

for (account acc2 : acc1){


contact con = new contact();
con.LastName =acc2.Name;
con.FirstName=acc2.Name;
con.Phone=acc2.Phone;
con.AccountId=acc2.Id;
conlist.add(con);

}
if( !conlist.isempty()){
insert conlist;

}
}
public static void accounttocontactupdated(list<account> acc1){
list <contact> conlist = new list <contact> ();
list <id> idlist = new list <id> ();
for (account acc2 : acc1){
if(acc2.status__c=='Created'){
contact con = new contact();
con.AccountId=acc2.Id;
con.LastName=acc2.Name;
conlist.add(con);
}else if(acc2.status__c=='Closed'){
contact con = new contact();
con.AccountId=acc2.Id;
con.LastName=acc2.Name;
conlist.add(con);
}
if( !conlis

}t.isempty()){
insert conlist;

}
}
public static void restrictchildrecord(list <account> acc1){
integer noofrecords =0;
list<contact> conlist = new list<contact> ();
for (account acc2:acc1){
if (acc2.city__c =='Anantapur'){
noofrecords =(integer)acc2.no_of_records__c;
for(integer i=1; i<= noofrecords; i++){
contact con = new contact();
con.Lastname = acc2.Name;
con.MobilePhone = acc2.Phone;
con.AccountId = acc2.Id;
conList.add(con);
}

}
if(!conlist.isempty()){
insert conlist;
}
}

@testSetUp
public static void method1(){
list <Account> accc = new list <Account>();
Account acc=new Account();
acc.name= 'chinnareddy';
acc.status__c = null;
//acc.adderror('please select any value');
accc.add(acc) ;
Account acc1 =new Account();
acc1.name= 'chinna1';
acc1.status__c ='created' ;
accc.add(acc1) ;
Account acc2 =new Account();
acc2.name= 'chinna2';
acc2.status__c ='Closed' ;
accc.add(acc2) ;
Account acc4 =new Account();
acc4.name= 'chinna3';
acc4.Description=null;
acc4.Description='please enter any description';
accc.add(acc4) ;
Account acc5 =new Account();
acc5.name= 'chinna5';
acc5.city__c='Anantapur';
accc.add(acc5) ;
Account acc6 =new Account();
acc6.name= 'chinna5';
acc6.AnnualRevenue=1;
accc.add(acc6) ;
insert accc;

}
@isTest
public static void method2(){
List<Account> accList=[select Id,Name,status__c, Description,phone from
Account];
list<contact> conlist=[select AccountId from contact ];

accountexample1.accounttocontact(accList);
accountexample1.accounttocontactupdated(accList);

// accountexample1.makemandataryfieldstatus(accList);
//accountexample1.defaultdescription(accList);
//accountexample1.defaultstatus(accList);
//accountexample1.changedescrption(accList);

example 7
public class example3 {
public static void througherrorinaccount(list <account> acc1){
list<contact> conlist = new list<contact> ();
for (account acc2:acc1){
if (acc2.AnnualRevenue>1000){
acc2.adderror('please give lessthan 1000 in annual revenue');

}else if(acc2.AnnualRevenue<1000){
contact con = new contact();
con.AccountId=acc2.Id;
con.LastName=acc2.Name;
con.Phone=acc2.Phone;
conlist.add(con);
}
if(!conlist.isempty()){
insert conlist;
}
}

@isTest
public class test3 {
@testSetUp
public static void method1(){
list <Account> accc = new list <Account>();
Account acc=new Account();
acc.name= 'chinnareddy';
acc.AnnualRevenue=10;
accc.add(acc);
Account acc1=new Account();
acc1.name= 'chinnareddy';
acc1.AnnualRevenue=10000;
accc.add(acc1);
insert accc;
}
@isTest
public static void method2(){
list<Account> acclist= [select Id,name,Phone, AnnualRevenue from Account ];
example3.througherrorinaccount(acclist);
}
}

example 4

public class example4 {


public static void restrictchildrecord(list <account> acc1){
integer noofrecords =0;
list<contact> conlist = new list<contact> ();
for (account acc2:acc1){
if (acc2.city__c =='Anantapur'){
noofrecords =(integer)acc2.no_of_records__c;
for(integer i=1; i<= noofrecords; i++){
contact con = new contact();
con.Lastname = acc2.Name+i;
con.MobilePhone = acc2.Phone;
con.AccountId = acc2.Id;
conList.add(con);
}

}
if(!conlist.isempty()){
insert conlist;
}
}
}
}

@isTest
public class test4 {
@testSetUp
public static void method1(){
Account acc=new Account();
acc.Name= 'chinnareddy';
acc.city__c='Anantapur';
acc.no_of_records__c=3;
insert acc;
}
@isTest
public static void method2(){
list<Account> acclist= [select Id, Name,city__c,no_of_records__c, Phone
from Account];
example4.restrictchildrecord(acclist);
//for(integer i=0;i<6;i++){
Account acc = new Account();
// acc.Name = 'chinnareddy'+i;
//}
}
}

You might also like