How to create or update record when date field changes from empty using business rule

JRY
Mega Guru

Hi,

 

I'm trying to achieve below-point using a business rule, but it's not working properly. Now it's creating record only when date field getting empty. Can someone tell me what went wrong with my script or conditions?

  1. Create a record if the date field changes from empty to any date.
  2. Update existing records if the date field changes from any date to any date.

JRY_1-1671020014251.png

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

    var grOrder = new GlideRecord('u_trac_orders');
    grOrder.initialize();
    grOrder.u_store_number = current.sys_id;
    grOrder.u_order_type = 'New Install';
    grOrder.u_service_type = 'Broadband';
    grOrder.needs_attention = true;
    grOrder.u_fixture_date = current.u_fixture_date;
    grOrder.work_notes = "Fixture Date Has been Changed To" +  ' - '  + grOrder.u_fixture_date;
    grOrder.insert();

})(current, previous);

 

Thanks,

JRY

 

 

 

 

1 ACCEPTED SOLUTION

With the 2 Filter Conditions I suggested, the BR will not run if the current date changes to empty, which is what it sounded like you wanted from your 2 original criteria.  If you want to create a new record if the current date is empty or the previous date was empty (which doesn't really make sense as then you will create another new record when the date is populated) you would want only one Filter Condition when Fixture Date changes, then no condition on the Advanced tab.  The Script would look more like this:

(function executeRule(current, previous /*null when async*/ ) {
    if (previous.u_fixture_date == '' || current.u_fixture_date == '') {
        var grOrder = new GlideRecord('u_trac_orders');
        grOrder.initialize();
        grOrder.u_store_number = current.sys_id;
        grOrder.u_order_type = 'New Install';
        grOrder.u_service_type = 'Broadband';
        grOrder.needs_attention = true;
        grOrder.u_fixture_date = current.u_fixture_date;
        grOrder.work_notes = "Fixture Date Has been Changed To" +  ' - '  + grOrder.u_fixture_date;
        grOrder.insert();
    } else {
        var grOrder = new GlideRecord('u_trac_orders');
        grOrder.addQuery('u_store_number', current.sys_id);
        grOrder.query();
        if (grOrder.next()) {
            grOrder.u_fixture_date = current.u_fixture_date;
            grOrder.work_notes = "Fixture Date Has been Changed To" +  ' - '  + grOrder.u_fixture_date;
            grOrder.update();
    }
})(current, previous);

If you don't want it to run when the current date is empty, leave both Filter Conditions, still get rid of the Condition on the Advanced tab, and just take the || current... out of the first if statement.

 

View solution in original post

5 REPLIES 5

Mohit_Gupta
Tera Guru
Tera Guru

Hi @JRY ,

 

It would be great if you apply the filter only date field changes and in the advance code just get the previous object to check if previous object value is empty then create a record or if previous object have value then update the  records.

 

I hope this helps 

Please mark my answer correct if this helps you 

Thanks

Mohit

Hi Mohit,

Thanks for the reply, but if I change the condition to "changes," then it will create a new record every time I change the date, but I wanted a new record to be created only when the date field changes from empty to any date, and if there is already a date in the date field, then if I try to change the date to any other date, it should only update the record, with no need to create a new record.

I hope you got what I needed; if not, please let me know.

 

Thanks,

JRY

Brad Bowman
Kilo Patron
Kilo Patron

Change the When field to 'before' so that you have access to the previous object, then change the first Filter Condition to 'is not empty', then on the Advanced tab populate the Condition field with:

previous.u_fixture_date == ''

  

Hi Brad,

 

Thanks for your reply. I have tried your suggestion, but here on conditions, I'm a bit confused. If I check the filter condition that the fixture date is not empty, then it will not run when it's empty, right? If it's empty, it should run and create a new record when I try to add a date to the field, and if there is already a date in the field when I try to change it, it should only update the date on an existing record in the Order form.

Thanks,

JRY