2

I have a problem with Field Service Lightning managed package in my test class.

Context:

I have done the next things:

  • Create a Lightning Component and a Apex Controller to change the service appointment's status from Status 'A' to Status B.

  • Enabled the status transaction inside the field service settings.

In lightning the component is working fine. But I am getting this error when I run my test class:

Cannot change status from A to B

If I use (seeAllData=true) the test pass.


What I think that is my error:

I think that I have to create something like a configuration record in my test class to enable the status transaction.

Why do I get this error? How can I fix it?

2

After a quick research, I realized a few status transitions are auto-created by package but not all of them. When you are runnning test, you do not have access those, so you need to use following method to create status transition for your test class:

FSL.GlobalAPIS.addStatusTransition(fromStatus, toStatus);
2

Assuming you do not care what the previous status was.

If testing an update trigger on ServiceAppointment where Status changed from A to B:

In your test method update all statuses to null first and then update the ServiceAppointments again to the desired status.

    // All ServiceAppointment statuses are set to some '{Status A}'
    for(ServiceAppointment oneAppointment : ServiceAppointments){
        oneAppointment.Status = null;
    }

    update ServiceAppointments;
    for(ServiceAppointment oneAppointment : ServiceAppointments){
        oneAppointment.Status = '{Status B}';
    }

    Test.startTest();
    update ServiceAppointments;
    Test.stopTest();

Reference: https://help.salesforce.com/articleView?id=000265438&language=en_US&type=1

Quote from reference: Whether Status Transitions are enabled or not the Status can always be set to NULL.

  • The problem is that if the class doesn't have the @isTest anotation, you can change the status transanction, because there is a FSL trigger that control the status transitions. In other Salesforce Package as NPSP or HEDA you can setup the configurations, but in FSL you can't do that, why? :( – Torsen May 6 at 8:13

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.