1

I've been tasked with updating our Salesforce. I'm a pretty experienced Salesforce Admin but I have to admit I know very little about apex code.

Here's the situation. I've tested everything in Sandbox and it has gone swimmingly. I bring everything over into Production and that was fine too. A lot of the processes require immediate rollups, and I have Rollup Helper installed. As I am enabling the realtime triggers from that managed package, I get this error:

System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: 0060b00000mzUPx: [] Stack Trace: Class.testWordOrdersCalcMetrics.insertTestWorkOrder: line 8, column 1

On Class Name: testWordOrdersCalcMetrics and Method Name: insertTestWorkOrder

And as well, I have a Code Coverage Failure and it hovers between 72% - 74%.

At first I didn't know what this was, but I was looking around and saw that this was custom code written a long time ago by a consultant who worked on our instance.

I've tried the following:

  1. Deactivating that Apex Trigger using Sandbox -> Production (Same error and Code Coverage of 74%)
  2. I've made everything associated to this apex trigger (as far as I know but I'm all ears) Public Read/Write and made sure all profiles have CRE access to the Work Order Object. (Is it worth putting Modify All on every profile to test this out?)

I really need to have the real time triggers for Rollup Helper work. I don't know why this never came up within our Sandbox environment, as the test looks like it's failing there too, but I was able to enable real time for the rollup helper in our sandbox with no issues. I have no idea what any of this means so any help (especially basic-level help) on resolving these issues would be amazing.

Here's the code for that apex trigger:

trigger WorkOrders_CalcMetrics on WorkOrder (before insert, before update) {

// Assumes the work hours are the default for the Org
BusinessHours stdBusinessHours = [select id from businesshours where isDefault = true];

// Get the Record Type for "Work Order"
//Id devRecordTypeId = Schema.SObjectType.Work_Order__c.getRecordTypeInfosByName().get('XXXXXX').getRecordTypeId(); 

for (WorkOrder wo : Trigger.new) {

//if ((wo.RecordTypeId == devRecordTypeId) && (stdBusinessHours != NULL)) {
if (wo.Omit_from_Stats__c == True) {
    wo.Age_in_Business_Hours__c = null;
    wo.Age_Paused_in_Business_Hours__c = null;
}
else {    
    if (stdBusinessHours != NULL) {

           if ((wo.Date_Pause_Started__c == NULL) && (wo.Date_Pause_Ended__c == NULL)) {
               wo.Age_Paused_in_Business_Hours__c = null;
           }
           else if (wo.Date_Pause_Started__c == NULL) {
               wo.Age_Paused_in_Business_Hours__c = null;
           }
           else if (wo.Date_Pause_Ended__c == NULL) {
               wo.Age_Paused_in_Business_Hours__c = null;
           }
           else {
               wo.Age_Paused_in_Business_Hours__c = BusinessHours.diff (stdBusinessHours.id, wo.Date_Pause_Started__c, wo.Date_Pause_Ended__c) / 1000 / 60 / 60.00;
           }
       }    

       if (wo.Date_Completed__c != NULL) {
           if (wo.CreatedDate == NULL) {
               wo.Age_in_Business_Hours__c = 0;
           }
           else {
               wo.Age_in_Business_Hours__c = BusinessHours.diff (stdBusinessHours.id, wo.CreatedDate, wo.Date_Completed__c) / 1000 / 60 / 60.00;

               if (wo.Age_Paused_in_Business_Hours__c != NULL) {
                   wo.Age_in_Business_Hours__c =  wo.Age_in_Business_Hours__c - wo.Age_Paused_in_Business_Hours__c;
               }
           }
       }    
       else {
           wo.Age_in_Business_Hours__c = null; 
       }

}
}
}

As well, when I go to the Apex class and find the test, I see this:

private class testWordOrdersCalcMetrics {

static testMethod void insertTestWorkOrder() {

System.debug('BR:Inserting Work Order ');
WorkOrder workO1 = new WorkOrder (Rates__c='Testing Metrics Only 1',Plan_Price__c=1,Status='Completed',Omit_from_Stats__c=True,Date_Pause_Started__c=Date.parse('1/8/18'), Date_Pause_Ended__c=Date.parse('2/8/18'),Client_Limit__c=1, Staff_Limit__c=1, MonthlyorAnnual__c='Monthly', Resellers_Or_Aria__c='Resellers',Opportunity__c='0060b00000mzUPx');
insert workO1;

WorkOrder workO2 = new WorkOrder (Rates__c='Testing Metrics Only 2',Plan_Price__c=1,Status='Completed',Omit_from_Stats__c=False,Date_Pause_Started__c=Date.parse('1/8/18'), Date_Pause_Ended__c=Date.parse('2/8/18'),Client_Limit__c=1, Staff_Limit__c=1, MonthlyorAnnual__c='Monthly', Resellers_Or_Aria__c='Resellers',Opportunity__c='0060b00000mzUPx');
insert workO2;

// Read the record back and verify that the fields modified by this trigger are not null
WorkOrder workO3 = [select Rates__c,Id, Plan_Price__c,Client_Limit__c, Staff_Limit__c, MonthlyorAnnual__c, Resellers_Or_Aria__c, Age_in_Business_Hours__c, Age_Paused_in_Business_Hours__c
from WorkOrder 
where Rates__c = 'Testing Metrics Only 2'];
System.assert (workO3.Age_in_Business_Hours__c != NULL); 
}
}

Any help would be greatly appreciated!

2

The insert of the WorkOrder fails because the system cannot access the related Opportunity__c record. Looking at the code in the test class I noticed something which is considered bad practice: The Id for the record is hardcoded.

WorkOrder workO1 = new WorkOrder (Rates__c='Testing Metrics Only 1',Plan_Price__c=1,Status='Completed',Omit_from_Stats__c=True,Date_Pause_Started__c=Date.parse('1/8/18'), Date_Pause_Ended__c=Date.parse('2/8/18'),Client_Limit__c=1, Staff_Limit__c=1, MonthlyorAnnual__c='Monthly', Resellers_Or_Aria__c='Resellers',Opportunity__c='0060b00000mzUPx');

This is the culprit Opportunity__c='0060b00000mzUPx'); This Id was probably present in your sandbox, but it doesn't exist on production. To solve it you should add some code to your test class which inserts and opportunity__c record first and then use the id generated in the workorder.

Since I don't know your custom object, I don't know which fields are required to insert an opportunity__c record, but the code would look something like this:

private class testWordOrdersCalcMetrics {
        static testMethod void insertTestWorkOrder() {
            System.debug('BR:Inserting Work Order ');
            Opportunity__c opp01 = new Opportunity__c (Name = 'opp01');
            insert opp01;
            Opportunity__c opp02 = new Opportunity__c (Name = 'opp02');
            insert opp02;

            WorkOrder workO1 = new WorkOrder (Rates__c='Testing Metrics Only 1', Plan_Price__c=1, Status='Completed', Omit_from_Stats__c=True, Date_Pause_Started__c=Date.parse('1/8/18'), Date_Pause_Ended__c=Date.parse('2/8/18'), Client_Limit__c=1, Staff_Limit__c=1, MonthlyorAnnual__c='Monthly', Resellers_Or_Aria__c='Resellers', Opportunity__c='0060b00000mzUPx');
            insert workO1;
            WorkOrder workO2 = new WorkOrder (Rates__c='Testing Metrics Only 2', Plan_Price__c=1, Status='Completed', Omit_from_Stats__c=False, Date_Pause_Started__c=Date.parse('1/8/18'), Date_Pause_Ended__c=Date.parse('2/8/18'), Date_Completed__c=Date.parse('2/8/19'), Client_Limit__c=1, Staff_Limit__c=1, MonthlyorAnnual__c='Monthly', Resellers_Or_Aria__c='Resellers', Opportunity__c='0060b00000mzUPx');
            insert workO2;
            // Read the record back and verify that the fields modified by this trigger are not null
            WorkOrder workO3 = [select Rates__c, Id, Plan_Price__c, Client_Limit__c, Staff_Limit__c, MonthlyorAnnual__c, Resellers_Or_Aria__c, Age_in_Business_Hours__c, Age_Paused_in_Business_Hours__c
                                from WorkOrder
                                where Rates__c = 'Testing Metrics Only 2'];
            System.assert (workO3.Age_in_Business_Hours__c != NULL);
        }
    }

Hopes this helps!

Update

The assertion was failing because there was no value found for Age_in_Business_Hours__c . The reason for this was that the trigger was checking for Date_Completed__c before calculating this value. The test class has probably never worked, or the trigger was changed after deployment. To resolve the issue I've added a value for Date_Completed__c in the test class. Please let me know if this works!

| improve this answer | |
  • Hi RickForce - That does help! I was wondering if that was the case. It's so odd though since that hardcoded ID was in our production, not causing issues, ported to sandbox for my fixes, but now as I bring things back into production the deployment is failing....I did try again and got this error: System.AssertException: Assertion Failed on Line 17, Column 1. Not sure what that means and any help would be appreciated! – Cat Oct 6 '19 at 13:56
  • Hi Cat, the assertion failing means that the check that's at the end of your test class is failing. This one System.assert (workO3.Age_in_Business_Hours__c != NULL);. This assertion is checking if there is a value present for Age_in_Business_Hours__c (0 would also be a value). But apparently there is no value there. I just focused on your error and hadn't checked the entire code yet, but I don't think this test class has ever worked with this trigger. Unless the trigger code has been changed afterwards. – RickForce Oct 6 '19 at 16:21
  • updated the code in my answer. hopefully it works now, please let me know – RickForce Oct 6 '19 at 16:54
  • Oh wow thank you so much! I actually probably did something sacrilegious and just removed that entire Assertion piece and it went through into production fine and my rollup helper started working! I think you are right though, I don't think this test ever worked or something was changed after deployment. This was from a legacy user so had no idea what was going on. Not sure why it only started affecting my deployments after I ported stuff over from sandbox. What you said makes a lot of sense though, and if I weren't on a time crunch for Monday I would definitely try it and see if it works! – Cat Oct 6 '19 at 18:27
  • Haha yes, you broke some rules for sure. But the consultant who left you with this piece of code did a lot worse. Without assertions your test class is pretty useless, but it wasn't doing you much good anyway. The code might have been deployed in a time where tests and code coverage weren't enforced, or when code changes were possible on production. Can't say anything for certain though. Glad to hear you're back on track though! – RickForce Oct 6 '19 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.