Record producer variable vs REST API using get method

salu
Mega Guru

bawiskar

bryanbarnard

Hello,

I have record producer and contain a variable Carton Number.

When the user enters the Carton number in Record producer, the code   would need to take the carton number, plug it into this URL and process the json response and fill the value in rest of the field.

script include

sample_http_request : function(){

  //var responseBody='[{"olpnId":"DTR000749357","dfcNbr":"6707","channelDesc":"COM : RDC","rdcNbr":"5120","storeNbr":"4116","customerName":"JASON HOLLIS (412)824-9406","orderId":"TX85658","omsOrderNbr":"16592175","webOrderNbr":"W494870456","orderStatus":"190 : Shipped","shipDateTime":"2016-06-02 14:13:09"}]';

//Sample json response

  var carton_number = this.getParameter('sysparm_carton_number');

  var r = new sn_ws.RESTMessageV2('getCarton Number', 'get');

  r.setStringParameter('olpn', carton_number);

  var response = r.execute();

  var responseBody = response.getBody();

  var httpStatus = response.getStatusCode();

  var obj = JSON.parse(responseBody);

  var len = obj.length;

  var obj2 = [];

  for(i=0; i<len; i++){

  var u_olpnId = obj[i].olpnId;

  var u_dfcNbr= obj[i].dfcNbr;

  var u_channelDesc = obj[i].channelDesc;

  var u_rdcNbr = obj[i].rdcNbr;

  var u_storeNbr = obj[i].storeNbr;

  var u_customerName = obj[i].customerName;

  var u_orderId= obj[i].orderId;

  var u_omsOrderNbr = obj[i].omsOrderNbr;

  var u_webOrderNbr= obj[i].webOrderNbr;

  var u_orderStatus = obj[i].orderStatus;

  var u_shipDateTime= obj[i].shipDateTime;

  obj2[i] = {olpnId:u_olpnId, dfcNbr: u_dfcNbr, channelDesc : u_channelDesc, rdcNbr: u_rdcNbr, storeNbr: u_storeNbr, customerName: u_customerName,orderId : u_orderId, orderStatus: u_orderStatus, omsOrderNbr: u_omsOrderNbr, webOrderNbr: u_webOrderNbr,shipDateTime: u_shipDateTime,};

  }

  var json = new JSON();

  var data = json.encode(obj2);//JSON formatted string

  return data;

On change client script

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading || newValue == '') {

  return;

  }

  var gah = new GlideAjax('getCartonNumber');

  gah.addParam('sysparm_name','sample_http_request');

  gah.addParam('sysparm_carton_number',g_form.getValue('u_carton_number'));

  gah.getXML(ajaxResponse);

  function ajaxResponse(response) {

  try{

  var answer = response.responseXML.documentElement.getAttribute("answer");

  // get result element and attributes

  //alert('HEY');

 

  //alert(answer);

  answer = answer.evalJSON();

  for(var i = 0; i < answer.length; i++) {

  var u_olpnId = answer[i].olpnId;

  var u_dfcNbr= answer[i].dfcNbr;

  var u_channelDesc = answer[i].channelDesc;

  var u_rdcNbr = answer[i].rdcNbr;

  var u_storeNbr = answer[i].storeNbr;

  var u_customerName = answer[i].customerName;

  var u_orderId= answer[i].orderId;

  var u_omsOrderNbr = answer[i].omsOrderNbr;

  var u_webOrderNbr= answer[i].webOrderNbr;

  var u_orderStatus = answer[i].orderStatus;

  var u_shipDateTime= answer[i].shipDateTime;

  //alert('feb 10 '+u_dfcNbr);

  g_form.setValue('dfc',u_dfcNbr);

  g_form.setValue('channelDesc',u_channelDesc);

  g_form.setValue('rdcNbr',u_rdcNbr);

  g_form.setValue('storeNbr',u_storeNbr);

  g_form.setValue('customerName',u_customerName);

  g_form.setValue('orderId',u_orderId);

  g_form.setValue('omsOrderNbr',u_omsOrderNbr);

  g_form.setValue('webOrderNbr',u_webOrderNbr);

  g_form.setValue('orderStatus',u_orderStatus);

  g_form.setValue('shipDateTime',u_shipDateTime);

  g_form.setValue('dfcNbr',name);

  g_form.setValue('channelDesc',email);

  g_form.setValue('rdcNbr',phone);

  g_form.setValue('storeNbr',cont);

  }

  }catch(e){

  //alert('EEEEE ' + e);

  }

  }

  //Type appropriate comment here, and begin script below

}

How can I set the up the end point in the REST message?

find_real_file.png

1 ACCEPTED SOLUTION

Hi Saranya,



In the endpoint of get method under Rest Message have this as endpoint this will serve as dynamic and send this value to script include via ajax and use setParameter method


https://xxx.apps-np.xxx.com/DFCOrderInfo?olpn=${olpn}



var olpn = this.getParameter(); // this you will be sending from client script


r.setStringParameter('olpn', olpn);



This means this olpn value will be dynamic.


Under variable substitution for get method create new and add this in value and name


name = olpn


value = olpn


screenshot below



rest-variable.JPG



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Hello Ankur,


Thank you for the help.IT got worked


Thanks


Saranya


Hi Saranya,



Great. Could you also mark the answer as helpful and hit like. Thanks in advance.



Regards


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Saranya,



Thanks for marking the answer as correct and helpful. It would be great if you endorse the content so that it has high visibility. Thanks in advance.



Regards


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader