Survey conditions (create a business rule to add a condition on another table for the same survey)

Community Alums
Not applicable

Hello community,

 

I have a survey with survey conditions on the sc_req_item table. But now I have a new condition requirement but it involves another "table sc_task". Since I cannot dot-walk to that field because it's in another table I thought about doing a business rule to cover this requirement. The condition is: if the assigment_group.parent in catalog task is X... it should evaluate all the other conditions as well in order to trigger the survey.
But am I able to achieve this? Knowing that all of the other conditions need to be checked for the survey to be triggered as well? 
Could you help me reach a viable solution? Is it possible via BR? If so, how could I do it?

Thank you in advance!

1 REPLY 1

Community Alums
Not applicable

I tried various approaches, such as this one below (business rule). But I just can't get it to work. Any ideas how this could work, or even if it's possible?
Table: asmt_assessment_instance   (also tried on sc_req_item table)
When: Before
On Insert


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


var targetSurveySysId = 'survey_sys_id';

if (current.asmt_template != targetSurveySysId) {
// If it's not the targeted survey, allow the survey creation to proceed.
return;
}

// Retrieve the corresponding sc_req_item record.
var reqItemGr = new GlideRecord('sc_req_item');
if (!reqItemGr.get(current.source_id)) {
return; // Exit if no corresponding sc_req_item is found.
}

// Query the related sc_task records for this request item.
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('request_item', reqItemGr.sys_id); // Relating the sc_task to sc_req_item
taskGr.query();

var parentConditionMet = false;

// Step 4: Check the assignment_group.parent condition on each related task.
while (taskGr.next()) {
if (taskGr.assignment_group.parent == 'parent_group_sys_id) {
parentConditionMet = true;
break; // If condition is met, no need to check further.
}
}

// Step 5: If the condition is not met, prevent the survey from being created.
if (!parentConditionMet) {
gs.addInfoMessage("Survey blocked: Assignment group parent does not meet the required condition.");
current.setAbortAction(true); // Stop the insert of the survey instance.
}

})(current, previous);