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:57 AM - edited ‎07-24-2024 07:07 AM
@Bert_c1 The script works for me as well through background script but the same logic doesnt work in Business rule.
Did this:
And Background script :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 07:00 AM
Again, use Satishkumar B's code for the BR and sum the integer values and divide by the number of records.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 07:53 AM
@Swapnilc191 - If you looking for number then in while loop query filter you need to pass 'instance.number' . It should work.
Now coming to your question- in background script you are passing har coded sys _ id of record which is already created. Hence you are getting expected log. Main difference here that through business rule dynamically it is creating record in assessment instance table first then related questions are created. So the code point of view all good. But output may differ on trigger of logic.
Hope it helps
Please mark my response as helpful/correct if it helps you.
Regards,
Priyanka Salunke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 09:58 AM
I took @Satishkumar B's code, added logic to add the values and calculate the average, then created a business rule on the 'asmt_assessment_instance', and tested.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var surveyNumber = current.number;
gs.info('BR: survey number is: ' + surveyNumber);
var surveyValue = new GlideRecord('asmt_assessment_instance_question');
surveyValue.addQuery('instance.number', surveyNumber);
surveyValue.query();
var noRecords = surveyValue.getRowCount();
var total = 0;
while(surveyValue.next()) {
//var value = surveyValue.getValue('instance');
gs.info("BR: Value: " + surveyValue.value);
total += parseInt(surveyValue.value);
}
gs.info("BR: Total = " + total + ", average = " + total/noRecords);
})(current, previous);
and got the desired result.