Skip to content

Commit e282fc7

Browse files
authored
Fixed and updated 'Use setValue()'s displayValue Parameter with Reference Fields' Linter check (#91)
1 parent 726bcf6 commit e282fc7

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
L05SDNFQIepqo45ck9oJnUfhlsshR11vQgHkD4wzOpQrjSZFe2NZzwMgkg2NK1YTICOOOv9il_jnQzongixvCmUzcYXEiReSfy6hsdaKsnOnwgxfVh_WDZUYObwLcQTIT7RzE-YnPpL46yHDJKAWzdbf2iSx50xMlOhS4AiJ2900eVr8YYQvs9sCQlY3LNyYaInmgwKuyYBjQ4RXsf-ZEylc4349-ksoBu0sekZLqZfHDoXcwInrlzJloTWPGfu35AE8BuLUYIiGM8ykvB4a9ODOrXDuqF4VqWFzhFSoCVvvfsXUGx4flSVfJmHSuTc2KKjuMZKy6CS62M506U8XPJbbJroJ57XvpgjpGhtH2eaODZj1GGMymQhbrrgwqTfaxusIANtx9ZaD4LkMh12IGwTi397o1Hcvjr34hnpU_jDPYFo0UOBpnoofdJE7y_UHjVZSsmqXow_TblntZ_wQ5UA0_qVkOpryeAl5mBR9aI4t-Y1jB7N-Lr7vdoal69U0DB0KUuvY5LQxPPQXC_IG_yrdz4pRTlKx9EgzGelNfUsl6d9QsMFfKBZ0mpS7HWUg_TRX_A8-UHCEYlEUr55dlfVKAZVtOqfw_H6kiVeHtxONNOTxoEioALWazwelFxc9IEnSkugs6W7fFYKwNV4neZRRgOe6SN2oAG_tkZUojUc
1+
us24VYwzDRWhd4Oah1VDlP_Gi4_4M9btMV4eKYPBm8Zzc5LaRJUAxHANj7wFAl6t57zgJ_lTtkOUFkFVYMcCjHvli38HPThQ3BmHXwvVFRmHpRPOZZvjmuzxlyH07Mn4mno2LWxtQ7ndCMMnos7H7PqTryMbkNY5-I4kaudTZRcnVMRYXeBAjt0rmsycqK4CxWDQ191IKlHMrCTKXowvpYWfPzh2czKYc8V9KE7ThxisuEYXUEpnOp_n73KAqqpdMdo0WvugM451Xp8hSNpOziQLVxGiQ20jNQcEU1_Q-4Ic-Kq2CHZb_BHaLPKPttxvwUqHtoTDE6o84VW7BtvYMKbhdWRdnv9mVBGw9O_xT001SB8ZnRHW_wl8pst-oMsR3Sr8oKfrUfE3mXUSF3dfj4U6wwUj9TSJ-OC0nYg0_EzeaNq_9ZrQHd87kvKnkB1wkq_C3OYrC6ojWRoiEZUcyox4DRRkub7hCYIoRvynx0uL6x1DM-ZxR3cwtzZYNv3eSTVB55mtWnD8jz0RQ8RZYNwkoNJE22J6cBG3FsL9SYtU6brPGg7Ln6tbRTfGIBL5EynQII5ZCVo0-geMItmYfGLc4vCZbeI_FuDQJmvhWgX0OaMWwJO3aSzO4rQ0RQH8zidCOOybwX4F6ThDnM9uDqjC1LgME_ZpthcJPKl9sWk

ca8467c41b9abc10ce0f62c3b24bcbaa/update/scan_linter_check_85c352ae2f3db11002eb2ca62799b68e.xml

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,82 @@
22
<scan_linter_check action="INSERT_OR_UPDATE">
33
<active>true</active>
44
<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.&#13;
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>
78
<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>
910
<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.&#13;
12+
&#13;
13+
Example: &#13;
14+
&#13;
15+
Instead of &#13;
16+
// Client needs to go back to the server to&#13;
17+
// fetch the name that goes with this ID&#13;
18+
g_form.setValue('assigned_to', id); &#13;
19+
&#13;
20+
Use&#13;
21+
// No server call required&#13;
22+
g_form.setValue('assigned_to', id, name); </resolution_details>
1123
<run_condition/>
1224
<score_max>100</score_max>
1325
<score_min>0</score_min>
1426
<score_scale>1</score_scale>
1527
<script><![CDATA[(function(engine) {
1628
1729
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+
}
3065
});
3166
3267
})(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>
3469
<sys_class_name>scan_linter_check</sys_class_name>
3570
<sys_created_by>admin</sys_created_by>
3671
<sys_created_on>2023-10-13 13:18:39</sys_created_on>
3772
<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>
4075
<sys_package display_value="Example Instance Checks" source="x_appe_exa_checks">ca8467c41b9abc10ce0f62c3b24bcbaa</sys_package>
4176
<sys_policy/>
4277
<sys_scope display_value="Example Instance Checks">ca8467c41b9abc10ce0f62c3b24bcbaa</sys_scope>
4378
<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>
4681
</scan_linter_check>
82+
<sys_translated_text action="delete_multiple" query="documentkey=85c352ae2f3db11002eb2ca62799b68e"/>
4783
</record_update>

0 commit comments

Comments
 (0)