Background script not worked in business rule for the survey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2024 01:48 AM
The following script works as background script but when I want this code used in business rule It is not working
The code for to get total average of survey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2024 02:03 AM
Hi @Asha Pathak ,
I tried your problem in my PDI and it works for me. I hope this works for you as well Please refer below code in BR
I created Before Business rule and it works on Insert and Update
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
gs.log("Current = " + current.sys_id)
var array = [];
var total = 0;
var avg = 0;
var test = new GlideRecord('asmt_assessment_instance');
test.addQuery('sys_id', current.sys_id); // current.sys_id
test.query();
if (test.next()) {
gs.log("Inside if " + test.number);
var questions = new GlideRecord('asmt_assessment_instance_question');
questions.addQuery('instance', test.sys_id);
// questions.addEncodedQuery('metric.datatype=numericscale');
questions.query();
gs.log("Count = " + questions.getRowCount());
while (questions.next()) {
gs.log('Metric...' + questions.getDisplayValue('metric') + ' .. ' + questions.value);
array.push(questions.value.toString());
}
var metricName = questions.getDisplayValue('metric');
gs.log('array ' + array);
for (var i = 0; i < array.length; i++) {
total += parseInt(array[i]);
}
gs.log('array value ' + total);
avg = (total / array.length).toFixed(2);
gs.log('array value avg ' + avg);
var questions1 = new GlideRecord('asmt_assessment_instance_question');
questions1.addQuery('instance', test.sys_id);
// questions1.addEncodedQuery('metric.nameSTARTSWITHOverall average');
questions1.query();
gs.log("New 222 " + questions1.getRowCount());
if (questions1.next()) {
questions1.setValue('value', '1.5');
questions1.update();
gs.log('Metric...' + questions1.getDisplayValue('metric') + ' .. ' + questions1.value);
}
}
})(current, previous);
Here image for reference
Here's the result
When I insert any record
My BR will work
I added the log and it's coming
Please feel free to reach me if any query comes
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2024 02:18 AM
Hi @Asha Pathak
(function executeRule(current, previous /* previous is not used in this script */) {
var instanceId = 'fcef73ed47a90210784786c8436d4309'; // Get the actual sys_id in instanceID
var total = 0;
var avg = 0;
var count = 0;
var test = new GlideRecord('asmt_assessment_instance');
if (test.get(instanceId)) {
gs.log('Assessment Number: ' + test.number);
var questions = new GlideRecord('asmt_assessment_instance_question');
questions.addQuery('instance', instanceId);
questions.addEncodedQuery('metric.datatype=numericscale');
questions.query();
while (questions.next()) {
gs.log('Metric: ' + questions.getDisplayValue('metric') + ', Value: ' + questions.value);
total += parseInt(questions.value, 10);
count++;
}
if (count > 0) {
avg = (total / count).toFixed(2);
gs.log('Total: ' + total + ', Count: ' + count + ', Average: ' + avg);
var overallAverageQuery = 'metric.nameSTARTSWITH=Overall average';
var overallAvgQuestion = new GlideRecord('asmt_assessment_instance_question');
overallAvgQuestion.addQuery('instance', instanceId);
overallAvgQuestion.addEncodedQuery(overallAverageQuery);
overallAvgQuestion.query();
if (overallAvgQuestion.next()) {
overallAvgQuestion.setValue('value', avg);
overallAvgQuestion.update();
gs.log('Updated Overall Average Metric Value to: ' + avg);
} else {
gs.log('Overall Average Metric not found.');
}
} else {
gs.log('No numeric scale metrics found for the assessment instance.');
}
} else {
gs.log('Assessment instance with ID ' + instanceId + ' not found.');
}
})(current, previous);
Please try and update me with the logs you get so that I can assist you further on it.
Regards,
Amit