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.

Dictionary calculated value script, value in list and form view but can't populate in cat item

Tungw
Tera Expert

I attempted two different approaches to populate a value in my service catalog. First, I created a field in the alm_license table and used the Dictionary Calculated Value to retrieve the value. I successfully obtained the value on the form and list view. However, when I attempted to auto-populate it in a catalog variable, the value didn't display.

 

I tried catalog client script with a script include as well, but I didn't get any value back. I retrieved the XML from the list and realized the value was empty.


Below is the script for reference:

 

(function calculatedFieldValue(current) {
    // Function to count usage using GlideAggregate
    function countUsage() {
        var ga = new GlideAggregate('alm_entitlement');
        ga.addQuery('licensed_by', current.sys_id);
        ga.addAggregate('COUNT');
        ga.query();
        if (ga.next()) {
            return parseInt(ga.getAggregate('COUNT'), 10);
        }
        return 0;
    }


    // Get the usage count
    var usageCount = countUsage();


    // Calculate the balance by subtracting usage count from rights
    var rights = alm_license.rights || 0; // Default to 0 if rights is not defined
    var balance = rights - usageCount;

 

 

 

1 ACCEPTED SOLUTION

Bhavani Shankar
Tera Guru

Hi @Tungw,

 

I tried to recreate the problem statement in my PDI. I got the expected results. Check it out

 

for Rights allocated column, I did a script calculation as follows

 

 

(function calculatedFieldValue(current) {
	function countUsage() {
        var ga = new GlideAggregate('alm_entitlement');
        ga.addQuery('licensed_by', current.sys_id);
        ga.addAggregate('COUNT');
        ga.query();
        if (ga.next()) {
            return parseInt(ga.getAggregate('COUNT'), 10);
        }
    }
	// Add your code here
	return countUsage();  // return the calculated value

})(current);

 

 

 

For Rights balance column, I did a formula calculation like this

BhavaniShankar_0-1722789865680.png

 

and the XML retrieved from form view looked like this, received values as expected

BhavaniShankar_1-1722790031563.png

and here is the form view.

BhavaniShankar_2-1722790090198.png

 

 

If my response helped, please leave a thumbs up and mark this helpful

 

Regards,

Bhavani Shankar

 

 

 

 

 

Regards,
Bhavani Shankar
Linked In

View solution in original post

1 REPLY 1

Bhavani Shankar
Tera Guru

Hi @Tungw,

 

I tried to recreate the problem statement in my PDI. I got the expected results. Check it out

 

for Rights allocated column, I did a script calculation as follows

 

 

(function calculatedFieldValue(current) {
	function countUsage() {
        var ga = new GlideAggregate('alm_entitlement');
        ga.addQuery('licensed_by', current.sys_id);
        ga.addAggregate('COUNT');
        ga.query();
        if (ga.next()) {
            return parseInt(ga.getAggregate('COUNT'), 10);
        }
    }
	// Add your code here
	return countUsage();  // return the calculated value

})(current);

 

 

 

For Rights balance column, I did a formula calculation like this

BhavaniShankar_0-1722789865680.png

 

and the XML retrieved from form view looked like this, received values as expected

BhavaniShankar_1-1722790031563.png

and here is the form view.

BhavaniShankar_2-1722790090198.png

 

 

If my response helped, please leave a thumbs up and mark this helpful

 

Regards,

Bhavani Shankar

 

 

 

 

 

Regards,
Bhavani Shankar
Linked In