- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2020 11:00 AM
Hey folks,
I'm looking for help with coding a for loop or any other suggestion you think would be a better solution!
I have a record producer that is putting about a dozen different variables values into the description field with:
current.business_case += '[VARIABLE_LABEL] ' + producer.[VARIABLE_NAME].getDisplayValue() + '\n\n';
The update requirement is to display the empty/null variables at the bottom of the field. To that end I'm thinking of re-writing it by creating a defined list of variables that I can then iterate through with a for loop. Within that loop I'll check if it's empty or not, and place it in the appropriate order.
Something like (pseudocode):
// EITHER
var varListArray = {};
// OR
var varListFieldName = '[VARIABLE_NAME1], [VARIABLE_NAME2], [VARIABLE_NAME3]'
var populatedList = '';
var emptyList = '';
for (loop) {
if (arrayVariable == '') {
emptyList += '[FIELD_POSITION0_LABEL] ' + producer.[FIELD_POSITION0_NAME].getDisplayValue() + '\n\n';
emptyList += '[FIELD_POSITION1_LABEL] ' + producer.[FIELD_POSITION1_NAME].getDisplayValue() + '\n\n';
} else {
populatedList += '[FIELD_POSITION0_LABEL] ' + producer.[FIELD_POSITION0_NAME].getDisplayValue() + '\n\n';
populatedList += '[FIELD_POSITION1_LABEL] ' + producer.[FIELD_POSITION1_NAME].getDisplayValue() + '\n\n';
}
}
current.business_case += populatedList;
current.business_case += emptyList;
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2020 09:00 AM
I've made some headway on this, but the first function (createNewRequest) is not running the script after the array declaration.
The second function (createNewIncident) works with the exception of one variable I haven't identified returning 'undefined'.
Any insights as to what is going on?
// SN Item Testing
current.u_portfolio = 'a5105f6030bddec4b2e187ae8224f9fe'; // IT-Service Excellence
current.u_product = 'cf91911a68651140b2e123719f97b7cc'; // ServiceNow
current.u_theme = '557691da68651140b2e123719f97b71b'; // Service Request / Catalog
current.assigned_to = producer.vs_assignee;
current.submitter = producer.vs_requested_by_item;
if (producer.type_of_request == 'new') {
if (producer.v_type_of_catalog_item == 'request') {
createNewRequest();
} else if (producer.v_type_of_catalog_item == 'incident') {
createNewIncident();
}
}
if (producer.type_of_request == 'update') {
updateItem();
}
if (producer.type_of_request == 'retire') {
retireItem();
}
function createNewRequest() {
davLog('Running createNewRequest', 'awReqTest');
current.short_description = 'Request to Build a Catalog Item in ServiceNow \n';
davLog('Var Test: ' + producer.type_of_request, 'awReqTest');
// Define the variables to display in the description field of the idea record
var variables = [producer.type_of_request, producer.v_type_of_catalog_item, producer.v_add_contact, producer.v_suggested_name, producer.v_volume, producer.v_short_description, producer.v_approvals_needed, producer.v_description, producer.v_describe_approval, producer.v_describe_customer, producer.v_live, producer.v_delivery_time, producer.v_tasks_needed, producer.v_task_details, producer.v_task_description, producer.v_task_short_description, producer.v_format, producer.v_custom_notif, producer.v_describe_variables, producer.v_altered, producer.v_job_aids_referenced, producer.v_job_aids_updated, producer.v_marketed, producer.v_job_aids_created, v_logo];
davLog('After variables array', 'awReqTest');
davLog('Variables string:' + variables.toString(), 'awReqTest');
davLog('Variables length:' + variables.length(), 'awReqTest');
// Variables to hold the variable list
var populatedList = '';
var emptyList = '';
// Reference fields - '.getQuestion()' does not cooperate with reference fields
var mainContact = producer.v_main_contact.getDisplayValue(); // 'Main Contact: ' + producer.v_main_contact.getDisplayValue() + '\n\n';
var ci = producer.v_cmdb_ci.name; // 'Configuration Item: ' + producer.v_cmdb_ci.name + '\n\n';
var taskAssignment = producer.task_assignment.getDisplayValue(); // 'Task Assignment Group: ' + producer.task_assignment.getDisplayValue() + '\n\n';
davLog('Reference fields: ' + '\n\n' + 'Main Contact: ' + mainContact + '\n\n' + 'CI: ' + ci + '\n\n' + 'Task Assignment: ' + taskAssignment, 'awReqTest');
// Loop through the variables
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
// If the variable has a value, add to first list
if (question.getValue() != '') {
populatedList += question.getLabel() + ": " + question.getValue() + '\n\n';
}
// If the variable is empty, add to second list
else {
emptyList += question.getLabel() + ': ' + '\n\n';
}
}
davLog('Populated list1: ' + populatedList, 'awReqTest');
davLog('Empty list1: ' + emptyList, 'awReqTest');
// Add the reference fields to the description fields or the appropriate list
if (mainContact != '') {
current.business_case += 'Main Contact: ' + producer.v_main_contact.getDisplayValue() + '\n\n';
} else {
emptyList += 'Main Contact: ';
}
if (ci != '') {
populatedList += 'Configuration Item: ' + producer.v_cmdb_ci.name + '\n\n';
} else {
emptyList += 'Configuration Item: ';
}
if (taskAssignment != '') {
populatedList += 'Task Assignment Group: ' + producer.task_assignment.getDisplayValue() + '\n\n';
} else {
emptyList += 'Task Assignment Group: ';
}
davLog('Populated list2: ' + populatedList, 'awReqTest');
davLog('Empty list2: ' + emptyList, 'awReqTest');
// Add the lists to the description field. Filled out variables first, then empty variables
current.business_case += populatedList + '\n\n' + emptyList;
return;
}
function createNewIncident() {
current.short_description = 'Request to Build a Record Producer in ServiceNow \n';
// Define the variables to display in the description field of the idea record
var variables = [producer.type_of_request, producer.v_type_of_catalog_item, producer.v_add_contact, producer.v_suggested_name, producer.v_volume, producer.v_short_description, producer.v_approvals_needed, producer.v_description, producer.v_describe_approval, producer.v_describe_customer, producer.v_live, producer.v_delivery_time, producer.v_incident_short_description, producer.v_incident_description, producer.v_describe_variables, producer.v_altered, producer.v_job_aids_referenced, producer.v_job_aids_updated, producer.v_marketed, producer.v_job_aids_created];
/*MainContact, CI, IncAssignment*/
// Variables to hold the variable list
var populatedList = '';
var emptyList = '';
// Reference fields - '.getQuestion()' does not cooperate with reference fields
var mainContact = producer.v_main_contact.getDisplayValue(); // 'Main Contact: ' + producer.v_main_contact.getDisplayValue() + '\n\n';
var ci = producer.v_cmdb_ci.name; // 'Configuration Item: ' + producer.v_cmdb_ci.name + '\n\n';
var incAssignment = producer.incident_assignment.getDisplayValue(); //'Incident Assignment Group: ' + producer.incident_assignment.getDisplayValue() + '\n\n';
// Loop through the variables
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
// If the variable has a value, add to first list
if (question.getValue() != '') {
populatedList += question.getLabel() + ": " + question.getValue() + '\n\n';
}
// If the variable is empty, add to second list
else {
emptyList += question.getLabel() + ': ' + '\n\n';
}
}
// Add the reference fields to the description field or the appropriate list
if (mainContact != '') {
current.business_case += 'Main Contact: ' + producer.v_main_contact.getDisplayValue() + '\n\n';
} else {
emptyList += 'Main Contact: ';
}
if (ci != '') {
populatedList += 'Configuration Item: ' + producer.v_cmdb_ci.name + '\n\n';
} else {
emptyList += 'Configuration Item: ';
}
if (incAssignment != '') {
populatedList += 'Incident Assignment Group: ' + producer.incident_assignment.getDisplayValue() + '\n\n';
} else {
emptyList += 'Incident Assignment Group: ';
}
// Add the lists to the description field. Filled out variables first, then empty variables
current.business_case += populatedList + '\n\n' + emptyList;
return;
}
function updateItem() {
current.short_description = 'Request to Update a Catalog Item in ServiceNow \n\n';
// Define the variables to display in the description field of the idea record
var variables = [producer.type_of_request, producer.update_desc];
/*UpdateItem*/
// Variables to hold the variable list
var populatedList = '';
var emptyList = '';
// Reference fields - '.getQuestion()' does not cooperate with reference fields
var updateItem = producer.update_cat_item.getDisplayValue(); //'Catalog Item Requiring Change: ' + producer.update_cat_item.getDisplayValue() + '\n\n';
// Loop through the variables
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
// If the variable has a value, add to first list
if (question.getValue() != '') {
populatedList += question.getLabel() + ": " + question.getValue() + '\n\n';
}
// If the variable is empty, add to second list
else {
emptyList += question.getLabel() + ': ' + '\n\n';
}
}
// Add the reference fields to the description field or the appropriate list
if (updateItem != '') {
current.business_case += 'Catalog Item Requiring Change: ' + producer.update_cat_item.getDisplayValue() + '\n\n';
} else {
emptyList += 'Catalog Item Requiring Change: ';
}
// Add the lists to the description field. Filled out variables first, then empty variables
current.business_case += populatedList + '\n\n' + emptyList;
return;
}
function retireItem() {
current.short_description = 'Request to Retire a Catalog Item in ServiceNow \n\n';
// Define the variables to display in the description field of the idea record
var variables = [producer.type_of_request, producer.retire_desc];
/*RetireItem*/
// Variables to hold the variable list
var populatedList = '';
var emptyList = '';
// Reference fields - '.getQuestion()' does not cooperate with reference fields
var retireItem = producer.retire_cat_item.getDisplayValue(); //'Catalog Item to be Retired: ' + producer.retire_cat_item.getDisplayValue() + '\n\n';
// Loop through the variables
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
// If the variable has a value, add to first list
if (question.getValue() != '') {
populatedList += question.getLabel() + ": " + question.getValue() + '\n\n';
}
// If the variable is empty, add to second list
else {
emptyList += question.getLabel() + ': ' + '\n\n';
}
}
// Add the reference fields to the description field or the appropriate list
if (retireItem != '') {
current.business_case += 'Catalog Item to be Retired: ' + producer.retire_cat_item.getDisplayValue() + '\n\n';
} else {
emptyList += 'Catalog Item to be Retired: ';
}
// Add the lists to the description field. Filled out variables first, then empty variables
current.business_case += populatedList + '\n\n' + emptyList;
return;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2020 11:01 AM
Try using something like the below script:
var variables = producer.variables.getElements();
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
gs.log(question.getLabel() + ":" + question.getValue())
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2020 11:12 AM
Thanks Elijah. This looks close to what I am looking for, but there is a specific list of variables that are inserted into the description field based on the value of two select boxes: Update/New > Incident/Request.
How would you recommend I manually define the list of variables for the loop to use? We already have if statements to check the values of the two select boxes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2024 07:33 AM
This worked for me. I added question.getType() to determine the field type and adjust the result. Thank you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2020 11:27 AM
Hi Alex,
Can you try below once.
var variables = producer.variables.getElements();
var populatedList = '';
var emptyList = '';
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
if(question.getValue()!='')
{
populatedList+ =question.getLabel() + ":" + question.getValue();
}
else
{
emptyList+ =question.getLabel();
}
} //close for
current.business_case = populatedList+ '\n\n'+ emptyList;