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

Chuck Tomasi
Tera Patron

Hi Kari,



I haven't tested this, but here's how I would approach it.


  • Create an after insert/update business rule on the sc_req_item (Request Item) table
  • Put a condition on the business rule to only react if the item is the specific one you want (so it doesn't try copying variables from items that don't have those variables.)
  • In the script, do a GlideRecord query on the sc_request table and retrieve the record that has a sys_id of current.request
  • Update the field on retrieved record (ex: req.u_field1 = current.variables.field1)
  • Update the record (req.update())

Thank you.   This is what I have, but it's not working:



Business Rule in the sc_req_item table:


Run After 100; Insert


Filter Conditions:   Item is Request something


Advance Script:


function executeRule(current, previous /*null when async*/) {



      //This function will be automatically called when this rule is processed.


      var gr = new GlideRecord('sc_request');  


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


      gr.query();  


      while(gr.next())  


      {  


          gr.short_description = current.variables.Request_title;


          gr.update();


      }



})(current, previous);


Everything looks pretty straight forward based on what you've described.



The next step would be to enable business rule debugging to see if it is triggering the business rule. If so, add a few debug statements to see how far it gets through the script.



Don't forget to check the Error and Warning logs to see if there's a bug being caught there.


ctomasi and kmathis



Could be the fact that request (sc_request) is created after all the request items so your business rule doesn't know what the request record is yet.



Check out blog post Which came first, the Request or the Requested Item?



"a) Requested Item records are created first before the parent Request record."