IHow do you copy catalog variables to a request field?

kmathis
Kilo Contributor

I've seen this topic in the community a lot, but no straight answers on how to accomplish this.

I have two variables in a Service Catalog item that I need to copy to a field in the Request (sc_request).   Is this possible?   I've tried writing a Business Rule, a variable client script, etc.   I cannot copy these variables contents into a Request field.

Thanks in advance!

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Kari,



Write a Before Insert Business rule on sc_request table and add the following script in there. The following script copies all the questions and their values of all the requested items into description field on the request form



var gr= new GlideRecord("sc_req_item");


  gr.addQuery('request',current.sys_id);


  gr.query();


  var varArr='';


  while(gr.next()){


  var set = new GlideappVariablePoolQuestionSet();


  set.setRequestID(gr.sys_id);


  set.load();


  var vs = set.getFlatQuestions();


  for (var i=0; i < vs.size(); i++) {


  if(vs.get(i).getLabel() != '') {


  varArr+=vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue()+'\n';


  }


  }


  }


  current.description=varArr;




Thanks,


Abhinay




PS: Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

24 REPLIES 24

Thanks for this information Chuck .



But even if I commented current.update(); its not working.


Sorry, I meant to type more about the solution before hitting save.



First thing to check is if your variable actually has a value.



inside the if statement, add a line something like this then check the logs.



gs.info('>>>DEBUG: WD_sa_requested_for: ' + ritm.variables.WD_sa_requested_for);



Alternatively you can use the script debugger to set a break point and investigate the values of the variables and fields.



Second thing, check the log for any errors. It may be complaining and you're not seeing it.


Hello Abhinay,

How we can exclude the questions(variables) which are null.

Please suggest.

Thanks

kmathis
Kilo Contributor

I tweaked the script just a bit to accomplish what I needed.   Much thanks to all of your for your input, time & help!


This is a Business Rule in the sc_request table running Before Insert.   This script completes the Short Description and Description field from the Variables on the request form in the catalog:



var gr= new GlideRecord("sc_req_item");


  gr.addQuery('request',current.sys_id);


  gr.query();


  var varArr='';


  while(gr.next()){


  var set = new GlideappVariablePoolQuestionSet();


  set.setRequestID(gr.sys_id);


  set.load();


  var vs = set.getFlatQuestions();


  for (var i=0; i < vs.size(); i++) {


  if(vs.get(i).getLabel() != '') {


  varArr+=vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue()+'\n';


  }


  }


  }


  current.short_description=gr.variables.Request_title;


  current.description=gr.variables.description;


Dead Blade
Kilo Guru

This script works thanks, but how can I only show variables that equal true and hide "= true"