Look Up select box is not working via integration

SumanthK7729163
Tera Contributor

1) In the background script is working fine :

 

var taskSysId = '5f1f516dc35203d05a76d5fe0501315d';
var grTask = new GlideRecord('sc_task');

if (grTask.get(taskSysId)) {
var ritmSysId = grTask.request_item.toString();
gs.info("Found Parent RITM: " + ritmSysId);

var test = new x_custom_scope.CustomScriptInclude();
var variableJSON = test.getVariableJSON(ritmSysId);

gs.info("Variable JSON Output:\n" + JSON.stringify(variableJSON, null, 4));
} else {
gs.error("Could not find an sc_task record with the provided sys_id: " + taskSysId);
}


2) when i run via Business rules and script includes its getting the Sys_id Instead of script inlcude in the payload generation

...
var variableJSON = CustomScriptInclude.getVariableJSON(current.request_item.toString());
....


var payload = {};
...
payload.variables = variableJSON;
....

// Genericized custom table name
var transactionlogGr = new GlideRecord('x_custom_scope_transaction_log');
transactionlogGr.initialize();

transactionlogGr.payload = JSON.stringify(payload);

.......................

1 ACCEPTED SOLUTION

Hi @SumanthK7729163 

 

may you try replace 


var variableJSON = vendorIntegrationOutboundUtils.getVariableJSON(current.request_item.toString());

to 

 

var variableJSON = vendorIntegrationOutboundUtils.getVariableJSON(current.getValue('request_item'));

The issue occurs because in scoped Business Rules within ServiceNow, using ⁠current.request_item.toString()⁠ can fail to extract the pure string of the reference sys_id⁠, passing a ⁠GlideElement⁠ object instead. This causes the ⁠GlideRecord⁠ query in the Script Include to fail or behave inconsistently.


The correct way to retrieve the
⁠sys_id⁠ as a clean string within the Business Rule context is to use ⁠current.getValue('request_item')⁠.

 

 

If this response was helpful, please mark it as Helpful and, if applicable, as Correct.
This helps other users find accurate and useful information more easily

View solution in original post

11 REPLIES 11

Rafael Batistot
Kilo Patron

Hi @SumanthK7729163 

 

If you could share the complete code for the CustomScriptInclude, especially the getVariableJSON() method, it would be much easier to determine why it returns only the sys_id when called from the Business Rule, while it returns the expected JSON when executed from the Background Script.

If this response was helpful, please mark it as Helpful and, if applicable, as Correct.
This helps other users find accurate and useful information more easily

below is in the scoped Application In the sc_task table after insert or update
<-----Business rule code--->

(function executeRule(current, previous /*null when async*/ ) {

var debug = (gs.getProperty('x_4dai_vi.vendorIntegration.debug', false).toString() == "true");
if (debug) {
gs.info("Business Rule: VI: Catalog Task Assigned - Triggered");
}

var vendorIntegrationOutboundUtils = new x_4dai_vi.Vendor_Integration_OutboundUtils();
var processSysid = vendorIntegrationOutboundUtils.getProcessSysID('request_management');
var methodSysid = vendorIntegrationOutboundUtils.getMethodSysId(processSysid, 'taskAssigned');
var processPriority = vendorIntegrationOutboundUtils.getProcessPriority('request_management'); var requestedforDetails = {};
requestedforDetails['first_name'] = current.request_item.request.requested_for.first_name.toString();
requestedforDetails['last_name'] = current.request_item.request.requested_for.last_name.toString();
requestedforDetails['email_address'] = current.request_item.request.requested_for.email.toString();
requestedforDetails['user_name'] = current.request_item.request.requested_for.user_name.toString();

var requestedbyDetails = {};
requestedbyDetails['first_name'] = current.request_item.opened_by.first_name.toString();
requestedbyDetails['last_name'] = current.request_item.opened_by.last_name.toString();
requestedbyDetails['email_address'] = current.request_item.opened_by.email.toString();
requestedbyDetails['user_name'] = current.request_item.opened_by.user_name.toString();

var catalogitemDetails = {};
catalogitemDetails['name'] = current.request_item.cat_item.name.toString();
catalogitemDetails['sys_id'] = current.request_item.cat_item.sys_id.toString();
catalogitemDetails['catalog'] = current.request_item.cat_item.sc_catalogs.getDisplayValue();
catalogitemDetails['category'] = current.request_item.cat_item.category.getDisplayValue();

// Prepare some additional requested item details
var requestedItemDetails = {
number: current.request_item.number.toString(),
service: {
value: current.request_item.business_service.toString(),
display_value: current.request_item.business_service.getDisplayValue(),
},
service_offering: {
value: current.request_item.service_offering.toString(),
display_value: current.request_item.service_offering.number.toString(),
},
application_service: {
value: current.request_item.u_service_instance.toString(),
display_value: current.request_item.u_service_instance.getDisplayValue(),
},
cmdb_ci: vendorIntegrationOutboundUtils.prepareCI(current.request_item.cmdb_ci.toString()),
};

// Prepare some additional request details
var requestDetails = {
delivery_address: current.request_item.request.delivery_address.toString(),
special_instructions: current.request_item.request.special_instructions.toString(),
};

var ciDetails = vendorIntegrationOutboundUtils.prepareCIListInJSON(current.cmdb_ci.toString());
var variableJSON = vendorIntegrationOutboundUtils.getVariableJSON(current.request_item.toString());
//var attachmentJSON = vendorIntegrationOutboundUtils.getAttachmentJson(current.request_item.toString());

var payload = {};
payload.sys_class_name = current.sys_class_name.toString();
payload.servicenow_process = "request_management";
payload.servicenow_method = "taskAssigned";

payload.number = current.number.toString();
payload.requested_item_number = current.request_item.number.toString();
payload.requested_item = requestedItemDetails;
payload.request = requestDetails;
payload.catalog_item = catalogitemDetails;
payload.requested_for = requestedforDetails;
payload.requested_by = requestedbyDetails;
payload.service = current.business_service.getDisplayValue();
payload.service_offering = current.service_offering.number.toString();
payload.application_service = current.u_service_instance.getDisplayValue();
if (ciDetails) {
payload.cmdb_ci = JSON.parse(ciDetails);
} else {
payload.cmdb_ci = "";
}
payload.description = current.description.toString();
payload.short_description = current.short_description.toString();
payload.priority = current.priority.toString();
payload.assignment_group = current.assignment_group.getDisplayValue();
payload.variables = variableJSON;
//payload.attachments = attachmentJSON;
payload.legal_entity = current.u_legal_entity.toString();

var transactionlogGr = new GlideRecord('x_4dai_vi_transaction_log');
transactionlogGr.initialize();
transactionlogGr.direction = "Outbound";
transactionlogGr.method = methodSysid;
transactionlogGr.process = processSysid;
transactionlogGr.u_processing_priority = processPriority;
transactionlogGr.payload = JSON.stringify(payload);
transactionlogGr.integration_account = current.assignment_group.u_integration_account;
transactionlogGr.related_task = current.sys_id;
transactionlogGr.state = "Ready";
var res = transactionlogGr.insert();})(current, previous);

 

<----Script include function and this is also a scoped application----->

getVariableJSON: function(requestItemsysid) {
var finalJson = {};

var grRITM = new GlideRecord('sc_req_item');
if (grRITM.get(requestItemsysid)) {
var vars = grRITM.variables.getElements(true);

var arMrvs = [];
var arVariables = [];
var typenotAllowed = gs.getProperty('x_4dai_vi.notallowed.variable.types.for.outbound');
var arTypenotAllowed = typenotAllowed.split(',');

for (var i = 0; i < vars.length; i++) {
var scVar = vars[i];
if (scVar.isMultiRow()) {
var jsonMrvs = {};
jsonMrvs.name = scVar.getLabel();
jsonMrvs.display_name = scVar.getLabel();
jsonMrvs.total_rows = scVar.getRowCount();

var rows = scVar.getRows();
var arRows = [];
for (var j = 0; j < scVar.getRowCount(); j++) {
var row = rows[j];
var cells = row.getCells();
var arCells = [];

for (var k = 0; k < cells.length; k++) {
var cell = cells[k];

var jsonCell = {};
jsonCell.name = cell.getVariableName();
jsonCell.value = cell.getCellValue();
jsonCell.display_value = cell.getCellDisplayValue();
arCells.push(jsonCell);

}
var jsonRow = {};
jsonRow.cells = arCells;
arRows.push(jsonRow);
}
jsonMrvs.rows = arRows;
arMrvs.push(jsonMrvs);

} else {
var question = scVar.getQuestion();
if (arTypenotAllowed.indexOf(question.type.toString()) > -1) {
continue;
} else {
var jsonVariables = {};
jsonVariables.name = question.getName();
jsonVariables.value = question.getValue();
jsonVariables.display_value = question.getDisplayValue();

arVariables.push(jsonVariables);
}
}
}

finalJson.mrvs = arMrvs;
finalJson.variables = arVariables;
}
return finalJson;
},

 

Hi @SumanthK7729163 

 

may you try replace 


var variableJSON = vendorIntegrationOutboundUtils.getVariableJSON(current.request_item.toString());

to 

 

var variableJSON = vendorIntegrationOutboundUtils.getVariableJSON(current.getValue('request_item'));

The issue occurs because in scoped Business Rules within ServiceNow, using ⁠current.request_item.toString()⁠ can fail to extract the pure string of the reference sys_id⁠, passing a ⁠GlideElement⁠ object instead. This causes the ⁠GlideRecord⁠ query in the Script Include to fail or behave inconsistently.


The correct way to retrieve the
⁠sys_id⁠ as a clean string within the Business Rule context is to use ⁠current.getValue('request_item')⁠.

 

 

If this response was helpful, please mark it as Helpful and, if applicable, as Correct.
This helps other users find accurate and useful information more easily

Thanks mannn!!!