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

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.