function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vinothk balasubramanianvinothk balasubramanian 

Could you Please help me how to write the test class for this code

Hi All,

   I don't have much experience in writing the test class as i have written the below code. Can you please write the test class for the below code.
public without sharing class CaseFrecuencyIncidenceCLS {

    public static Id RTMX_REDLTH_CONSUMER_CALL  = null;
    public static Id RTREJC_SERVICE = null;
    private String vDueDay;
    private String vDueTime;
    private double vDueTimeDoub;
    private String vDueTimeDoubl;
    
     private String Classification;
    private map<String,list<Account>> mpCoverDC; //index by Colony
    
       //-----------------------------$$ future method for final case record update : -------
   @future
    public static void updateCaseStatusToAssigned(id caseRecId) {
    system.debug('@@caseRecId@@'+caseRecId);
    Case caseRec = [SELECT id, status, Service_Center__c,Declined_Reason__c, Confirmed_Declined_by__c
                    FROM case
                    WHERE id=: caseRecId];
                    
    if(caseRec.Service_Center__c != null){
    caseRec.status= 'Assigned';
    caseRec.Declined_Reason__c='';
    caseRec.Confirmed_Declined_by__c='';
    
    update caseRec;
    }
    }
    //----------------------------------------------------------------------   
    public void rt(){
        RTMX_REDLTH_CONSUMER_CALL  = getRecordTypeId('Case','MX RedLTH Consumer Call');
        RTREJC_SERVICE = getRecordTypeId('Case','Rejected Service');
    }//rt()
    //----------------------------------------------------------------------
    //Used to get record Type Id
    public Id getRecordTypeId(String objType, String name) {        
        Id RecordTypeId = null;        
        Schema.SObjectType targetType = Schema.getGlobalDescribe().get(objType);
        if (targetType != null) {
            SObject obj = targetType.newSObject();
            Schema.DescribeSObjectResult d = obj.getSObjectType().getDescribe();
            if (d != null) {
                Map<String,Schema.RecordTypeInfo> rtMap = d.getRecordTypeInfosByName();
                if (rtMap != null) {
                    Schema.RecordTypeInfo rtInfo = rtMap.get(name);
                    if (rtInfo != null) {
                        RecordTypeId = rtInfo.getRecordTypeId();
                    }//if
                }//if
            }//if
        }//if
        return RecordTypeId;
    }//getRecordTypeId
    //----------------------------------------------------------------------
    public void updateRelatedContact(list<Case> cases){
        list<Id> lsIDCITY = new list<Id>();
        list<Id> lsIDCOLO = new list<Id>();
        list<Id> lsIDCONT = new list<Id>();
        for(Case c: cases){
            lsIDCITY.add(c.City__c);
            lsIDCOLO.add(c.Colony__c);
            lsIDCONT.add(c.ContactId);
        }//for
        
        map<Id,City__c> mpCITY = new map<Id,City__c>([SELECT Id,Name From City__c where id=:lsIDCITY]);
        map<Id,Colony__c> mpCOLONY = new map<Id,Colony__c>([SELECT Id,Name From Colony__c where id=:lsIDCOLO]);
        map<Id,Contact> mpCONTACT = new map<Id,Contact>([SELECT Id,Name From Contact where id=:lsIDCONT AND MailingCity=null AND MailingState=null]);
        
        for(Case Issue: cases){
            if(mpCONTACT.containsKey(Issue.ContactId)){
                Contact cont = mpCONTACT.get(Issue.ContactId);
                cont.MailingCountry= (Issue.Country__c == null)?'':Issue.Country__c;
                cont.MailingStreet= ((Issue.Street__c == null)?'':Issue.Street__c) + ' ' + ((Issue.Street_number__c == null)?'':Issue.Street_number__c);
                cont.MailingCity= (Issue.City__c == null || !mpCITY.containsKey(Issue.City__c))?'':mpCITY.get(Issue.City__c).Name;
                cont.MailingState= (Issue.State__c == null)?'':Issue.State__c;
                cont.MailingPostalCode= (Issue.Zip_Code__c == null)?'':Issue.Zip_Code__c;
                cont.Colony__c= (Issue.Colony__c == null || !mpCOLONY.containsKey(Issue.Colony__c))?'':mpCOLONY.get(Issue.Colony__c).Name;
                cont.Between_Streets__c= (Issue.Between_Streets__c == null)?'':Issue.Between_Streets__c;
                cont.Reference__c= (Issue.References__c == null)?'':Issue.References__c;
                cont.Place__c= (Issue.Place__c == null)?'':Issue.Place__c;
            }//if
            }//for
        
        if(!mpCONTACT.isEmpty())
            update mpCONTACT.values();
    }//updateRelatedContact
    //----------------------------------------------------------------------      
 
    public void updateFrecuencyIncidence(list<Case> cases){
        searchServiceCenter(cases);
        for(Case c: cases){
            Account center = assignServiceCenter(c.Colony__c);
            if(center.Id != null) {
                c.Service_Center__c=center.Id;
                c.Service_center_email__c=center.E_mail__c;
                c.Status = 'Assigned';
                // Changed value from 'center.AccountID' to 'c.Contact.AccountId' #(Task: , Story:S-266263)
                c.AccountID = c.Contact.AccountId;
                System.debug(' Case Center Id :' + center.Id);
                 System.debug(' Case Service Center :' + c.Service_Center__c);
                  System.debug('Case Service Center email :' + c.Service_center_email__c);
                   System.debug('Case Status :' + c.Status);
               //------Added by Vinoth-----------    
               
                
                //--------------------------------
                
            }
            else {
                c.addError('No existe un Centro de Servicio registrado con cobertura para esta colonia. There is no Service Center Coverage for this Neighborhood');
            }//if
        }//for
    }
    //----------------------------------------------------------------------
    public void searchServiceCenter(list<Case> cases){
        list<Id> lsColony = new list<Id>();
      
        for(Case c: cases){
            lsColony.add(c.Colony__c);
            //lsCity.add(c.City__c);             
            vDueDay = c.Due_Day__c;
            vDueTimeDoub = c.Due_Time__c;
            Classification = c.Classification__c;
            System.debug('vDueTimeDoub :' + vDueTimeDoub);
        }//for
        
        // Get all the 'Service Centers Ids' for this colony that provides 'Home Delivery'
        list<Service_Center_Coverage__c >lsCover = new list<Service_Center_Coverage__c >([Select id,Colony__c,Service_Center__c FROM Service_Center_Coverage__c Where Colony__c=: lsColony
        AND Service_Center__r.RecordType.Name = 'LTH Service Center' and Service_Center__r.Service_Center_Type__c = 'Red LTH SC' and Service_Center__r.Home_delivery_service__c = true]);        
        
        list<Id> lsACC = new list<Id>();
        for(Service_Center_Coverage__c csc :lsCover){
            lsACC.add(csc.Service_Center__c);
            System.debug('* 2. SC Coverage  : '+ csc.id);
        }//for

        
        mpCoverDC = new map<String,list<Account>>();      
        //map<Id,Account> mpAccount ;
        map<Id,Account> mpAccount = new map<Id,Account>();
        // Query for all the Account that provides 'Home Delivery' in this colony

        
        if (vDueDay == 'Saturday' && Classification== 'Home Service - Scheduled')
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Saturday__c <> 'Closed'
              AND Closing_Hours_Saturday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Saturday_Int__c <= :vDueTimeDoub                            
              order by Frecuency__c, Incidence__c]);
        }//vDueDay == 'Saturday'
        else if (vDueDay == 'Sunday' && Classification== 'Home Service - Scheduled')
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Sunday__c <> 'Closed'   
              AND Closing_Hours_Sunday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Sunday_Int__c <= :vDueTimeDoub                            
              order by Frecuency__c, Incidence__c]);            
        }//vDueDay == 'Sunday'
        else if ((vDueDay == 'Monday'
                 || vDueDay == 'Tuesday'
                 || vDueDay == 'Wednesday'  
                 || vDueDay == 'Thursday'
                 || vDueDay == 'Friday') && Classification == 'Home Service - Scheduled' )
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Monday_to_Friday__c <> 'Closed'            
              AND Closing_Hours_Monday_to_Friday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Monday_to_Friday_Int__c <= :vDueTimeDoub                                                                                    
              order by Frecuency__c, Incidence__c]);            
        }//vDueDay == Weekdays
        
         else if (vDueDay == 'Saturday' && Classification == 'Home Service')
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Sunday__c <> 'Closed'   
              AND Closing_Hours_Sunday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Sunday_Int__c <= :vDueTimeDoub                            
              order by Frecuency__c, Incidence__c]);            
        }
        
        else if (vDueDay == 'Sunday' && Classification == 'Home Service')
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Sunday__c <> 'Closed'   
              AND Closing_Hours_Sunday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Sunday_Int__c <= :vDueTimeDoub                            
              order by Frecuency__c, Incidence__c]);            
        }
        
        else if ((vDueDay == 'Monday'
                 || vDueDay == 'Tuesday'
                 || vDueDay == 'Wednesday'  
                 || vDueDay == 'Thursday'
                 || vDueDay == 'Friday') && Classification == 'Home Service' )
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Monday_to_Friday__c <> 'Closed'            
              AND Closing_Hours_Monday_to_Friday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Monday_to_Friday_Int__c <= :vDueTimeDoub                                                                                   
              order by Frecuency__c, Incidence__c]);            
        }
        system.debug('* Service Centers found : ' + mpAccount.keySet());
        
        calculateFrequencyNIncidents(mpAccount.values());

        for(Case c: cases){
            String COLONY = c.Colony__c;
            list<Account> lsTMP = new list<Account>();
            for(Service_Center_Coverage__c csc:lsCover){
                if(csc.Colony__c==COLONY && mpAccount.containsKey(csc.Service_Center__c))
                    lsTMP.add(mpAccount.get(csc.Service_Center__c));
            }//for
            system.debug('lsTMP '+lsTMP);
      
            mpCoverDC.put(COLONY,lsTMP);
        }
    }//searchServiceCenter
    //----------------------------------------------------------------------
    
 
    public map<String,list<Account>> searchServiceCentersforrejected(list<Case> cases){   //$$ method return type changed
        system.debug('##entry into searchServiceCenter ##');
        list<Id> lsColony = new list<Id>();
        list<Id> lsACC = new list<Id>();  //$$
        list<Id> lsAlredyAssignedACC = new list<Id>();  //$$ to hold the already assigned srvc centre ids
        Set<Id> allCaseId = new Set<Id>();
        list<Service_Center_Case_Status__c> allRelatedSrvcCntrCaseStatus = new list<Service_Center_Case_Status__c>(); //$$-----
        Set<Id> allRelatedServiceCenterIds = new Set<Id>();  //$$---------------
      
        for(Case c: cases){
            lsColony.add(c.Colony__c);
            allCaseId.add(c.id);  //$$------------
            //lsCity.add(c.City__c);             
            vDueDay = c.Due_Day__c;
            vDueTimeDoub = c.Due_Time__c;
            //vDueTimeDouble = c.DueTime__c;
            Classification = c.Classification__c;
            System.debug('vDueTimeDoub :' + vDueTimeDoub);
        }//for
        //$$---------------------------------
        system.debug('##allCaseId##'+allCaseId);
        allRelatedSrvcCntrCaseStatus = [SELECT Service_Center__c,Case_Number__c
                                      FROM Service_Center_Case_Status__c
                                      WHERE Case_Number__c in: allCaseId];
        system.debug('##allRelatedSrvcCntrCaseStatus ##'+allRelatedSrvcCntrCaseStatus );
        if(allRelatedSrvcCntrCaseStatus.size()>0)
        {
            for(Service_Center_Case_Status__c sCCSVar: allRelatedSrvcCntrCaseStatus)
            {
                allRelatedServiceCenterIds.add(sCCSVar.Service_Center__c);
             }
         }
         system.debug('##allRelatedServiceCenterIds##'+allRelatedServiceCenterIds);
         //$$----------------------
        // Get all the 'Service Centers Ids' for this colony that provides 'Home Delivery'  //$$-------------
        list<Service_Center_Coverage__c >lsCover = new list<Service_Center_Coverage__c >([Select id,Colony__c,Service_Center__c
                                                                                            FROM Service_Center_Coverage__c
                                                                                            Where Colony__c=: lsColony
                                                                                            AND Service_Center__r.RecordType.Name = 'LTH Service Center'
                                                                                            and Service_Center__r.Service_Center_Type__c = 'Red LTH SC'
                                                                                            and Service_Center__r.Home_delivery_service__c = true
                                                                                            AND Service_Center__r.Id NOT in: allRelatedServiceCenterIds]);        
         system.debug('##lsCover ##'+lsCover );
        if(lsCover.size() > 0)
        {
        lsACC = new list<Id>();
            for(Service_Center_Coverage__c csc :lsCover){
                lsACC.add(csc.Service_Center__c);
                System.debug('## SC Coverage option ##: '+ csc.id);
            }//for
        }// end of if
        else{
          system.debug('## NO SERVICE CENTER AVAILABLE');  
        }
        system.debug('##lsACC ##'+lsACC);
        mpCoverDC = new map<String,list<Account>>();      
        //map<Id,Account> mpAccount ;
        map<Id,Account> mpAccount = new map<Id,Account>();
        // Query for all the Account that provides 'Home Delivery' in this colony

        
        if (vDueDay == 'Saturday' && Classification== 'Home Service - Scheduled')
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Saturday__c <> 'Closed'
              AND Closing_Hours_Saturday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Saturday_Int__c <= :vDueTimeDoub                            
              order by Frecuency__c, Incidence__c]);
        }//vDueDay == 'Saturday'
        else if (vDueDay == 'Sunday' && Classification== 'Home Service - Scheduled')
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Sunday__c <> 'Closed'   
              AND Closing_Hours_Sunday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Sunday_Int__c <= :vDueTimeDoub                            
              order by Frecuency__c, Incidence__c]);            
        }//vDueDay == 'Sunday'
        else if ((vDueDay == 'Monday'
                 || vDueDay == 'Tuesday'
                 || vDueDay == 'Wednesday'  
                 || vDueDay == 'Thursday'
                 || vDueDay == 'Friday') && Classification == 'Home Service - Scheduled' )
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Monday_to_Friday__c <> 'Closed'            
              AND Closing_Hours_Monday_to_Friday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Monday_to_Friday_Int__c <= :vDueTimeDoub                                                                                    
              order by Frecuency__c, Incidence__c]);            
        }//vDueDay == Weekdays
        
         else if (vDueDay == 'Saturday' && Classification == 'Home Service')
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Sunday__c <> 'Closed'   
              AND Closing_Hours_Sunday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Sunday_Int__c <= :vDueTimeDoub                            
              order by Frecuency__c, Incidence__c]);            
        }
        
        else if (vDueDay == 'Sunday' && Classification == 'Home Service')
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Sunday__c <> 'Closed'   
              AND Closing_Hours_Sunday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Sunday_Int__c <= :vDueTimeDoub                            
              order by Frecuency__c, Incidence__c]);            
        }
        
        else if ((vDueDay == 'Monday'
                 || vDueDay == 'Tuesday'
                 || vDueDay == 'Wednesday'  
                 || vDueDay == 'Thursday'
                 || vDueDay == 'Friday') && Classification == 'Home Service' )
        {
            mpAccount
            = new map<id,Account>
            ([Select id, Name, E_mail__c,Frecuency__c, Incidence__c,
              Closing_Hours_Monday_to_Friday__c,Closing_Hours_Saturday__c,Closing_Hours_Sunday__c,
              Opening_Hours_Saturday_Int__c,Opening_Hours_Sunday_Int__c,Opening_Hours_Monday_to_Friday_Int__c
              FROM Account
              Where
              Id =: lsACC
              AND Closing_Hours_Monday_to_Friday__c <> 'Closed'            
              AND Closing_Hours_Monday_to_Friday_Int__c >= :(vDueTimeDoub + 1)
              AND Opening_Hours_Monday_to_Friday_Int__c <= :vDueTimeDoub                                                                                   
              order by Frecuency__c, Incidence__c]);            
        }
        system.debug('* Service Centers found : ' + mpAccount.keySet());
        
        calculateFrequencyNIncidents(mpAccount.values());

        for(Case c: cases){
            String COLONY = c.Colony__c;
            list<Account> lsTMP = new list<Account>();
            for(Service_Center_Coverage__c csc:lsCover){
                if(csc.Colony__c==COLONY && mpAccount.containsKey(csc.Service_Center__c))
                    lsTMP.add(mpAccount.get(csc.Service_Center__c));
            }//for
            system.debug('##lsTMP ##'+lsTMP); //$$------------
           
            system.debug('##lsTMP@@'+lsTMP);  //$$-------------
            mpCoverDC.put(COLONY,lsTMP);
        }
        return mpCoverDC;
    }//searchServiceCentersforrejected
   
 
    //----------------------------------------------------------------------------------------------------------
    private void calculateFrequencyNIncidents(list<Account> Accs){
        Service_Center_Selector__c scs = Service_Center_Selector__c.getInstance(UserInfo.getProfileId()); //Custom Hierarchy settings
        double Incid = (scs==null || scs.Incidents_Range_Days__c ==null )?1:scs.Incidents_Range_Days__c;
        Date d= system.today().addDays(Incid.intValue()*-1);
        System.debug('****Incidence Date****:' +d);
        
       // Changed the RecordType from "Rejected Service" to 'MX RedLTH Consumer Call' and added the Status "Declined" by Vinoth
        AggregateResult[] grpINC = [Select Count(id) TOT,Service_Center__c FROM Case Where Service_Center__c in : Accs and RecordType.Name = 'MX RedLTH Consumer Call' and Status='Declined' and CreatedDate >=: d GROUP BY Service_Center__c];
        map<Id,Integer> mpINCIDENCES = new map<Id,Integer>();
        for(AggregateResult AR: grpINC)
            mpINCIDENCES.put(Id.valueOf(String.valueOf(AR.get('Service_Center__c'))),Integer.valueOf(AR.get('TOT')));
        //FREQUENCY
        double Frec = (scs==null || scs.Frequency_Range_Days__c ==null )?1:scs.Frequency_Range_Days__c;
        d= system.today().addDays(Frec.intValue()*-1);
        System.debug('***Frequency Date****:' +d);
        // Changed the record type from 'Home service' to 'MX RedLTH Consumer Call' #(T-325212)
        AggregateResult[] grpFRC = [Select Count(id) TOT,Service_Center__c FROM Case Where Service_Center__c in : Accs and RecordType.Name = 'MX RedLTH Consumer Call' and Status='Assigned' and CreatedDate >=: d GROUP BY Service_Center__c];
        map<Id,Integer> mpFREQUENCY = new map<Id,Integer>();
        for(AggregateResult AR: grpFRC)
            mpFREQUENCY.put(Id.valueOf(String.valueOf(AR.get('Service_Center__c'))),Integer.valueOf(AR.get('TOT')));
        for(Account A:Accs){
            A.Frecuency__c = (mpFREQUENCY.containsKey(A.Id))?mpFREQUENCY.get(A.Id):0;
            System.debug('****Frequency****:' +A.Frecuency__c );
            A.Incidence__c = (mpINCIDENCES.containsKey(A.Id))?mpINCIDENCES.get(A.Id):0;
            System.debug('****Incidence****:' + A.Incidence__c );
        }//for
    }//calculateFrequencyNIncidents
    //----------------------------------------------------------------------
    public Account assignServiceCenter(String Colony){
        Account center = new Account();
        Boolean assignment = false;
        if(mpCoverDC.containsKey(Colony)){
            list<Account> lsAccount = mpCoverDC.get(Colony);
            for(Account acc : lsAccount){
                if(assignment==false || (acc.Frecuency__c==center.Frecuency__c && acc.Incidence__c<center.Incidence__c) || (acc.Frecuency__c<center.Frecuency__c)){
                   center.Id=acc.Id;
                   center.E_mail__c=acc.E_mail__c;
                   center.Frecuency__c=acc.Frecuency__c;
                   center.Incidence__c=acc.Incidence__c;
                   assignment=true;
                   System.debug('****CenterId****:' + center.Id );
                   System.debug('****CenterEmail****:' + center.E_mail__c );
                   System.debug('****CenterFrequency****:' + center.Frecuency__c );
                   System.debug('****CenterIncidence****:' + center.Incidence__c );
                }//if
            }//for
        }
        //This method is added to update the account record (Task: T-325212)
        if(center != null) {
        updateAccountRecord(center);
        }//if
        return center;
    }//assignServiceCenter  
    
  private void updateAccountRecord(Account cen) {
   
        List<Account> acc =                          
                    [SELECT
                    Id, Frecuency__c, Incidence__c,
                    Opening_Hours_Monday_to_Friday__c,
                    Closing_Hours_Monday_to_Friday__c,
                    Opening_Hours_Saturday__c,
                    Closing_Hours_Saturday__c,
                    Opening_Hours_Sunday__c,
                    Closing_Hours_Sunday__c                 
                    FROM
                    Account
                    WHERE Id =:cen.Id LIMIT 1];
                If (acc.size()>0)
                {
                        if(acc[0].Frecuency__c != null) {
                        acc[0].Frecuency__c++;
                }//if
                else {
                    acc[0].Frecuency__c = 1;
                    
                }
            update acc;
        
                }//If (acc.size()>0)
        
    }//updateAccountRecord
    }

 
Keshab AcharyaKeshab Acharya
Dear Vinothk,

Please let us know if you have any issue in writing and executing test class for above code snippet. You can check the below link on how to write test class.

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods