You need to sign in to do that
Don't have an account?
golla Anitha
Create a custom field called 'Count of Cases' on Contact record. This should contain the count of associated cases on the Contact. In the event of deletion of a Case, this count field should be updated appropriately on the Contact. Also, Test class should
Create a custom field called 'Count of Cases' on Contact record. This should contain the count of associated cases on the Contact. In the event of deletion of a Case, this count field should be updated appropriately on the Contact. Also, Test class should be written.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class contactcase {
public static void contactcasemethod(List<case> lstcase)
{
List<id> lstid=new List<id>();
for(case c:lstcase)
{
lstid.add(c.ContactId);
system.debug('lstid'+lstid);
}
integer noOfRecInCase=lstcase.size();
system.debug('noOfRecInCase'+noOfRecInCase);
//fetch contact record to update if exists
List <contact> replica=[select id,Count_of_Cases__c from contact where id in :lstid ];
system.debug('fetch contact record to update if exists'+replica);
if(replica.size()>0)
{
List<integer> casecount=[SELECT count(id) FROM case WHERE contactid in:replica];
//logic for inserting new case, update contact count
for(contact replica3:replica)
{
if(casecount[0]>0 || casecount[0]!=null)
{
replica3.Count_of_Cases__c=casecount[0]+noOfRecInCase;
system.debug('logic for inserting new case, update contact count'+replica3);
}
else{
replica3.Count_of_Cases__c=noOfRecInCase;
system.debug('logic for inserting new case, update contact count1'+replica3);
}
}
}
update replica;
}
}
/* public static void contactcasemethod1(List<case> lstcase1)
{
List<id> lstid1=new List<id>();
for(case c1:lstcase1)
{
lstid1.add(c1.ContactId);
system.debug('lstid'+lstid1);
}
integer noOfRecInCase1=lstcase1.size();
//fetch contact record to update if exists
List <contact> replic=[select id,Count_of_Cases__c from contact where id in :lstid1 ];
system.debug('fetch contact record to update if exists'+replic);
if(replic.size()>0)
{
List<case> casecount=[SELECT Origin, Subject, status, ContactId FROM case WHERE contactid in:replic];
system.debug('casecount'+casecount.size());
//logic for inserting new case, update contact count
for(contact replica3:replic)
{
if(casecount.size()>0 || casecount.size()!=null)
{
replica3.Count_of_Cases__c=casecount.size()-noOfRecInCase1;
system.debug('logic for inserting new case, update contact count'+replica3);
}
else{
replica3.Count_of_Cases__c=noOfRecInCase1;
system.debug('logic for inserting new case, update contact count1'+replica3);
}
}
}
update replic;
}
}*/
error was like
Illegal assignment from List<AggregateResult> to List<Integer>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class contactcase {
public static void contactcasemethod(List<case> lstcase)
{
List<id> lstid=new List<id>();
for(case c:lstcase)
{
lstid.add(c.ContactId);
system.debug('lstid'+lstid);
}
integer noOfRecInCase=lstcase.size();
system.debug('noOfRecInCase'+noOfRecInCase);
//fetch contact record to update if exists
List <contact> replica=[select id,Count_of_Cases__c from contact where id in :lstid ];
system.debug('fetch contact record to update if exists'+replica);
if(replica.size()>0)
{
List<integer> casecount=[SELECT count(id) FROM case WHERE contactid in:replica];
//logic for inserting new case, update contact count
for(contact replica3:replica)
{
if(casecount[0]>0 || casecount[0]!=null)
{
replica3.Count_of_Cases__c=casecount[0]+noOfRecInCase;
system.debug('logic for inserting new case, update contact count'+replica3);
}
else{
replica3.Count_of_Cases__c=noOfRecInCase;
system.debug('logic for inserting new case, update contact count1'+replica3);
}
}
}
update replica;
}
}
/* public static void contactcasemethod1(List<case> lstcase1)
{
List<id> lstid1=new List<id>();
for(case c1:lstcase1)
{
lstid1.add(c1.ContactId);
system.debug('lstid'+lstid1);
}
integer noOfRecInCase1=lstcase1.size();
//fetch contact record to update if exists
List <contact> replic=[select id,Count_of_Cases__c from contact where id in :lstid1 ];
system.debug('fetch contact record to update if exists'+replic);
if(replic.size()>0)
{
List<case> casecount=[SELECT Origin, Subject, status, ContactId FROM case WHERE contactid in:replic];
system.debug('casecount'+casecount.size());
//logic for inserting new case, update contact count
for(contact replica3:replic)
{
if(casecount.size()>0 || casecount.size()!=null)
{
replica3.Count_of_Cases__c=casecount.size()-noOfRecInCase1;
system.debug('logic for inserting new case, update contact count'+replica3);
}
else{
replica3.Count_of_Cases__c=noOfRecInCase1;
system.debug('logic for inserting new case, update contact count1'+replica3);
}
}
}
update replic;
}
}*/
error was like
Illegal assignment from List<AggregateResult> to List<Integer>
Please try with below code.
If this helps, Please mark it as best answer.
Regards,
Ankaiah Bandi