The CreatorCon Call for Content is officially open! Get started here.

Add Condition to Script to Not Run if Date is Empty/Blank

richelle_pivec
Mega Guru

I have two scripts that are not playing well with each other. The first is a UI Action button to "Copy Change" which allows my team members to copy the basic details of the existing change, putting the state back to "Planning" and removing the Start and End Dates, etc...:

UI Action: COPY CHANGE

Table: Change Request (change_request)

Order: 80

Action Name: sysverb_insert_and_stay

Condition: gs.hasRole("itil")

Script:

doInsertAndStay();

function doInsertAndStay() {

      var saveMe = current;

current.u_change_state = "";

current.closed_at = "";

current.u_start_date = "";

current.u_end_date = "";

current.u_implemented_as_planned_ = "";

current.u_unplanned_event = "";

current.u_details_of_unplanned_event = "";

current.u_cab_followup_date = "";

current.u_cab_review_date = "";

current.u_cab_approval = "";

current.wf_activity = "";

      current.insert();

      action.setRedirectURL(saveMe);

}

Secondly, I wrote business rules that enforce the lead times for the Changes (Minor - 2 Days, Significant - 5 days, Major - 10 days). The new scripts work great on a new Change, but when the Copy Change button is used, it fails because the "lead time for the change is not being met"...with (now) blank dates.

So, I thought I'd try to change the criteria on the business rule to have it exclude changes that have no Start or End Dates. (The fields are required for new Changes, so team members won't be able to Save a new Change without them. What I don't know how to write is the condition that says u_start_date is empty and u_end_date is empty. Here's what I have for the Minor - 2 Days business rule:

Business Rule: Start Time in 2 Days

Table name: Change Request (change_request)

Order: 100

Run at Server

When to Run: Before

Condition: current.type == "Normal" && current.risk == 4 && current.u_start_date != null && u_current.u_end_date != null;

Script:

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

      // Add your code here

              //gs.nowDateTime() will give the current date/time in GMT  

          //current.work_start will give the corresponding time in GMT. Replace work_start with your appropriate field name  

          var diffSeconds = gs.dateDiff(gs.nowDateTime(),current.u_start_date, true); //will store difference as number of seconds  

          if (diffSeconds < 172800) //172800 is the number of seconds in 2 days  

          {  

          current.setAbortAction(true);  

                        gs.addInfoMessage("A Normal, Minor Change needs to have 2 days for lead time.");

          }  

})(current, previous);

I tried adding filter conditions, but that made the script not run at all. Is "null" in the condition supposed to be something else? Like "empty"? How do I change the condition so that it runs when those fields are empty?

Thanks,


Richelle

1 ACCEPTED SOLUTION

Thank you for your suggestion. Unfortunately sometimes they want to leave it in "Planning" without a Start and End date while they build the change/figure out the details.



Here's what I ended up doing. I shut off the UI Policies that were sometimes making the Start Date and End Date Required when the change was in the Planning State.


Then I removed the "condition" from Advanced tab on the Business Rules and built out the Conditions (only) on the "When to Run" tab. I used:



Type is Normal (as we don't want the enforcement for Expedited or Emergency changes)


Risk is (Minor, Significant, or Major - depending on which business rule I was modifying)


Start Date is not empty


End Date is not empty



So, the Copy Change button works because the Start and End Dates are empty in them and the Lead Time enforcement works because Start and End Time are required to have a different change state (Pending Mgr Approval, Approved, etc...).



thanks,


Richelle


View solution in original post

2 REPLIES 2

kristenankeny
Tera Guru

Rather than creating the change immediately upon "copy" being clicked, what about invoking a UI Page that prompts them for the start/end dates, and using that to create the record? You can add validation of the dates to the button on the UI Page.


Thank you for your suggestion. Unfortunately sometimes they want to leave it in "Planning" without a Start and End date while they build the change/figure out the details.



Here's what I ended up doing. I shut off the UI Policies that were sometimes making the Start Date and End Date Required when the change was in the Planning State.


Then I removed the "condition" from Advanced tab on the Business Rules and built out the Conditions (only) on the "When to Run" tab. I used:



Type is Normal (as we don't want the enforcement for Expedited or Emergency changes)


Risk is (Minor, Significant, or Major - depending on which business rule I was modifying)


Start Date is not empty


End Date is not empty



So, the Copy Change button works because the Start and End Dates are empty in them and the Lead Time enforcement works because Start and End Time are required to have a different change state (Pending Mgr Approval, Approved, etc...).



thanks,


Richelle