How do I insert a record into a table and grab values from that specific record?

willkara
Kilo Contributor

So I'm creating some of the backing fields for an order-able catalog item that'll go through a workflow. I'm trying to create the first step that generates/holds the request information.

One field that I need is an auto-incrementing value inside a certain table that simply tracks the Request ID and who ordered it.

Request Table

Header 1Header 2
Request IDRequested By

So when a user wants to order an item, a record gets created in this table with the sys_id of who ordered it an it then increments the request ID by 1.

i.e:

  1. Will orders a new item,
  2. A new row is created in Request Table
    1. Request ID gets incremented by 1
    2. Requested By becomes the sys_id of the current user.

I then need to grab the new Request ID to insert it into a workflow payload (for a soap call).

Is there a way to do this in a workflow? My guess is that it needs to be done in a Run Script block, but I'm not sure on how to write it.

Here's what I have of the script so far,

/*

This script generates and stores the Request and Order/Instance numbers for the payload and the rest of the workflow.

*/

var workflow.scratchpad.requestNumber;

var workflow.scratchpad.orderNumber;

var workflow.scratchpad.instanceNumber;

var numbering = new GlideRecord('u_request_instance');

numbering.initialize();

numbering.u_requested_by = current.request.opened_by;

numbering.insert();

//I then need to grab that new request_id value from the u_request_instance table.

1 ACCEPTED SOLUTION

venkatiyer1
Giga Guru

Hi William,



var numbering = new GlideRecord('u_request_instance');



numbering.initialize();


numbering.u_requested_by = current.request.opened_by;


var sys_id = numbering.insert();



if(numbering.get(sys_id )) {


var request_id =   numbering.request_id;


}



// request_id should hold your request id value


View solution in original post

7 REPLIES 7

venkatiyer1
Giga Guru

Hi William,



var numbering = new GlideRecord('u_request_instance');



numbering.initialize();


numbering.u_requested_by = current.request.opened_by;


var sys_id = numbering.insert();



if(numbering.get(sys_id )) {


var request_id =   numbering.request_id;


}



// request_id should hold your request id value


Venkat,



Thanks for the assist! I'll give it a try and see what I come up with.



- Will


lks
ServiceNow Employee
ServiceNow Employee

Hello William,



I am just expanding your given script with few more statements to achieve this objective:



var numbering = new GlideRecord('u_request_instance');



numbering.initialize();


numbering.u_requested_by = current.request.opened_by;


var new_rec = numbering.insert() + '';


var req_id = ''



Option 1: Re-using the 'numbering' object it's already declared above


========


if(numbering.get(id)){


req_id = numbering.request_id + '';//here you find the request_id


}



Option 2: Same as Venkat suggested.


lks
ServiceNow Employee
ServiceNow Employee

Hello William,



Are you able to make any progress and achieve your objective?


Let us know when you need further assistance!