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

Omkar Mone
Mega Sage

Hello,

 

Create a BR on Table A which will run after insert/update

 

below is the script you can try, please update your relevant field names in - 

 

(function executeRule(current, previous /*null when async*/) {
    if (current.cost_1 != previous.cost_1) {
        var cost1Value = parseFloat(current.cost_1 || 0);
        if (cost1Value === 0) return;

        var tableBGr = new GlideRecord('table_b');
        tableBGr.addQuery('number', current.number);
        tableBGr.query();

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

Ankur Bawiskar
Tera Patron
Tera Patron

@VIKAS MISHRA 

this should be an easy one

what did you start with and where are you stuck?

BR: After update on Table A

Condition: Field 1 [Changes] AND Field 1 [IS NOT EMPTY]

Script:

(function executeRule(current, previous /*null when async*/) {
    // Get the number from the current record in Table A
    var number = current.number;  // use correct field name
    var cost1 = current.cost1; // use correct field name

    // Query Table B to find a record with the same number
    var tableB = new GlideRecord('u_table_b'); // Replace 'u_table_b' with your actual Table B name
    tableB.addQuery('number', number);  // use correct field name
    tableB.query();

    if (tableB.next()) {
        // If a record is found, update the Cost1 field
        tableB.cost1 = tableB.cost1 + cost1;  // use correct field name
        tableB.update();
    } else {
        // If no record is found, create a new record in Table B
        var newRecord = new GlideRecord('u_table_b'); // Replace 'u_table_b' with your actual Table B name
        newRecord.initialize();
        newRecord.number = number;  // use correct field name
        newRecord.cost1 = cost1;  // use correct field name
        // Copy other fields from Table A to Table B as needed
        newRecord.insert();
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@VIKAS MISHRA 

Did you get a chance to check above script?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@VIKAS MISHRA 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader