The CreatorCon Call for Content is officially open! Get started here.

How to retrieve value of Request State of a Requested Item

reginabautista
Kilo Sage

Hi guys,

I need to display the request state of a requested catalog item in my Service Portal landing page. For some reason I am unable to get the value of the field.

I am also confused on its supposed value..

In the list view and on the Form, the value is "Approved"

find_real_file.png

however when I checked the XML, the value of the request_state field is "in_process"

find_real_file.png

Anyway, I was hoping to get the value "Approved"..Here's my code:

(function() {

// populate the 'data' object

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

  data.pirs = [];

  var gr1 = new GlideRecord('task');

  var qc1 = gr1.addQuery('sys_class_name', 'u_pir');

  qc1.addOrCondition('sys_class_name', 'sc_request');

  qc1.addOrCondition('sys_class_name', 'u_pir_task');

  gr1.addActiveQuery();

  gr1.addQuery('opened_by', gs.getUserID());

      gr1.query();

    data.count = gr1.getRowCount();

      var gr = new GlideRecord('task');

  var qc2 = gr.addQuery('sys_class_name', 'u_pir');

  qc2.addOrCondition('sys_class_name', 'sc_request');

  qc2.addOrCondition('sys_class_name', 'u_pir_task');

  gr.addActiveQuery();

  gr.addQuery('opened_by', gs.getUserID());

  gr.orderByDesc('sys_updated_on');

  gr.setLimit(5);

      gr.query();

  data.count2 = gr.getRowCount();

      while(gr.next()) {

              var pir = {};

  var dtReq = gr.getDisplayValue('opened_at');

  var dtOnly = dtReq.substr(0,10)

  var formType = gr.sys_class_name;

  if(formType=='sc_request'){

pir.stage = gr.getDisplayValue('request_state');

  }else if((formType=='u_pir')) {

    pir.stage = gr.getDisplayValue('state');

  }

  console.log('stage=' + gr.request_state);

    console.log('formType' + formType);

              pir.number = gr.getDisplayValue('number');

              pir.daterequest = dtOnly;

              pir.sys_id = gr.getUniqueValue();

              pir.sys_updated_on = gr.getValue('sys_updated_on');

              data.pirs.push(pir);

      }

})();

Greatly   appreciate any help. Thanks

8 REPLIES 8

No worries, thanks Chuck.


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

I noticed gr is a task object and request_state only exists on the sc_request table.   That may be causing your issue.   You may need to initiate a new GlideRecord of type sc_request to get the value.


Actually after doing a little digging, there is a business rule called "Set Request State" running on the sc_request table that is setting it to in_process:


https://INSTANCE-NAME.service-now.com/nav_to.do?uri=sys_script.do?sys_id=19a9ecb40a0a0a65013c62ab86e...


Thanks Michael for the reply.



I gave it a try and got same result



if(formType=='sc_request'){


  //get state of sec_item


  var scgr = new GlideRecord('sc_request');


  console.log('sys_id='+ gr.getUniqueValue());


  scgr.addQuery('sys_id', gr.getUniqueValue());


  scgr.query();


  console.log('here 2');


  // if(scgr.hasNext()){


  pir.stage = scgr.getDisplayValue('request_state');


        console.log('val=' + scgr.getDisplayValue('request_state'))


  // }



  }else if((formType=='u_pir')) {


    pir.stage = gr.getDisplayValue('state');


  }



I also realized that sc_request table is extended from the Task table that's probably why i'm getting the same behaviour..




Regina