Before Insert Business Rule not working as expected

SnowSB
Tera Contributor

Hi,

 

I am trying to populate the 'fiscal period' on Requested allocation based on start date and end date through before Insert/Update BR but it's not working. This BR is not running at all. Can someone please help me with this ?

Requested allocation record gets created when we submit resource plan.

 

SnowSB_0-1700942198554.png

 

 

SnowSB_1-1700942198554.png

 

 

SnowSB_2-1700942198565.png

 

 

1 ACCEPTED SOLUTION

This is likely related to the timezone offset pointed out below.  To work with the other field formats, the script would look like this:

 

(function executeRule(current, previous /*null when async*/ ) {
	var AllocStartDate = new GlideDateTime(current.start_date)
	AllocStartDate = AllocStartDate.getDate().toString().replace("-","") + "T000000";
    var AllocEndDate = new GlideDateTime(current.end_date)
	AllocEndDate = AllocEndDate.getDate().toString().replace("-","") + "T235959";
    var fsp = new GlideRecord('fiscal_period');
    fsp.addEncodedQuery("start_date_time<=" + AllocStartDate + "^end_date_time>=" + AllocEndDate + "^fiscal_type=quarter");
    fsp.query();
	gs.addInfoMessage("Allocation Date Range " + AllocStartDate + ' - ' + AllocEndDate)
    if (fsp.next()) {
        current.u_fiscal_period = fsp.sys_id;
        gs.addInfoMessage("Inside if : " + current.number);
    } else {
        gs.addInfoMessage("Inside else : " + current.number + " : " + current.start_date + " : " + current.end_date);
        //gs.addErrorMessage("Due to allocation mismatch, could not populate fiscal period.");
    }
})(current, previous);

 

If this still isn't working for all fiscal periods in your environment, I'll check it out in your PDI and likely go back to the other date fields with a timezone offset, if that's the issue.

View solution in original post

39 REPLIES 39

Brad pointed out the issue. It's OOB script include which is stopping all the scripts by setting setWorkflow(false) hence any BR (after/Before) insert is not running on this table.

@SnowSB In this case, you can use the onChange client script to populate the fiscal period when the new record is getting created via the form. 

That's where the issue is. Record is not getting created via form. When Resource Manager submits the Resource Plan -> Requested allocations gets created automatically through script include (OOB) which has setWorkflow(false) and because of that my BR (after/before insert) is not working. 

In this case, there is no other way but to customise the OOTB script include. Please follow the approach suggested by Brad.

It turns out there is a very logical, yet convoluted explanation why your Business Rule isn't running before Insert.  I'll go through it, but skip to the end for the fix.

 

There is a (out of box, as everything mentioned hereafter is) Business Rule on the resource_plan table that runs after Insert to create the Requested Allocation record.  This is done via a Script Include named ResourcePlan, which in turn calls a Script Include named ResourcePlanRequest, which calls yet another Script Include named RequestedAllocation.  The lines of code which actually create the Requested Allocation record start at line 295.  Line 311, just before the insert() which creates the record, contains setWorkflow(false), which means no other Business Rules will run when the record is inserted.

 

Short answer, comment out line 311, and your before Insert and Update Business Rule is all you need.  To avoid a skipped record next update, you could create a copy of this Script Include to make the revision, then you would have to make a copy of all the previous ones to call your copy of each, all the way back to inactivating the out of box Business Rule to instead run a copy of it that calls your copy of the ResourcePlan Script Include.