how insert a new record or update existing one through BR script

VIKAS MISHRA
Tera Contributor

In table A we have decimal type field named "cost 1" and if this field amount changes then BR should run and glide the table B and check if table B is already having one record where field "number" is already existing same as the record in table A then it should simply sum of Cost1 field with Cost 1 field from table B and add it in the table B Cost1 field else it should create a new record in table B with all the same information such as Cost1 field value.

Please suggest how to write this script thorugh BR

5 REPLIES 5

yuvarajkate
Giga Guru

Business Rule Configuration

  • Table: Table A
  • When to Run: After
  • Conditions: cost1 changes
  • Script: Use the following script:

 

(function executeRule(current, previous) {
    var tableBGR = new GlideRecord('table_b');
    tableBGR.addQuery('number', current.number);
    tableBGR.query();

    if (tableBGR.next()) {
        tableBGR.cost1 = parseFloat(tableBGR.cost1 || 0) + parseFloat(current.cost1 || 0);
        tableBGR.update();
    } else {
        var newRecord = new GlideRecord('table_b');
        newRecord.initialize();
        newRecord.number = current.number;
        newRecord.cost1 = current.cost1;
        newRecord.insert();
    }
})(current, previous);