You need to sign in to do that
Don't have an account?
Sana123
how to write test class for this code?
public static void countContactDetails(List<Contact> listContacts, Map<Id,Contact> mapOldContacts){
Set<Id> setAccountId = new Set<Id>();
Map<Id,Account> mapOfAccount = new Map<Id,Account>();
if( mapOldContacts == null || mapOldContacts.isEmpty() ) {
for( Contact objCont : listContacts ) {
if( objCont.AccountId != null ) {
setAccountId.add( objCont.AccountId );
}
}
}
else if( mapOldContacts != null && !mapOldContacts.isEmpty()) {
for( Contact objCont : listContacts ) {
if( objCont.AccountId != null
&& mapOldContacts.get( objCont.Id ).AccountId != objCont.AccountId ) {
setAccountId.add( objCont.AccountId );
if( mapOldContacts.get( objCont.Id ).AccountId != null ) {
setAccountId.add( mapOldContacts.get( objCont.Id ).AccountId );
}
}
else if( objCont.AccountId == null && mapOldContacts.get( objCont.Id ).AccountId != objCont.AccountId ) {
setAccountId.add( mapOldContacts.get( objCont.Id ).AccountId );
}
}
}
if( !setAccountId.isEmpty() ) {
List<Account> listAccount = new List<Account>();
for (AggregateResult ar : [SELECT AccountId AcctId, Count(id) ContactCount
FROM Contact
WHERE AccountId in: setAccountId
GROUP BY AccountId]){
Account a = new Account();
a.Id = (Id) ar.get('AcctId');
a.Total_Contacts__c = (Integer) ar.get('ContactCount');
listAccount.add(a);
}
if( !listAccount.isEmpty() ) {
update listAccount;
}
}
if( !setAccountId.isEmpty() ) {
List<Account> listAccount1 = new List<Account>();
List<AggregateResult> results = [Select AccountId ,Count(Id) ContactCount, Contact_Status__c str from Contact WHERE AccountId in: setAccountId GROUP BY AccountId, Contact_Status__c];
Integer activecount = 0;
Integer inactivecount = 0;
for (AggregateResult ar : results) {
if(ar.get('str')=='Active'){
activecount += (Integer)ar.get('ContactCount');
}
if(ar.get('str')=='Inactive'){
inactivecount = (Integer)ar.get('ContactCount');
}
//a.Total_Contacts__c = (Integer) ar.get('ContactCount');
// mapOfAccount.put((ID)ar.get('AccountId') , new Account(Id = (Id)ar.get('AccountId'),Total_Active_Contacts__c = activecount ,Total_Inactive_Contacts__c = inactivecount));
Account a1 = new Account();
a1.Id = (Id) ar.get('AccountId');
a1.Total_Active_Contacts__c = activecount;
a1.Total_Inactive_Contacts__c = inactivecount;
listAccount1.add(a1);
}
system.debug('Mapofaccount=='+ listAccount1);
}
}
Set<Id> setAccountId = new Set<Id>();
Map<Id,Account> mapOfAccount = new Map<Id,Account>();
if( mapOldContacts == null || mapOldContacts.isEmpty() ) {
for( Contact objCont : listContacts ) {
if( objCont.AccountId != null ) {
setAccountId.add( objCont.AccountId );
}
}
}
else if( mapOldContacts != null && !mapOldContacts.isEmpty()) {
for( Contact objCont : listContacts ) {
if( objCont.AccountId != null
&& mapOldContacts.get( objCont.Id ).AccountId != objCont.AccountId ) {
setAccountId.add( objCont.AccountId );
if( mapOldContacts.get( objCont.Id ).AccountId != null ) {
setAccountId.add( mapOldContacts.get( objCont.Id ).AccountId );
}
}
else if( objCont.AccountId == null && mapOldContacts.get( objCont.Id ).AccountId != objCont.AccountId ) {
setAccountId.add( mapOldContacts.get( objCont.Id ).AccountId );
}
}
}
if( !setAccountId.isEmpty() ) {
List<Account> listAccount = new List<Account>();
for (AggregateResult ar : [SELECT AccountId AcctId, Count(id) ContactCount
FROM Contact
WHERE AccountId in: setAccountId
GROUP BY AccountId]){
Account a = new Account();
a.Id = (Id) ar.get('AcctId');
a.Total_Contacts__c = (Integer) ar.get('ContactCount');
listAccount.add(a);
}
if( !listAccount.isEmpty() ) {
update listAccount;
}
}
if( !setAccountId.isEmpty() ) {
List<Account> listAccount1 = new List<Account>();
List<AggregateResult> results = [Select AccountId ,Count(Id) ContactCount, Contact_Status__c str from Contact WHERE AccountId in: setAccountId GROUP BY AccountId, Contact_Status__c];
Integer activecount = 0;
Integer inactivecount = 0;
for (AggregateResult ar : results) {
if(ar.get('str')=='Active'){
activecount += (Integer)ar.get('ContactCount');
}
if(ar.get('str')=='Inactive'){
inactivecount = (Integer)ar.get('ContactCount');
}
//a.Total_Contacts__c = (Integer) ar.get('ContactCount');
// mapOfAccount.put((ID)ar.get('AccountId') , new Account(Id = (Id)ar.get('AccountId'),Total_Active_Contacts__c = activecount ,Total_Inactive_Contacts__c = inactivecount));
Account a1 = new Account();
a1.Id = (Id) ar.get('AccountId');
a1.Total_Active_Contacts__c = activecount;
a1.Total_Inactive_Contacts__c = inactivecount;
listAccount1.add(a1);
}
system.debug('Mapofaccount=='+ listAccount1);
}
}
Try Below Test Class Please Mark It As Best Answer If it Helps
Thank You!
Can yu plese help me
Hi,
Please do some needful changes according to your requirement. I think this below code would cover 75%
Please mark it as the Best Answer if it helps you
Thank You
Please help me...
Thank you in advance