Aggregate two integer fields without scripting

Mohamed Elsayed
Tera Expert

Hi All,

 

I am new to this community and ServiceNow and have zero scripting experience. So, is there any way to aggregate two integer fields without scripting? 

 

Simpley, The calculation should be: Total Units= Total Units + Add Tokens.

 

MohamedElsayed_0-1717265473512.png

 

Regards,

Mo

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Mohamed Elsayed ,

There is a way without using GlideAggregate you can simplly do like - 

Right click on the field in which you need to store the response and add this line of code 

SarthakKashyap_1-1717267729577.png

 

(function calculatedFieldValue(current) {

	// Add your code here
	var a = current.u_a;
	var b = current.u_b;
	var c = current.u_c;
	var d = current.u_d;

	return a+b+c+d;  // return the calculated value

})(current);

SarthakKashyap_0-1717267695176.png

 

For emaple in my Incident form there a 5 fields - A,B,C,D and E

E stors the sum of A,B,C,D 

 

Result

SarthakKashyap_2-1717267800617.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

Hi @Mohamed Elsayed ,

There is a way without using GlideAggregate you can simplly do like - 

Right click on the field in which you need to store the response and add this line of code 

SarthakKashyap_1-1717267729577.png

 

(function calculatedFieldValue(current) {

	// Add your code here
	var a = current.u_a;
	var b = current.u_b;
	var c = current.u_c;
	var d = current.u_d;

	return a+b+c+d;  // return the calculated value

})(current);

SarthakKashyap_0-1717267695176.png

 

For emaple in my Incident form there a 5 fields - A,B,C,D and E

E stors the sum of A,B,C,D 

 

Result

SarthakKashyap_2-1717267800617.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

Thanks Sartha,

 

I used it and it works but the result is always triple what it should be. So, for example, when I add 100 to the field (Add Tokens) which is (B) in the script. The script adds 300 to the (Total Units) which is (A) in the script.

 

The script:

(function calculatedFieldValue(current) {

	// Add your code here
	var a = current.total_units
	var b = current.u_add_tokens
	return a+b;  // return the calculated value

})(current);

 

Sandeep Rajput
Tera Patron
Tera Patron

@Mohamed Elsayed If you wish to configure the average on a list within in ServiceNow then you can use List calculations. https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/administer/list-adminis...

 

However, if you wish to do it at form level then you will have to use Calculated field https://www.servicenow.com/community/developer-blog/calculate-value-field-use-case/ba-p/2386808 or a function field https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/build/platform-function... and little bit of scripting to calculate the average.

 

Hope this helps.

PrashantLearnIT
Giga Sage

Hi @Mohamed Elsayed ,

 

You can also use Formula Builder - Formula Builder || Use of Formula Builder || New Concept ServiceNow (youtube.com)

 

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************