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

First code snippet is to be written in display BR for setting the value of red color.


Second one is for client script and yes you can't acces current object here, if you want to set the value of indicator, please do it in BR itself.



  1. if (currentdate >= redcolorstate)  
  2.   {  
  3. g_scratchpad.redColor ="true";
  4. current.u_status_indicator = 3;
  5. current.update();
  6. }

i done it in BR itself before but it is updating when i insert or update, so i need to update it automatically so i asked how to write this in Styles, so that it can update automatically.


Sorry, couldn't understand your concern here.


Can you please elaborate a bit.


Above said is the BR written to update the status indicator field(color to green/amber/red). it is changing once we update any values to record based on created date. I just want the status indicator field to update it automatically based on the date without any manual update or insert so instead of BR styles can be done in service now. i need the code in styles for the above said BR.


You need a scheduled job then which can run every day automatically(but can't apply CSS with it, you can only update the status indicator value) as a BR will always need a trigger event.( Insert, Update, query, display).


I would still go for a display BR because whenever you will go to see data, it will reflect the color,


The only case would be scheduled reporting where it won't work.


Do you have such case??



Thanks


Gaurav