You need to sign in to do that
Don't have an account?

Work Order before insert trigger test class coverage not working
Test class not covering work order before insert trigger.
Trigger:
Test Class:
Trigger:
trigger assignTechnician on Work_Order__c (before insert) { List<User> userList = new List<User>(); for(Work_Order__c wo : Trigger.New) { if(wo.Auto_assign_technician__c == TRUE) { userList = [select id from User where City = :wo.City__c AND UserRole.Name = :wo.Order_Type__c]; for(User u : userList) { wo.OwnerId = u.Id; } } } }
Test Class:
@isTest private class assignTechnicianTest { static testMethod void validateassignTechnician() { Test.startTest(); Work_Order__c wo = new Work_Order__c(); wo.City__c = 'Mumbai'; wo.Order_Type__c = 'Field Service'; insert wo; wo.Auto_assign_technician__c = true; update wo; System.assertEquals(wo.Auto_assign_technician__c, true); List<User> userList = new List<User>(); UserRole r = new UserRole(name = 'Field Service'); insert r; Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', ProfileId = p.Id, LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles', UserName='testservicepro@testorg.com'); u.City = 'Mumbai'; u.UserRoleId = r.id; insert u; Test.stopTest(); } }
You can try out the below code,
1. Inserted UserRole
2. Inserted User.
3. To avoid Mixed DML used System.runAs
4. Insert your custom object record.
Hope this will solve your problem..!
All Answers
You can try out the below code,
1. Inserted UserRole
2. Inserted User.
3. To avoid Mixed DML used System.runAs
4. Insert your custom object record.
Hope this will solve your problem..!
1. Let ensure your trigger is active.
2. Ensure give all mandatory field values in your test class for the Work order object.
3. Run the test class and check if there any excption found in DEBUG LOG.
Let me know if you still have problem..!