You need to sign in to do that
Don't have an account?
ramesh
how to write class in test class my code is........
public class acc_contact_num_rollup
{
public static Void InsertMethod(list<Contact> lstCon ){
List<Account> accList=new List<Account>();
Set<Id> setAccIds = new Set<Id>();
for(Contact con : lstCon){
if(con.AccountId != null){
setAccIds.add(con.AccountId);
}
}
for(Account acc :[Select id,Number_of_Contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_Contact__c = acc.contacts.size();
acclist.add(acc);
}
if(!acclist.isempty()){
update accList;
}
}
public static Void UpdateMethod(list<Contact> lstCon,map<Id,Contact>oldmap ){
List<Account> accList=new List<Account>();
Set<Id> setAccIds = new Set<Id>();
for(Contact con : lstCon){
if(con.AccountId != null){
setAccIds.add(con.AccountId);
setAccIds.add(oldMap.get(con.Id).AccountId);
}
}
for(Account acc :[Select id,Number_of_Contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_Contact__c = acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
public static Void deleteMethod(list<Contact> lstCon){
List<Account> accList=new List<Account>();
Set<Id> setAccIds = new Set<Id>();
for(Contact con : lstCon){
if(con.AccountId != null){
setAccIds.add(con.AccountId);
}
}
for(Account acc :[Select id,Number_of_Contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_Contact__c = acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
{
public static Void InsertMethod(list<Contact> lstCon ){
List<Account> accList=new List<Account>();
Set<Id> setAccIds = new Set<Id>();
for(Contact con : lstCon){
if(con.AccountId != null){
setAccIds.add(con.AccountId);
}
}
for(Account acc :[Select id,Number_of_Contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_Contact__c = acc.contacts.size();
acclist.add(acc);
}
if(!acclist.isempty()){
update accList;
}
}
public static Void UpdateMethod(list<Contact> lstCon,map<Id,Contact>oldmap ){
List<Account> accList=new List<Account>();
Set<Id> setAccIds = new Set<Id>();
for(Contact con : lstCon){
if(con.AccountId != null){
setAccIds.add(con.AccountId);
setAccIds.add(oldMap.get(con.Id).AccountId);
}
}
for(Account acc :[Select id,Number_of_Contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_Contact__c = acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
public static Void deleteMethod(list<Contact> lstCon){
List<Account> accList=new List<Account>();
Set<Id> setAccIds = new Set<Id>();
for(Contact con : lstCon){
if(con.AccountId != null){
setAccIds.add(con.AccountId);
}
}
for(Account acc :[Select id,Number_of_Contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_Contact__c = acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
Can you try the below test class which gives you 100% coverage for the class and even for the trigger using this class as well. We are creating Account and Contact checking the results based on our code .
Let me know if you face any issue in the code
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
Can you try the below test class which gives you 100% coverage for the class and even for the trigger using this class as well. We are creating Account and Contact checking the results based on our code .
Let me know if you face any issue in the code
If this solution helps, Please mark it as best answer.
Thanks,
Try Below Code Coverage 100% Please Mark It As Best Answer If It Helps
Thank You!