Get current catalog item variable name or sys_id from within widget

mspeirs
Mega Expert

In a ui macro that is attached to a variable on a catalog item, you are able to get the sys_id of that variable using jelly ${jvar_question_id} and get the name using ${jvar_question_name} and the value using ${jvar_question_value} and label using ${jvar_question_label}. How can I do this from within a service portal widget that is attached to a catalog variable of type macro? That is, how can I tell which macro variable is being represented from within the widget client script or server script so I don't have to hard-code variable names or other info?

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Michael,



This is a bit buried in the documentation, but you can get at the name:


Embedded widgets & g_form

When using the Service Catalog variable type "Macro" and "Macro with Label" you can pick a widget to embed in a catalog item form. Within that embedded widget's client controller you can access the field object and catalog item g_form instance using:


  • $scope.page.field
  • $scope.page.g_form()


documentation/client_scripting.md at master · service-portal/documentation · GitHub


View solution in original post

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Michael,



This is a bit buried in the documentation, but you can get at the name:


Embedded widgets & g_form

When using the Service Catalog variable type "Macro" and "Macro with Label" you can pick a widget to embed in a catalog item form. Within that embedded widget's client controller you can access the field object and catalog item g_form instance using:


  • $scope.page.field
  • $scope.page.g_form()


documentation/client_scripting.md at master · service-portal/documentation · GitHub


Thanks a bunch. I guess I'd seen that about $scope.page.field a while back (and forgot because I hadn't used it). Though I had used the $scope.page.g_form(). I always check the documentation first but occasionally I don't look in the right place or my search terms don't quite pick it up. I was close, I was looking in "Client Script" and "Server script" and APIs. Just missed the "Client Scripting and g_form". I really appreciate it.



Unless I missed it, the documentation didn't mention what attributes were available for $scope.page.field so I'll list the more useful ones I discovered by dumping it into a JSON string:


  • label
  • sys_id
  • variable_name
  • value


So for example, using $scope.page.field.variable_name in the client script gives you the variable name as expected.


Hi Brad,



Thanks for the info.



I'm trying to pass the onchange variable value to server side inorder to pass the values to REST message.



Can you please help me how to achieve that.



Here is my code:



Client controller:



function($rootScope) {


  /* widget controller */


var c = this;


c.data.result = "";


$rootScope.$on("field.change", function(evt, parms) {


//c.data.result = "on change ";


if (parms.field.variable_name == 'short_description') {


c.data.shortdesc = parms.newValue;


// c.data.result += " of short description";


c.server.refresh();


c.data.result = data.result;



}


});




Server script:



  /* populate the 'data' object */


  /* e.g., data.table = $sp.getValue('table'); */


gs.log("############################################ inside server script of widget" + $sp.getValue('short_description'));


//   if(input.stagedModel)


//   {


var helfulkb = '';


gs.log("############################################ inside server script of widget");


//Make web service call


var r = new sn_ws.RESTMessageV2('<rest messagename>', '<REST method name>');


var authenticationtype ='oauth2';


var profName = <profile used>;


r.setAuthentication(authenticationtype, profName);



var body = '{ "input": { "csvInstance": ["application"]}}';// Here i have to pass the onchage Short description variable value. This is what i was looking for.



r.setRequestBody(body);



var response = r.execute();





//}



HTML:



Results: {{c.data.result}}