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

Sandeep Rajput
Tera Patron
Tera Patron

@Lon Landry4 use JSON.parse(stringJSON); to convert string to an object and access the individual property by dot walking for e.g. 

var answer = '{"first_name":"John"}';
answer= JSON.parse(answer);
var firstName = answer.first_name;

Hope this helps.

 

 

I tried in my script.

But, var firstName = answer.first_name;

results in John (or in my case the sys_id) mentioned in script as opposed to a value from string.

@Lon Landry4 

In your case you can access the attributes as follows.

 

var submitted_for = answer[0].tb_submitted_for; //will give you sys_id of submitted for

var state = answer[0].state_submitted_for; //value of state

var submitted_for = answer[0].assets_assigned_to_submitted_for; //will give you sys_id of assigned_to

You can also use a for loop to access these values one by one.

Error while running Client Script "set certification_results": TypeError: Cannot read properties of undefined (reading 'assets_assigned_to_submitted_for')