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

In case of a new record nothing needs to be handled via an onLoad Script. The onChange will take care of the Fiscal period the moment end date is populated. For an existing record the value for fiscal record would always be there. OnLoad script is not needed in this case.

Hi Sandeep,

 

I tried this with OnChange CS, but its working only when I open the requested allocation form but before that I see Fiscal Period on Requested allocation table is blank in List view. So untill I open the form, Fiscal period is not getting populated and it's not getting saved to DB.

@SnowSB The onChange CS only works on the form and the change will not reflect on the DB until you save the changes. If you wish to populate the fiscal period on all the existing records then you will either have to run a background script or a fix script. 

Hi Sandeep, I don't want to fix the existing records. My requirement is when Requested Allocation records are getting created, at the same time Fiscal Period should be populated and saved to DB.

@SnowSB In this case the business rule is the best approach, since your BR is not working in case of insert and only working in case of update, I recommend you to find out a reason behind this issue first. You can use gs.info() in your business rule to check why the fiscal period field is not populating in case of insert. 

 

A combination of onChange client script and business rule can also be used. The BR will only populate the data when the current.fiscal_period field is empty.