Can I disable a data resource until all of the required inputs are set?

Jon G Lind
ServiceNow Employee
ServiceNow Employee

Hello. I have a GraphQL data resource that takes several parameters. I wish to disable the resource until all of the values are correctly set, but no matter what I do any change to one of the referenced client state triggers the API.  I have set the data resource to "Execute only when invoked (explicit)", but as you can see every time a bound client state changes it triggers the API call, even if it's missing mandatory values.

Is there a way to make a data resource fire conditionally, i.e. only when all of the values are set?

Data Resource Keeps Updating.gif

 

data resource settings.jpg

1 ACCEPTED SOLUTION

Jon G Lind
ServiceNow Employee
ServiceNow Employee

So, as best I can tell the "Only when invoked" option is not honored unless the data resource is set to "Mutates data" (which you can do on the data resource record itself).  Otherwise it will pretty much fire continuously whenever a dependent client state changes.

One thing I did to work around this was to change my API to not error and just return a blank result if the inputs were blank.  In this way I could kind of "disable" the API by blanking out the input state values--it would still be making calls but those calls would be short and wouldn't be doing anything until I changed the state values back.

View solution in original post

3 REPLIES 3

Jon G Lind
ServiceNow Employee
ServiceNow Employee

So, as best I can tell the "Only when invoked" option is not honored unless the data resource is set to "Mutates data" (which you can do on the data resource record itself).  Otherwise it will pretty much fire continuously whenever a dependent client state changes.

One thing I did to work around this was to change my API to not error and just return a blank result if the inputs were blank.  In this way I could kind of "disable" the API by blanking out the input state values--it would still be making calls but those calls would be short and wouldn't be doing anything until I changed the state values back.

Bernard Esterhu
Tera Expert

In my opinion this is a big deficiency in the Data Resource feature. There should be options for when to evaluate:

  • Implicit (immediately)
  • Explicit (on input change)
  • On trigger (on .execute call)


A very common use case is to first set all the inputs, and then evaluate the data resource. At present this is not possible without workarounds.

In my case, using a transform data resource, I will need to make a JSON type "payload" input, where I can put all my actual inputs, before updating this input to fire the execution. This is possible but not very elegant or easy to maintain.

GTSPerformance
Tera Guru

We wrote an article recently about another way to avoid invoking a data resource until all of the required inputs are set. Essentially, you can create new Client State Parameter(s) and bind that/those to your Data Resource. Then you build a Client Script that will set your Client State Parameter(s) only in the condition that all your required inputs have been provided. Finally, map your Client Script to the events that correspond to the appropriate point in your page/component lifecycles. For example, you could fire your Client Script after the "Page ready" event on the "body" component or the "Data Fetch Succeeded" event of a Data Resource upon which you are dependent. A video is included in the article: see https://www.servicenow.com/community/developer-articles/performance-best-practice-for-next-experienc...