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


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> ();

            for(Region__c RegionObj :listOfRegionUpdated ){
                if( Region__c.IsPrimary  == true  ) {



        list<case> listOfCases = new list<case> ();
        list<case> listOfCasesUpdated = new list<case> ();
            listOfCases = [select id,CaseNumber,RegionCode__c from case where id in :setOfCaseIds];

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

         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

  • 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
    Commented Aug 25, 2018 at 8:02

2 Answers 2


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) {

    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.


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


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:


You must log in to answer this question.

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