The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Client Script help!

shane_davis
Tera Expert

I need some help creating a script for the process below.   Unfortunately, my limited scripting abilities isn't getting me the required results.   Any help is appreciated!

My full process is:

1. I open a Change Task which is assigned to me.

2. I choose my Failure Type and Failure Cause choices which puts an integer in the Failure Score (u_failure_score) field.

  •         All 3 fields are on the change_task table

3. The script should take the Failure Score and add it to the Failed Change Score field (u_failed_change_score) on the User table for the user assigned to the parent Change Request.

In addition, I need to delete the value of u_failed_change_score every 91 days because we only want to keep 90 days history in this field.

Below is code I've tried to create, but it's obviously not all I need...

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

//Get value of u_failed_change_score on the sys_user table, add that to the u_failure_score on the change_task table, and then overwrite the current user's score with the new score.

var totalusrscore = g_form.getReference('sys_user');

// var f2 = g_form.getIntValue('totalusrscore.u_failed_change_score'); //Field on the sys_user table

var f1 = g_form.getIntValue('u_failure_score'); //Field on the change_task table

var total = f1 + totalusrscore.u_failed_change_score; //Add the 2 fields together to get the sum

g_form.setValue('f2', total); //set the score on the sys_user table to the new score

}

1 ACCEPTED SOLUTION

I would create a business rule to run "After" the record has updated. This will only add the current updated score to the existing score on their user record. See the scheduled job below for how to remove anything previous to 90 days.



Here is the script:


(if you're on ServiceNow Eureka, check the Advanced checkbox)


Insert = True


Update = True


When to run = After


Condition: current.u_failure_score.changes()


Script:


var u_usr = new GlideRecord('sys_user');


u_usr.addQuery('sys_id',current.change_request.assigned_to);


u_usr.query();


if (u_usr.next()){


u_usr.u_failed_change_score += current.u_failure_score;


u_usr.update();


}




Also since you want to remove anything past 90 days old, I believe you should have a daily scheduled job to recalculate the score that runs this script:


var u_usr = new GlideRecord('sys_user');


var u_cTask = new GlideRecord('change_task');


var u_sum = new GlideAggregate('change_task');



//Find all change tasks created in the last 90 days


u_cTask.addEncodedQuery('sys_created_onONLast 90 days@javascript:gs.daysAgoStart(90)@javascript:gs.daysAgoEnd(0)');


u_cTask.query();


while (u_cTask.next()){


var u_at = u_cTask.change_request.assigned_to;




//aggregate the sum of the failure scores grouped by assigned to


u_sum.addAggregate('SUM', 'u_cTask.u_failure_score');


u_sum.groupBy(u_at);


u_sum.query();


while (u_sum.next()){


var total = u_sum.getAggregate('SUM','u_cTask.u_failure_code');




//Here is where you should set that value back to the user record by updating the sys_user table.


//I'm not 100% sure of the code here, but add gs.log statements in the script above to see what


//output you are getting.




//You need something like for each assigned to user, set the u_failed_change_score to be the


//sum aggregate we got from the above query.








View solution in original post

29 REPLIES 29

kopp820
Mega Guru

Let me know how it goes - the scheduled job will be the most complex part and i can help out some more once you do some testing. Thanks Shane!



Hi Kailey,



        I had a meeting with the requestors of the change and we decided on a better design.   Triggering the business rules to calculate off of the Successful and Unsuccessful UI Actions when there are multiple rollbacks in the workflow was simply more complex than it needed to be!



        Instead, I am controlling displaying/removing States based on the change task.   This is working much better.   I have the Successful business rule working based on the screenshot below.   I do not want the user's score to count down if their score is 0 OR if it has already counted down once....due to workflow rollbacks.


Successful.PNG



        Unfortunately, the Unsuccessful business rule isn't working so great!   With the code below, it doesn't calculate at all....whether u_failure_score is empty or not.   The score will not always change when state 41 (Change Unsuccessful) is chosen.   It could be that a score is chosen, the workflow progresses and is then rolled back, but the current score still remains.   Any ideas?   I had a bunch more code that I tried with else's and if's, but no luck there either....


Unsuccessful.PNG


So I was able to get part of the Unsuccessful rule working.   Everything above the else works.   In essence, if the user's score is nothing, it adds the score from the change task.   It's not subtracting any previous score and adding a new score...     I'll keep chugging at it!



var user = new GlideRecord('sys_user');


user.addQuery('sys_id',current.change_request.assigned_to);


user.query();


if (user.next()){


if (user.u_failed_change_score == 0 || user.u_failed_change_score == ''){


  user.u_failed_change_score += current.u_failure_score;


  user.update();


}


else


  user.u_failed_change_score -= previous.u_failure_score;


  user.u_failed_change_score += current.u_failure_score;


}


add some gs.log statements in your if/else to see if your code is getting to those points.



try this in your last else, when you expect it to get to that point:



gs.log('The failed change score is ' + previous.u_failure_score);


I've never used debug so I must be doing something wrong.   I searched the left nav for "debug" and clicked "Debug Business Rule."   Then, I went to my change task, changed the failure type and cause which changed the score, and right-clicked Save.   I don't see anything related to the gs.log statement I added.     Shouldn't I see the text where the log displays at the bottom of the task I update?



var user = new GlideRecord('sys_user');


user.addQuery('sys_id',current.change_request.assigned_to);


user.query();


if (user.next()){


if (user.u_failed_change_score == 0 || user.u_failed_change_score == ''){


  user.u_failed_change_score += current.u_failure_score;


  user.update();


}


else {


  if (previous.user.u_failed_change_score != current.user.u_failed_change_score){


    gs.log('The failed change scrore is' + previous.u_failure_score);


    user.u_failed_change_score -= previous.u_failure_score;


    user.u_failed_change_score += current.u_failure_score;


  }


}


}