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.

Referencing Service Catalog Variables in Workflow

jmiskey
Kilo Sage

We have a Service Catalog to request various Telecom Requests.   I am building a Workflow, based on the Requested Item that is created from the Request.   I am trying to add an IF Action that checks to see if the request was for some special software (in which case, I will then use Create Task to create a task fro the software installation).

So, on the Service Catalog, the variable for the requested item is named u_request_type, and it is a Reference Field to the table which contains the various request selections.   The code for my IF action looks like this:

answer = ifScript();

var requestType = current.variables.u_request_type;

function ifScript() {
      if (requestType == 'Special Software') {
              return 'yes';

      } else {

              return 'no';

      }
}

Needless to say, it doesn't work.   I think it is because the u_request_type is a reference field to another table.

So, how do I get the displayed value for this field?   Do I need to dot-walk, or somehow use GetDisplayName?

Not sure exactly what the code for that needs to look like.

Thanks

1 ACCEPTED SOLUTION

My and Joe, i completely over looked your ifScript function   . Your script looks good to me. Lets try one last thing here , for the if



if(current.variables.u_request_type.service.service_name.indexOf('Avaya Software') > -1){


return 'yes';


}


View solution in original post

29 REPLIES 29

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

You can easily get the display value of something using the getDisplayValue() function:


GlideRecord - ServiceNow Wiki



So this line:


var requestType = current.variables.u_request_type;



Becomes:


var requestType = current.variables.u_request_type.getDisplayValue();


Hi Michael,



I have had issues with using   getDisplayValue on variables. Does the behavior differ between fields and variables?


I had tried that earlier, but it was not working (perhaps because the variable was set before the function (see my previous reply to harishkumar).



I wanted to see if it would work in a background script, so I tried both methods like this:


var gr = new GlideRecord('sc_req_item');


gr.addQuery('number','RITM0010549')


gr.query();


while(gr.next()){


      gs.print(gr.variables.u_request_type.service.service_name);


      gs.print(gr.variables.u_request_type.getDisplayValue);


}



and this is what was returned:


*** Script: Avaya Software


*** Script: function getDisplayValue() {


[native code, arity=1]


}



So I don't know that it works.


You forgot the parentheses on getDisplayValue()


var gr = new GlideRecord('sc_req_item');


gr.addQuery('number','RITM0010549')


gr.query();


while(gr.next()){


      gs.print(gr.variables.u_request_type.service.service_name);


      gs.print(gr.variables.u_request_type.getDisplayValue());


}


OK, I added them in, and then that did return the correct value of 'Avaya Software'.



But when I tried to put it back into my Workflow script, it did not work.   Here is what I tried:


                          answer = ifScript();



                          var requestType = current.variables.u_request_type.getDisplayValue();



                          function ifScript() {


                                                          if (requestType == 'Avaya Software') {


                                                                                      return 'yes';


                                                          } else {


                                                                                      return 'no';


                                                          }


                          }



Do we need to use Glide Records in this code?