5

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 Answers 2

11

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);
1
  • 1
    This is exactly what I needed right now, today. You solved a big issue for me with this answer. Thanks!
    – joaniej
    Commented Feb 25, 2022 at 20:28
1

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.

1
  • 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
    Commented May 6, 2019 at 8:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .