I need to copy the last 7 characters of the RITM number into a new field I created on sc_req_item

Suzanne Coffman
Tera Guru

Based on this post Solved: Flow Designer - adding "substring to remove last c... - ServiceNow Community

and others I have tried a number of approaches to a script in the Update Requested Item Record action of a fb flow to copy the last 7 characters (the numeric part) of the RITM number into a new field I created on the sc_req_item table called u_contractor_id.

 

I am new to scripting

 

I have tried multiple variations of the following:

return substring(fd_data.trigger.request_item.number,4,7)
 
and
var a = fd_data.trigger.request_item.number;
//var st = a.toString(); it should already be a string?
var cid = a.substring(4,7)
//gs.print(cid);
 
Suggestions?  
1 ACCEPTED SOLUTION

Can you show your configuration? 
Please compare it with this (working) example:

MarkusKraus_1-1684473435993.png

Note that the applied transformations are as follows:

1.) Substring (Start Index=4, Leave End Index empty)

2.) Convert String to Number (No arguments)

 

 

 

View solution in original post

5 REPLIES 5

Markus Kraus
Kilo Sage
var a = fd_data.trigger.request_item.number.toString();
var cid = a.substr(a.length - 7);
gs.info(cid);

Note: You can perform this using a string transformation in the flow designer, no scripting needed.

Note2: If you only want "skip" the "RITM" and the leading zeroes, you can also accomplish this by using flow designer transforms: 1.) substring to remote the RITM 2.) cast to integer (this will automatically remove the leading zeroes) 3.) cast to string again.

Thank you that was helpful.  Using transform functions makes sense.  Both the number field and my custom field are string fields, so it seems like just the substring function would be enough without converting to a number then back to a string?  But when I use the string transform with start index 4 and end index null  and test it I get an "is not a valid record." error. 

Can you show your configuration? 
Please compare it with this (working) example:

MarkusKraus_1-1684473435993.png

Note that the applied transformations are as follows:

1.) Substring (Start Index=4, Leave End Index empty)

2.) Convert String to Number (No arguments)

 

 

 

Amit Gujarathi
Giga Sage
Giga Sage

HI @Suzanne Coffman ,
I trust you are doing great.
To copy the last 7 characters (numeric part) of the RITM number into a new field called "u_contractor_id" on the sc_req_item table, you can use the following script in the "Update Requested Item Record" action of your Flow Designer flow:

var ritmNumber = fd_data.trigger.request_item.number;
var contractorId = ritmNumber.substring(ritmNumber.length - 7);
current.u_contractor_id = contractorId;

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi