Need help in making a script to use the current score of a formula indicator

Rohit8
Tera Expert

Greetings!

I am working on an Asset Dashboard and currently trying to create an Indicator to show the "Number of Projected Desktop replacements - Current year".

So I already have a (formula) indicator "Average Age of Retired Assets" which uses "Summed age of assets DIVIDED BY Number of assets". The summed age is being calculated as per the asset's release date using this script.

var diff=function(x,y){return y.dateNumericValue() - x.dateNumericValue();};

//var seconds=function(x,y){return diff(x,y)/(1000);};

//var minutes=function(x,y){return diff(x,y)/(60*1000);};

//var hours=function(x,y){return diff(x,y)/(60*60*1000);};

//var days=function(x,y){return diff(x,y)/(24*60*60*1000);};

//var weeks=function(x,y){return diff(x,y)/(7*24*60*60*1000);};

var years=function(x,y){return diff(x,y)/(365*24*60*60*1000);};

years(current.u_avg_hw_release_date, score_end);

So is there a way, I can use the same script (with some modifications) in this new Automated Indicator so that while filtering to use the Deployed desktops only, it also uses the current score of the indicator "Average Age of Retired Assets" and includes only the ones in count which have the age EQUAL TO OR GREATER THAN the Average age.

Thanks a lot.

1 ACCEPTED SOLUTION

Adam Stout
ServiceNow Employee
ServiceNow Employee

You'll call the API and parse the results.   Take a look at the example here:



PAScorecard - Scoped


View solution in original post

6 REPLIES 6

Adam Stout
ServiceNow Employee
ServiceNow Employee

Scripts are attached to a single table (since the current. field may differ table to table).   However, I recommend you put your logic into a Script Include so that you can reuse your logic over and over again.   The PA Script would just be one or two lines to call the function in the script include with the field from that table.



What part is causing you trouble?


Thank you, Adam.



Unfortunately, I currently do not have access to Script Includes. Is this possible using Breakdowns & Bucket groups ??



I was able to create the Breakdown - (Past Average Age) with the Bucket groups - TRUE (0)/FALSE (1) using this script.PA3.JPG)



var diff=function(x,y){return y.dateNumericValue() - x.dateNumericValue();};


//var seconds=function(x,y){return diff(x,y)/(1000);};


//var minutes=function(x,y){return diff(x,y)/(60*1000);};


//var hours=function(x,y){return diff(x,y)/(60*60*1000);};


//var days=function(x,y){return diff(x,y)/(24*60*60*1000);};


//var weeks=function(x,y){return diff(x,y)/(7*24*60*60*1000);};


var years=function(x,y){return diff(x,y)/(365*24*60*60*1000);};




var gdt = new GlideDateTime();


var currentYear=gdt.getYearLocalTime().toString().substr(-2);


var yearlastDate='31-Dec-'+currentYear;




var assetAge= years(current.u_avg_hw_release_date, score_end);//score_end




var avgAge=6.2;


if(assetAge >= parseFloat(avgAge))


answer = 0;


else


answer = 1;



The issues I am facing are:



1. Currently I had to manually enter the score (AvgAge) in the script - 6.2 years. Is it possible to have the script (dynamically) use the current score of a Formula Indicator ?



2. I need the asset age to be determined using the Last day of the current year MINUS Current Release Date. But when I replace the 'score_end' with 'yearLastDate'. and start the collection, it groups them all into FALSE (1). I did try to keep the current year date format similar to Release date's, i.e. dd-mmm-yy.



I read about the Script Includes but couldn't understand how it will work in this situation.



3. Can I use it to call the current score of a formula indicator ?



Thank you.


Adam Stout
ServiceNow Employee
ServiceNow Employee

Script includes are not required, they will just make your life easier because you won't have to repeat the same code multiple times.



1) Yes, you can pull existing scores (like a formula indicator) using the PA Scorecard API. However, you won't be able to pull breakdown information due to how Automated Indicators are processed.   [When we need to do this, we typically use formula indicators which can operate on breakdowns]



2) Put in some debugging (gs.log() statements) to make sure you get what you think you are getting.   Script Includes let you re-use your logic.   They add not functionality but let you reuse functionality you develop.



3) The API mentioned above works on all indicators types (but again, in a PA Script you don't have access to the breakdown but you may be able to get what you need using the element if you only have one breakdown.




I would step back and see if we can accomplish what we are looking for with other tools, I'm not sure you are going to get what you want forcing this into a PA Script.   You may want to look at using a Scripted Filter to pull the average age and then you wouldn't need this breakdown as the whole indicator would be over the average or under the average.


Thanks a lot, Adam.



I am working on the date issue, I think I messed up the formats.



I just need my Automated Indicator to use the script and compare the Age of the Desktops to the current score of a different formula indicator (Average age), and then accordingly bucket them into 2 groups (LESS THAN or EQUAL/GREATER THAN). The Formula Indicator which will be referenced doesn't have a Breakdown.



Hence the PA Scorecard API indeed looks like the way to go. I was trying to start with that.



Is it something simple like just replacing ":AvgAge" variable in my script with "sysparm_uuid=<sysid of the formula indicator here> ?



Pardon my noobish questions please. I am new to PA and don't have that much knowledge in scripting yet.



Appreciate your patience.