0

I'm pretty new to writing test classes and I seem to be stuck here. Please can you point out the issue?

Function:

public static void UpdateCaseRegionCode(List<SObject> listOfNewSObjects ){

         list<Region__c> listOfRegion = (list<SObject>) listOfNewSObjects ;
         list<Region__c> listOfRegionUpdated = new list<Region__c> () ;

          Set<Id> setOfCaseIds = new  Set<Id> ();


         if(!listOfRegion.isEmpty()){
            for(Region__c RegionObj :listOfRegionUpdated ){
                if( Region__c.IsPrimary  == true  ) {
                    setOfCaseIds.add(RegionObj.Case__c);
                }
            }

         }

        if(setOfCaseIds.isEmpty()){
            return;
        }

        list<case> listOfCases = new list<case> ();
        list<case> listOfCasesUpdated = new list<case> ();
        if(!setOfCaseIds.isEmpty())
        {
            listOfCases = [select id,CaseNumber,RegionCode__c from case where id in :setOfCaseIds];
        }

         if(!listOfCases.isEmpty()){
             for (Case caseObj: listOfCases){
                if( caseObj.RegionCode__c!='M1'  || caseObj.RegionCode__c !=  'M2' ){
                    caseObj.RegionCode__c ='USA2';
                    listOfCasesUpdated.add(caseObj);
                }
             }
         }

         if(listOfCasesUpdated!= null && !listOfCasesUpdated.isEmpty()){
            update listOfCasesUpdated;
         }



         }

I have written a test class which creates a region object and a case object. WHat else should be done.Also, i believe even creating these 2 objects should contribute to some percentage of code coverage which i am unable to get.

Any help is appreciated.

Regards, Anna

1
  • 2
    So your issue is with the test class not producing coverage and you would like us to comment yet you do not provide any code at the root of said problem?
    – Eric
    Aug 25, 2018 at 8:02

2 Answers 2

1

The code you have posted does not look to me like it would logically work (or compile). Best to not have so many variables and to avoid unnecessary if conditions so the important logic stands out.

Something like this:

public static void updateCaseRegionCode(List<SObject> listOfNewSObjects) {

    List<Region__c> regions = (List<Region__c>) listOfNewSObjects;

    Set<Id> caseIds = new Set<Id>();
    for (Region__c r : regions) {
        if (r.IsPrimary__c && r.Case__c != null) {
            caseIds.add(r.Case__c);
        }
    }

    if (!caseIds.isEmpty()) {
        List<Case> updates = [
                select Id, RegionCode__c
                from Case
                where Id in :caseIds
                and RegionCode__c not in ('M1', 'M2')
                ];
        for (Case c : updates) {
            c.RegionCode__c = 'USA2';
        }
        update updates;
    }
}

I think the Apex Testing Trailhead module covers everything you need to know to test the class.

0

You havent provided enough information to answer your question.But I need to give few suggestion to you.I think it will work for you

Suggestion 1:

classname-> classname of the UpdateCaseRegionCode.

Just create list of sobjects and call this method

     classname.UpdateCaseRegionCode(createdListOfSobjects).

Suggestion 2:

While creating regional object: It should match satisfying condition

For example: while creating regional isPrimary should be true.(sample rule applies for other Objects as well).(for example:case in your example)

Check test class practices here:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .