Need code in styles for the business rule.

suryan
Kilo Expert

I wrote a business rule to update status indicator with color green if created date is less than   3 days, amber if it is equal to or greater than 4 days and red if it is equal to 5 or more than 5 days. Status indicator field updates only when insert or update action is performed since it is business rule. I need the code to write this in styles.

Below is the business Rule written, how to apply it in STYLES?

(function executeRule(current, previous /*null when async*/)

{

var currentdate = new GlideDate();

var created_date = current.getValue('opened_at');

var redcolorstate = new GlideDate();

redcolorstate.setValue(created_date);

redcolorstate.addDaysLocalTime(5);

var ambercolorstate1 = new GlideDate();

ambercolorstate1.setValue(created_date);

ambercolorstate1.addDaysLocalTime(4);

var ambercolorstate = new GlideDate();

ambercolorstate.setValue(created_date);

ambercolorstate.addDaysLocalTime(3);

if (currentdate >= redcolorstate)

  {

current.u_status_indicator = 3;

gs.addInfoMessage(current.u_status_indicator);

current.update();

  }

  else if (currentdate >= ambercolorstate   &&   currentdate <= ambercolorstate1)

  {

current.u_status_indicator = 2;

gs.addInfoMessage(current.u_status_indicator);

current.update();

  }

  else

  {

current.u_status_indicator = 1;

gs.addInfoMessage(current.u_status_indicator);

current.update();

  }

// Add your code here

})(current, previous);

1 ACCEPTED SOLUTION

Hi Suryan,



Happy to help



Try with the limit of only 10 records and check.


Also, try the same script in background script and check whether its takes the same time or not?



var gr = new GlideRecord('sn_customerservice_case');  


gr.addQuery('u_choice_6NOT IN4 - Ready for ISG testing,5a - ISG test successful,5b - ISG test failed,6 - Pushed to Production,7 - Closed,8 - Information Mail,91 - Rejected by Demand Board,92 - Cancelled by user');


gr.setLimit(10);


gr.query();




and before update set workflow false as well to stop any BR running after the script.



gr.setWorkfFlow(false);


gr.update();



You need to see whats causing the harm. Try different runs and see the output.


Should not be much of a hassle.




Thanks


Gaurav



PS: Mark the answer Correct. helpful based on the impact. This will help others to follow the solution.


View solution in original post

26 REPLIES 26

Iam not reflecting any color, iam just updating a Status indicator field as green /red/amber   based on the condition


Iam not reflecting any color, iam just updating a Status indicator field as green /red/amber   based on the condition. So to update status indicator automatically u said to do scheduled job so is it possible to send me a code how to do this?



Hi Surya,



So I got it wrong the whole time, what you are asking is that can you update the status indicator field everyday without any insert/update action, right??


If yes, then you need to define your data set on which you will apply the scheduled job.



This can be certainly achived with scheduled jobs of script execution type.find_real_file.png



find_real_file.png










var gr = new GlideRecord('table_name'); //replace table_name with your table name


gr.addQuery(''); // place your query to filter the exact data for which you want to update the status indicator


gr.query();


while(gr.next()){


//loop though all the records and update the indicator field








// replace all 'current' with 'gr' as now gr will give the record on which the script will run


var currentdate = new GlideDate();


var created_date = gr.getValue('opened_at');



var redcolorstate = new GlideDate();


redcolorstate.setValue(created_date);


redcolorstate.addDaysLocalTime(5);



var ambercolorstate1 = new GlideDate();


ambercolorstate1.setValue(created_date);


ambercolorstate1.addDaysLocalTime(4);



var ambercolorstate = new GlideDate();


ambercolorstate.setValue(created_date);


ambercolorstate.addDaysLocalTime(3);



if (currentdate >= redcolorstate)


  {


gr.u_status_indicator = 3;


gs.addInfoMessage(gr.u_status_indicator);


gr.update();


  }


  else if (currentdate >= ambercolorstate   &&   currentdate <= ambercolorstate1)


  {


gr.u_status_indicator = 2;


gs.addInfoMessage(gr.u_status_indicator);


gr.update();


  }



  else


  {


gr.u_status_indicator = 1;


gs.addInfoMessage(gr.u_status_indicator);


gr.update();


  }



// Add your code here


}




Please let me know if this helps.


  1. gr.addQuery(''); // place your query to filter the exact data for which you want to update the status indicator  
  2. gr.query();



gr.addquery(' ')   -> i want to run for all records u asked to place query to filter the data to run all records what i need to add inside the gr.addquery(' ')?



What i need to add here. I just want to check all   records, as said based on the date the status indicator should change to amber/red/green


So you want to run it on all the records in your table, if yes, then you can remove this line.


Just curious how many records do you have in your table so that you won't run into any performance issue.