Survey Business rule not working

Swapnilc191
Tera Contributor

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:

Swapnilc191_2-1721826681247.png

 

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);

 

 

Swapnilc191_0-1721825977076.png

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.

Swapnilc191_1-1721826165202.png

 

 

 

8 REPLIES 8

@Bert_c1 The script works for me as well through background script but the same logic doesnt work in Business rule.

Did this:

Swapnilc191_0-1721829985760.png



Swapnilc191_1-1721830016652.png



And Background script :

Swapnilc191_2-1721830056306.png

 

Again, use Satishkumar B's code for the BR  and sum the integer values and divide by the number of records.

Priyanka_786
Tera Guru
Tera Guru

@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

Bert_c1
Kilo Patron

@Swapnilc191,

 

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.