Functions are not working with Name/values input inside script step for custom Flow Action

Nilesh Wahule
Tera Guru

Hi All,

Hope everyone is enjoying working in ServiceNow 😊.

 

I am developing a custom flow action which will react based on Name/ Values inputs datatype. as shown below

 

NileshWahule1_0-1676974370406.png

which will eventually return a Object with same data inside script step. 

 

NileshWahule1_1-1676974496656.png

I have verified this object inside the logs and sending data as expected :

{"Name":"Nilesh","Dept":"SNOW"} 

 

But when I am trying to use any function inside the script step for keys and values operations as follows, its giving unexpected result :

 

NileshWahule1_2-1676974746228.png

 

The output of this script should be like -

Name=Nilesh Dept=SNOW 

but when action test is taken it shows different things as follow

 

replaceAll= function (from, to)

{

return this.replace(from.toString(), to, "g");

}

 

NileshWahule1_3-1676974966236.png

 

 

Also, when I am performing same operation through background script, I can see expected results.

 

I am new to developing the custom flow actions, but I still wonder how this is happening 😐.

 

 

Please let me know if I am doing anything wrong in syntax or some methods. 

 

Thanks in Advance.

 

 

#serviceNow #flow

1 ACCEPTED SOLUTION

Nilesh Wahule
Tera Guru

Hi All,

Thank you for your time and efforts, as we couldn't find anything related towards the solution of this issue. we did raise HI ticket and the issue is solved. 

Following were the discussion points,

1. When we use flow data pill to populate user field data population, the text will be in its original form without escaping i.e. if user has entered any special characters inside field, that will be as it is, and may cause problems while you perform next operations. 

e.g. User has entered data in address field which is multiline in the following format 

At post A'bad

Dist Aurangabad , MH''- IN

 

as this data is multiline and having special characters we should first escape it so that the special characters from the sequence will be treated as simple string characters.

Know more about the escape Sequence 

 

2. There is OOB JavaScript utility provided by ServiceNow to esacpe and unescape the string text called as JSUtil. this script include helps preventing the breaking into the strings cause of specials symbols, you can use it as follow 

JSUtil.escapeText(String) and then serialize and JSUtil.unescapeText(String) and then de-serialize. 

Util URI: https://<<YOUR INSTANCE ADDRESS>>.service-now.com/sys_script_include.do?sys_id=8d5571660a0a0b5000fc9...

Note : Please take full API name if you are working for scoped Applications.

 

3. For better user performance and better efficiency, we should restrict fields on client side itself, use an onChange or onSubmit Client Script to capture the special characters

 e.g. refer to this Community post

 

 

At the end coming to main problem, How I am able to pass correct string inside the flow action input is, I have created a flow variables for all these fields which may contain a special characters and then using JSUtil serialized it. At the end instead of passing simple data from field , I passed the flow variable with escaped text.

 

Hope this will help for you all.

 

 

View solution in original post

9 REPLIES 9

Hi @Ankur Bawiskar ,

 

Business is using one standard format of table for showing all the necessary details as below while showing Task/Request on portal :

 

NileshWahule1_0-1677244867289.png

This table is filled in rich_description field to map to details in portal side. But as you can see the styling for table rows is not fixed and the number of rows populated in table is based on HR services. 

 

So we decided to develop a custom flow action where we can simply pass the field and Value pairs using flow Data pickers and inside custom action will handle rest of the things i.e. Constructing HTML and adding styles for table. for that purpose we need functions to construct each row.

 

I hope now its clear to you why we need to use functions inside flow designer with dynamic outputs.

 

 

Please let me know if you need more details.

 

 

Hi @Ankur Bawiskar @Chuck Tomasi ,

 

There is one improvement in reaching to solutions, I have verified the type of the Input.fields ( Action Input ) which is string with Escape Character as below :

                    {\"parameter\":\"dgfhj\",\"parameter1\":\"gfhj\"}

 

Now as this is string , we can simply use JSON.parse() to convert into JSON object but the problem is escape characters i.e. "\. So we need some way to remove this string escape chars and have only JSON string so that it will be parsed by JSON.

 

Its showing this error =  Error: Unexpected token in object literal,Detail: Unexpected token in object literal

 

Please let me know if there is anything else we need to follow and handle.

 

 

 

I have the exact same use case and issue as this. Did you find a solution?

Sorry, I see that you have provided the solution although I'm not quite clear on the use of JSUTIL here to try turn the name/values input into a parseable object.

 

Would you be able to provide your code as an example please?

Nilesh Wahule
Tera Guru

Hi All,

Thank you for your time and efforts, as we couldn't find anything related towards the solution of this issue. we did raise HI ticket and the issue is solved. 

Following were the discussion points,

1. When we use flow data pill to populate user field data population, the text will be in its original form without escaping i.e. if user has entered any special characters inside field, that will be as it is, and may cause problems while you perform next operations. 

e.g. User has entered data in address field which is multiline in the following format 

At post A'bad

Dist Aurangabad , MH''- IN

 

as this data is multiline and having special characters we should first escape it so that the special characters from the sequence will be treated as simple string characters.

Know more about the escape Sequence 

 

2. There is OOB JavaScript utility provided by ServiceNow to esacpe and unescape the string text called as JSUtil. this script include helps preventing the breaking into the strings cause of specials symbols, you can use it as follow 

JSUtil.escapeText(String) and then serialize and JSUtil.unescapeText(String) and then de-serialize. 

Util URI: https://<<YOUR INSTANCE ADDRESS>>.service-now.com/sys_script_include.do?sys_id=8d5571660a0a0b5000fc9...

Note : Please take full API name if you are working for scoped Applications.

 

3. For better user performance and better efficiency, we should restrict fields on client side itself, use an onChange or onSubmit Client Script to capture the special characters

 e.g. refer to this Community post

 

 

At the end coming to main problem, How I am able to pass correct string inside the flow action input is, I have created a flow variables for all these fields which may contain a special characters and then using JSUtil serialized it. At the end instead of passing simple data from field , I passed the flow variable with escaped text.

 

Hope this will help for you all.