Survey Business rule not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 06:17 AM
Hi,
Whenever a user submits a survey, a new record is created in 'asmt_assessment_instance' table, and records for each question are created in 'asmt_assessment_instance_question' table. A field called 'value' has a numerical data. I want to add all the values and get the average of those for each instance (survey) as below:
I have created a BR on 'asmt_assessment_instance' table with the below script:
(function executeRule(current, previous /*null when async*/ ) {
// var surveyNumber = current.sys_id;
var surveyNumber = current.number;
gs.log('survey number is: ' + surveyNumber);
var surveyValue = new GlideRecord('asmt_assessment_instance_question');
surveyValue.addQuery('instance', surveyNumber);
// surveyValue.addEncodedQuery('instance='+surveyNumber);
surveyValue.query();
while (surveyValue.next()) {
var questionValue = surveyValue.getValue('value');
gs.log('Question value: ' + questionValue);
}
})(current, previous);
For some reason (I reckon, the addquery filter is not right), it is not going in the while loop and log 'Question value: ' is not getting logged.
The same script works well in background script but not in after/ before insert Business Rule.
Need help in debugging/ understanding what is happening here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 06:42 AM
Hi @Swapnilc191
there is issue with your code. same worked for me. please update your code as follows:
(function executeRule(current, previous /*null when async*/ ) {
// var surveyNumber = current.sys_id;
var surveyNumber = current.number;
gs.log('survey number is: ' + surveyNumber);
var surveyValue = new GlideRecord('asmt_assessment_instance_question');
surveyValue.addQuery('instance.number', surveyNumber);
surveyValue.query();
while(surveyValue.next()) {
//var value = surveyValue.getValue('instance');
gs.log("Value: " + surveyValue.value);
}
})(current, previous);
……………………………………………………………………………………………………
Please Mark it helpful 👍and Accept Solution✔️!! If this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 06:54 AM
@Satishkumar B I used the same code as per your changes for both before/ after insert. But it didnt work.
The survey number gets printed but it doesnt goes in the while loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 11:29 AM
Hi @Swapnilc191
can you run you business rule on display BR .
…………………………………………..
Mark it helpful 👍and Accept Solution ✅!! If this helps you to understand.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 06:53 AM - edited 07-24-2024 06:58 AM
I tried the following in scripts background for one survey instance I have
// var surveyNumber = current.sys_id;
// since asmt_assessment_instance_question.instance is a Reference, need to use sys_id
var surveyNumber = 'a49d5cfd9f610200736af84bc42e704d'; // AINST0000501
// var surveyNumber = current.number;
gs.log('survey number is: ' + surveyNumber);
var surveyValue = new GlideRecord('asmt_assessment_instance_question');
//instance=a49d5cfd9f610200736af84bc42e704d
surveyValue.addQuery('instance', surveyNumber);
// surveyValue.addEncodedQuery('instance='+surveyNumber);
surveyValue.query();
gs.info("Found " + surveyValue.getRowCount() + " questions");
var total = 0;
var rCount = 0;
while (surveyValue.next()) {
rCount++;
var questionValue = parseInt(surveyValue.getValue('value'));
gs.log('Question value: ' + questionValue);
total += questionValue;
}
gs.info("total = " + total + ", average = " + total/rCount);
and get:
*** Script: survey number is: a49d5cfd9f610200736af84bc42e704d
*** Script: Found 19 questions
*** Script: Question value: 9
*** Script: Question value: 7
*** Script: Question value: 7
*** Script: Question value: 10
*** Script: Question value: 10
*** Script: Question value: 8
*** Script: Question value: 2
*** Script: Question value: 8
*** Script: Question value: 10
*** Script: Question value: 10
*** Script: Question value: 7
*** Script: Question value: 2
*** Script: Question value: 1
*** Script: Question value: 2
*** Script: Question value: 8
*** Script: Question value: 10
*** Script: Question value: 5
*** Script: Question value: 10
*** Script: Question value: 8
*** Script: total = 134, average = 7.052631578947368
I see Satishkumar B code would work in a BR. But to add the values and get an average, you need to sum the integer values and count the records found.