Get all variable values from a string in a client side script

Lon Landry4
Mega Sage

In the string below, I want to pull only the values (in bold) for assets_assigned_to_submitted_for.

 

"[{\"tb_submitted_for\":\"4cfb00b0c3d599d07ee3251ce0013178\",\"state_submitted_for\":\"In use\",\"assets_assigned_to_submitted_for\":\"b142f66d374843809bfbdaa543990ea2\"},

 

{\"tb_submitted_for\":\"4cfb00b0c3d599d07ee3251ce0013178\",\"state_submitted_for\":\"Review\",\"assets_assigned_to_submitted_for\":\"6c2bd5851b640c90e7d6620f6e4bcb5c\"},

 

{\"tb_submitted_for\":\"4cfb00b0c3d599d07ee3251ce0013178\",\"state_submitted_for\":\"Review\",\"assets_assigned_to_submitted_for\":\"3c4d400a372243046b75d5c543990ee1\"}]"

 

The number of rows will vary...

The solution is for a client script.

1 ACCEPTED SOLUTION

@Lon Landry4 Please update your script as follows.

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var assetsIcertify = g_form.getValue('u_returning_asset_submitted_for');
var assetsIcert = JSON.parse(assetsIcertify);
var submitted_for1 = assetsIcert[0].assets_assigned_to_submitted_for; //will give you sys_id of assigned_to
g_form.setValue('certification_results',submitted_for1);
return;
}

View solution in original post

11 REPLIES 11

@Lon Landry4 Could you please post your script here in the thread.

function onSubmit() {
var assetsIcertify = g_form.getValue('u_returning_asset_submitted_for');
g_form.setValue('certification_results',assetsIcertify);
return;
}

1. Script to test that data is present
above script returns (UPDATED)

[{"tb_submitted_for":"4cfb00b0c3d599d07ee3251ce0013178","state_submitted_for":"In use","assets_assigned_to_submitted_for":"b142f66d374843809bfbdaa543990ea2"},{"tb_submitted_for":"4cfb00b0c3d599d07ee3251ce0013178","state_submitted_for":"Review","assets_assigned_to_submitted_for":"6c2bd5851b640c90e7d6620f6e4bcb5c"}]

So, the data I need is here...(I am testing in Service Portal)

 

2. new script results in error  assets_assigned_to_submitted_for is undefined

function onSubmit() {
//Type appropriate comment here, and begin script below
var assetsIcertify = g_form.getValue('u_returning_asset_submitted_for');
const assetsIcert = JSON.stringify(assetsIcertify)
var answer = {"assets_assigned_to_submitted_for":"b142f66d374843809bfbdaa543990ea2"}
var submitted_for1 = answer[0].assets_assigned_to_submitted_for; //will give you sys_id of assigned_to
g_form.setValue('certification_results',submitted_for1);
return;
}

---end of script---

I have tried regex, sessionStorage, and 154 script possibilities.

@Lon Landry4 Please update your script as follows.

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var assetsIcertify = g_form.getValue('u_returning_asset_submitted_for');
var assetsIcert = JSON.parse(assetsIcertify);
var submitted_for1 = assetsIcert[0].assets_assigned_to_submitted_for; //will give you sys_id of assigned_to
g_form.setValue('certification_results',submitted_for1);
return;
}

Thanks for your help in time with this. I will mark it correct as soon as I work out the looping.
I can return the number of rows, so I must be close...

Any idea why this loop does not work?
function onSubmit() {
//Type appropriate comment here, and begin script below
var assetsIcertify = g_form.getValue('u_returning_asset_submitted_for');
var assetsIcert = JSON.parse(assetsIcertify);
var submitted_for1 = assetsIcert[0].assets_assigned_to_submitted_for; //will give you sys_id of assigned_to
var i = 0;
var done = false;
while(!done){
if (i >= 1){
++i;
g_form.setValue('certification_results',submitted_for1);
continue;
}
done=true;
return;
}}