Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Service Portal and \$sp.getRecord();

dmfranko
Kilo Guru

I'm trying to build a "detail" page where I can make RESTMessageV2 calls as a result of a user clicking a button.   The challenge is that seemingly on subsequent calls either c.server.update() or spUtil.update($scope) from the client controller seem to empty out the data object when the server side function is called.   Maybe this isn't the best way to do it, but I'd really like to take advantage of RESTMessageV2 if possible so I can reuse this in other places such as UI Action on the platform and keep things organized.

(function(){

  // This seems to null out data?

  var gr = $sp.getRecord();

  if(gr){

            data.canRead = gr.canRead();

            if (!data.canRead)

                      return;

            data.sys_id = gr.getUniqueValue();

  }

  // If someone clicks do action, $scope.data.doAction = 'yes';

  // Then I call c.server.update();

  if(data.doAction == 'yes'){

            // Make my RESTMessageV2 call here.

  }

})()

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Dan,



If you have to make a rest call I think you would want to do it from the client controller rather than the server. On the server side you wouldn't need to use rest, you could just call a script include.


I know I can do this all of the client side, but I'm calling a service external to servicenow, which means I then have to have a scheme to manage all of the endpoints, actions, and authentication in another place.   My hope would be that I could use the outbound REST functionality to manage all of that for me, which has the benefit of keeping it organized as well as reusable in other places in the platform.


dmfranko
Kilo Guru

Think I somewhat answered my own question here.   Reading the documentation here, I need to use input rather than data. Widget Server Script


Please provide an example of how to update a field in a record using sputil.