Help: Business Rule is not working.

YummyAmericano
Tera Contributor

Developers,

I am seeking guidance on resolving an issue that is currently impeding progress. In my scope application, I have implemented a Business Rule that validates supplier matches and creates/updates records accordingly. However, I have encountered a problem with a particular section of the script that fails to update the value in the Global. Specifically, it is unable to update the 'u_last_verified_date' field with today's date. This field is of the Date type. I would greatly appreciate your advice on how to rectify this issue. Thank you in advance.

Here is the BR script (part that is not working):

 // Update 'u_last_verify_date' in 'u_current_sup' table in Global to today's date

// I tried this and it is not working
        supplierGR.u_last_verify_date = gs.nowDateTime();
        supplierGR.update();*/

// I tried this and it is not working
		var today = new GlideDate();
		supplierGR.u_last_verify_date = today;
		supplierGR.update();



Here is the BR Script in the scope application:

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

    // Check if there is a matching supplier in 'u_current_sup'
    var supplierGR = new GlideRecord('u_current_sup');
    if (supplierGR.get('u_supplier_code', current.supplier_code)) {

        // Reconcile for existing supplier code
        var outputGR = new GlideRecord('x_new_sup');
        outputGR.addQuery('supplier_code', current.supplier_code);
        outputGR.query();

        if (outputGR.next()) {
            // Update existing record for reconciliation
            // Bring over the status to the output table
            outputGR.new_status = current.supplier_status; // copy over new active status
            outputGR.current_status = supplierGR.u_active; // copy over current active status
            outputGR.update();
        } else {
            // Create a new record for reconciliation
            outputGR.initialize();
            outputGR.supplier_code = current.supplier_code;
            outputGR.supplier_name = current.supplier; //supplierGR.supplier;
            outputGR.new_status = current.supplier_status;
            outputGR.current_status = supplierGR.u_active;
            outputGR.insert();
        }

        // Update 'u_last_verify_date' in 'u_current_sup' table in Global to today's date
        /*supplierGR.u_last_verify_date = gs.nowDateTime();
        supplierGR.update();*/

		var today = new GlideDate();
		supplierGR.u_last_verify_date = today;
		supplierGR.update();


    } else {
        // Reconcile for new supplier code
        var newSupplierOutputGR = new GlideRecord('x_new_sup');
        newSupplierOutputGR.initialize();
        newSupplierOutputGR.supplier_name = current.supplier;
        newSupplierOutputGR.supplier_code = current.supplier_code;
        newSupplierOutputGR.new_status = current.supplier_status;
        newSupplierOutputGR.current_status = current.supplier_status; // Assuming 'u_supplier_status' for new suppliers
        newSupplierOutputGR.new_supplier = true; // set new supplier to true
        newSupplierOutputGR.insert();
    }

})(current, previous);

 

12 REPLIES 12

Thanks for the info. Since my current method is working, I will try this method when I have a moment. 

Thanks again for your suggestions. I implemented your suggestion, but unfortunately, it did not yield the desired results. I have configured the CRUD operations for both tables. The table in the Global scope is where I want the update to occur, and the table in the Application scope triggers the corresponding business rule. Despite the script appearing to work based on my logs, it fails to update the value in the Global table. See the logs below:

YummyAmericano_0-1703078224369.png

 

I resolved the issue by creating a dedicated Business Rule in the Global scope.
Despite the field 'u_last_verify_date' being set as a Date type field, it is being updated correctly using GlideDateTime(), which only updates the value with the Date portion. Fortunately, I have found a solution to this issue and I am satisfied with the progress I have made. Here is the code snippet that successfully resolved the problem:

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

    // Check if there is a matching supplier in 'u_current_sup'
    var supplierGR = new GlideRecord('u_current_sup');
    if (supplierGR.get('u_supplier_code', current.supplier_code)) {

        // Reconcile for existing supplier code
        var outputGR = new GlideRecord('x_new_sup');
        outputGR.addQuery('supplier_code', current.supplier_code);
        outputGR.query();

        // Update 'u_last_verify_date' in 'u_current_sup' table in Global to today's date
        var nowDateTime = new GlideDateTime();
        supplierGR.u_last_verify_date = nowDateTime.getDisplayValue();
        supplierGR.update();
    }

})(current, previous);



@YummyAmericano : 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.