You need to sign in to do that
Don't have an account?
Cris9931
my test class is failing
Hi, I have this trigger which checks if I have a duplication(Dispatcher) in each Service Team.
How can i fix this guys?
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?
Can you try checking if the above code violates any of the custom validations?
Looking forward for your response.
Regards,
Anutej
All Answers
Can you try checking if the above code violates any of the custom validations?
Looking forward for your response.
Regards,
Anutej
Hi Anutej,
This error is coming from my trigger...
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:
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