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
Cris9931Cris9931 

my test class is failing

Hi, I have this trigger which checks if I have a duplication(Dispatcher) in each Service Team.
trigger AvoidduplicateDispatcher on SVMXC__Dispatcher_Access__c (before insert) {

// for every new dispatcher created fetches the id of the service team selected in serviceTeamMap
    map<Id, Set<String>> serviceTeamMap = new map<Id, Set<string>>();
    for(SVMXC__Dispatcher_Access__c disp: trigger.new){
        if(disp.SVMXC__Service_Team__c!= null){
            serviceTeamMap.put(disp.SVMXC__Service_Team__c, new set<String>());
            system.debug('************'+serviceTeamMap);
        }
     }
     

    if(!serviceTeamMap.isEmpty()){
        for(SVMXC__Dispatcher_Access__c disp1: [Select Id, SVMXC__Dispatcher__c, SVMXC__Service_Team__c from SVMXC__Dispatcher_Access__c where SVMXC__Service_Team__c =: serviceTeamMap.keySet()]){
            serviceTeamMap.get(disp1.SVMXC__Service_Team__c).add(disp1.SVMXC__Dispatcher__c);
        }
        system.debug('******serviceTeamMap*****'+serviceTeamMap);
    }
    
    if(!serviceTeamMap.isEmpty()){
        for(SVMXC__Dispatcher_Access__c dispRec: trigger.new){
            system.debug('************'+dispRec.SVMXC__Dispatcher__c);
             Set<String> dispSet = serviceTeamMap.get(dispRec.SVMXC__Service_Team__c);
             system.debug('************dispSet '+dispSet);
             if(dispSet.contains(dispRec.SVMXC__Dispatcher__c)){
                 system.debug('************'+dispRec.SVMXC__Dispatcher__c);
                     dispRec.addError('You are already a dispatcher in this Service Team!');
             }  
             
        }
    }

}


This is my Test Class.

@isTest
public class AvoidduplicateDispatcher_UT {
    @isTest static void AvoiDuplicateDispatcher_TestMethod(){
        SVMXC__Dispatcher_Access__c dp = new SVMXC__Dispatcher_Access__c();
        dp.SVMXC__Dispatcher__c = '0051i0000012ogr';
        dp.SVMXC__Service_Team__c = 'a341i000000PBPY';
        insert dp;
        
        SVMXC__Dispatcher_Access__c dp1 = new SVMXC__Dispatcher_Access__c();
        dp1.SVMXC__Dispatcher__c = '0051i0000012ogr';
        dp1.SVMXC__Service_Team__c = 'a341i000000PBPY';
        Database.SaveResult sresult = Database.insert(dp1, false); 
    }
}


I have 100% coverage.

 

However in PROD when i want to migrate I have this error:

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You are already a dispatcher in this Service Team!: []
Stack Trace: Class.AvoidduplicateDispatcher_UT.AvoiDuplicateDispatcher_TestMethod: line 12, column 1

How can i fix this guys?​​​​​​​
Best Answer chosen by Cris9931
ANUTEJANUTEJ (Salesforce Developers) 
Hi Cristian,

Can you try checking if the above code violates any of the custom validations?

Looking forward for your response.

Regards,
Anutej

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Cristian,

Can you try checking if the above code violates any of the custom validations?

Looking forward for your response.

Regards,
Anutej
This was selected as the best answer
AnudeepAnudeep (Salesforce Developers) 
I suggest disabling your custom validation rule and deploy the code. Let me know if it helps that way
Cris9931Cris9931

Hi Anutej,

 

This error is coming from my trigger...

You are already a dispatcher in this Service Team!: []
Cris9931Cris9931
Hi Anudeep... please see my msg above...I have 0 validation rule on this object.
Cris9931Cris9931
Now it works. No idea why. :D
Andrew GAndrew G
Hi Christian

Your code is doing what it is supposed to.  How you are getting it to pass in a Sandbox is unknown to me.

Given that you are looking to test that the code will throw an error, you need to do a try...catch and then assertion.

Example:
@isTest
public class AvoidduplicateDispatcher_UT {
    @isTest static void AvoiDuplicateDispatcher_TestMethod(){
        SVMXC__Dispatcher_Access__c dp = new SVMXC__Dispatcher_Access__c();
        dp.SVMXC__Dispatcher__c = '0051i0000012ogr';
        dp.SVMXC__Service_Team__c = 'a341i000000PBPY';
        insert dp;
        
        SVMXC__Dispatcher_Access__c dp1 = new SVMXC__Dispatcher_Access__c();
        dp1.SVMXC__Dispatcher__c = '0051i0000012ogr';
        dp1.SVMXC__Service_Team__c = 'a341i000000PBPY';
try
{
        Database.SaveResult sresult = Database.insert(dp1, false); 
}
catch (Exception e) 
{
     System.assertEquals(true, e.getMessage().contains('You are already a dispatcher in this Service Team!'));
}

    }
}

btw, i'm horrified that you are using Static Ids in a class that has made it to production.  Also your test class has no asserts.  What point is "100% coverage" if you have no real clue if you code is working or not.

Regards
Andrew

p.s. code samples provided uncompiled and as-is