|
2 | 2 | <scan_linter_check action="INSERT_OR_UPDATE">
|
3 | 3 | <active>true</active>
|
4 | 4 | <category>performance</category>
|
5 |
| - <description>When using setValue() on a reference field, be sure to include the display value with the value (sys_id). If you set the value without the display value, ServiceNow does a synchronous Ajax call to retrieve the display value for the record you specified. This extra round trip to the server can leave you at risk of performance issues.</description> |
6 |
| - <documentation_url>https://developer.servicenow.com/dev.do#!/guides/tokyo/now-platform/tpb-guide/client_scripting_technical_best_practices</documentation_url> |
| 5 | + <description>When using setValue() on a reference field, be sure to include the display value with the value (sys_id). If you set the value without the display value, ServiceNow does a synchronous Ajax call to retrieve the display value for the record you specified. This extra round trip to the server can leave you at risk of performance issues. |
| 6 | +</description> |
| 7 | + <documentation_url>https://developer.servicenow.com/dev.do#!/guides/tokyo/now-platform/tpb-guide/client_scripting_technical_best_practices#use-setvalues-displayvalue-parameter-with-reference-fields</documentation_url> |
7 | 8 | <finding_type>scan_finding</finding_type>
|
8 |
| - <name>Using setValue()'s displayValue Parameter with Reference Fields</name> |
| 9 | + <name>Use setValue()'s displayValue Parameter with Reference Fields</name> |
9 | 10 | <priority>3</priority>
|
10 |
| - <resolution_details>Using the third parameter, where display name of the reference record can be passed.</resolution_details> |
| 11 | + <resolution_details>Using the third parameter, where display name of the reference record can be passed. |
| 12 | + |
| 13 | +Example: |
| 14 | + |
| 15 | +Instead of |
| 16 | +// Client needs to go back to the server to |
| 17 | +// fetch the name that goes with this ID |
| 18 | +g_form.setValue('assigned_to', id); |
| 19 | + |
| 20 | +Use |
| 21 | +// No server call required |
| 22 | +g_form.setValue('assigned_to', id, name); </resolution_details> |
11 | 23 | <run_condition/>
|
12 | 24 | <score_max>100</score_max>
|
13 | 25 | <score_min>0</score_min>
|
14 | 26 | <score_scale>1</score_scale>
|
15 | 27 | <script><![CDATA[(function(engine) {
|
16 | 28 |
|
17 | 29 | engine.rootNode.visit(function(node) {
|
18 |
| - if (node.getTypeName() === "STRING") { |
19 |
| - var text = node.toSource(); |
20 |
| - |
21 |
| - // looks for all occurrences of g_form.setvalue() and verifies it has all the relevant |
22 |
| - // paramters, which includes display name to avoid the synchronous ajax call |
23 |
| - var pattern = /g_form\.setValue\(([^,]*),([^,]*)\)/gm; |
24 |
| - var regExp = new RegExp(pattern); |
25 |
| - var matchFound = regExp.test(text); |
26 |
| - if (matchFound){ |
27 |
| - engine.finding.incrementWithNode(node); |
28 |
| - } |
29 |
| - } |
| 30 | +
|
| 31 | + // Find an occurrence of the setValue function |
| 32 | + if (node.getNameIdentifier() && node.getNameIdentifier() === 'setValue') { |
| 33 | +
|
| 34 | + // Walk up the AST tree to check if function is called on g_form |
| 35 | + // We want to ignore other uses like setValue on GlideRecord |
| 36 | + if (node.getParent().getTypeName() === 'GETPROP' && |
| 37 | + node.getParent().toSource() === 'g_form.setValue') { |
| 38 | +
|
| 39 | + var argsProvided = 0; |
| 40 | +
|
| 41 | + // Walk up to grandparent to check for the arguments provided |
| 42 | + node.getParent().getParent().visit(function(childnode) { |
| 43 | + // Case 1: Finds argument specified as a string, number, or boolean |
| 44 | + // eg. g_form.setValue('field', 'value'); |
| 45 | + if (childnode.getTypeName() === 'STRING' || childnode.getTypeName() === 'NUMBER' || |
| 46 | + childnode.getTypeName() === 'TRUE' || childnode.getTypeName() === 'FALSE') { |
| 47 | + argsProvided++; |
| 48 | + } |
| 49 | + // Case 2: Find argument specified as a variable |
| 50 | + // eg. g_form.setValue('field', valueVariable); |
| 51 | + else if (childnode.getTypeName() === 'NAME' && childnode.getNameIdentifier() && |
| 52 | + childnode.getNameIdentifier() !== 'g_form' && |
| 53 | + childnode.getNameIdentifier() !== 'setValue') { |
| 54 | + argsProvided++; |
| 55 | + } |
| 56 | +
|
| 57 | + }); |
| 58 | + // Check to make sure the g_form.setValue call has exactly 3 arguments |
| 59 | + // eg. g_form.setValue('field', 'value', 'displayValue') |
| 60 | + if (argsProvided != 3) { |
| 61 | + engine.finding.incrementWithNode(node); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
30 | 65 | });
|
31 | 66 |
|
32 | 67 | })(engine);]]></script>
|
33 |
| - <short_description>Using setValue()'s displayValue Parameter with Reference Fields</short_description> |
| 68 | + <short_description>Use setValue()'s displayValue Parameter with Reference Fields</short_description> |
34 | 69 | <sys_class_name>scan_linter_check</sys_class_name>
|
35 | 70 | <sys_created_by>admin</sys_created_by>
|
36 | 71 | <sys_created_on>2023-10-13 13:18:39</sys_created_on>
|
37 | 72 | <sys_id>85c352ae2f3db11002eb2ca62799b68e</sys_id>
|
38 |
| - <sys_mod_count>2</sys_mod_count> |
39 |
| - <sys_name>Using setValue()'s displayValue Parameter with Reference Fields</sys_name> |
| 73 | + <sys_mod_count>3</sys_mod_count> |
| 74 | + <sys_name>Use setValue()'s displayValue Parameter with Reference Fields</sys_name> |
40 | 75 | <sys_package display_value="Example Instance Checks" source="x_appe_exa_checks">ca8467c41b9abc10ce0f62c3b24bcbaa</sys_package>
|
41 | 76 | <sys_policy/>
|
42 | 77 | <sys_scope display_value="Example Instance Checks">ca8467c41b9abc10ce0f62c3b24bcbaa</sys_scope>
|
43 | 78 | <sys_update_name>scan_linter_check_85c352ae2f3db11002eb2ca62799b68e</sys_update_name>
|
44 |
| - <sys_updated_by>admin</sys_updated_by> |
45 |
| - <sys_updated_on>2023-10-13 14:49:20</sys_updated_on> |
| 79 | + <sys_updated_by>nia.mccash</sys_updated_by> |
| 80 | + <sys_updated_on>2023-10-13 16:36:09</sys_updated_on> |
46 | 81 | </scan_linter_check>
|
| 82 | + <sys_translated_text action="delete_multiple" query="documentkey=85c352ae2f3db11002eb2ca62799b68e"/> |
47 | 83 | </record_update>
|
0 commit comments