Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Satishkumar B
Giga Sage
Giga Sage

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



@Satishkumar B I used the same code as per your changes for both before/ after insert. But it didnt work.

Swapnilc191_0-1721829116119.png

 

The survey number gets printed but it doesnt goes in the while loop.

Swapnilc191_1-1721829197918.pngSwapnilc191_2-1721829219536.png

 

 

Hi @Swapnilc191 

can you run you business rule on display BR .

 

…………………………………………..
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

Bert_c1
Kilo Patron

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.