Help with before business rule

VirendraKuD
Tera Contributor

Hi Team, 

 

I have requirement where I have two tables, custom table "XYZ Data" and another table "Asset (alm_asset)". I have a reference field on "XYZ Data" called Asset Name which is referenced to Asset table. I want to restrict users to map single record in XYZ data to single asset.

 

If Asset Name in XYZ Data record 1 is mapped to Asset 1 in asset table. Then system should restrict users from mapping XYZ data record 2 to Asset 1.

 

I have written this Before Business rule ( Insert and Update):

(function executeRule(current, previous /*null when async*/) {
    // Log the current asset sys_id
    gs.info('Business Rule triggered for asset: ' + current.u_asset_name);

    var existing = new GlideRecord('x_xyz_data');
    existing.addEncodedQuery('u_asset_name=' + current.u_asset_name + '^sys_id!=' + current.sys_id);
    existing.query();

    if (existing.next()) {
        gs.info('Duplicate mapping found for asset: ' + existing.u_asset_name);
        gs.addErrorMessage('This asset is already mapped to another XYZ data.');
        current.setAbortAction(true);
    } else {
        gs.info('No duplicate mapping found. Proceeding with save.');
    }
})();

 

 

 

I am undefined in gs.info - gs.info('Business Rule triggered for asset: ' + current.u_carear_product_name);

I am not getting values from fields into Before BR.

 

Thanks in advance

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@VirendraKuD 

this should work in before insert & update BR provided your field name is correct

I am using GlideAggregate for optimization

Note: you should use correct table name and field

(function executeRule(current, previous /*null when async*/) {
    // Log the sys_id of asset reference
    gs.info('Business Rule triggered for asset sys_id: ' + current.u_asset_name);

    // Use GlideAggregate to count how many records already map this asset excluding current record (for update)
    var ga = new GlideAggregate('x_xyz_data');
    ga.addQuery('u_asset_name', current.u_asset_name);
    ga.addQuery('sys_id', '!=', current.sys_id);
    ga.addAggregate('COUNT');
    ga.query();

    if (ga.next()) {
        var count = parseInt(ga.getAggregate('COUNT'), 10);
        if (count > 0) {
            gs.info('Duplicate mapping found for asset sys_id: ' + current.u_asset_name);
            gs.addErrorMessage('This asset is already mapped to another XYZ data.');
            current.setAbortAction(true);
        } else {
            gs.info('No duplicate mapping found. Proceeding with save.');
        }
    }
})(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

View solution in original post

9 REPLIES 9

Bhuvan
Tera Sage

@VirendraKuD 

 

Change the logic like below and if needed enhance and try it,

 

(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr = new GlideRecord("x_xyz_data");
gr.addQuery("u_asset_name", current.u_asset_name);
gr.query();
if (gr.getRowCount() > 0) {
current.setAbortAction(true);
gs.addErrorMessage(gs.getMessage("Not allowed the duplicate Asset: " + current.u_asset_name));
}
}
})(current, previous);

If this helped to answer your query, please mark it helpful & accept the solution. 

 

Thanks,

Bhuvan

G Ponsekar
Mega Guru

Hi @VirendraKuD ,

 

Are you referring to the correct backend name of the Asset field? What is the field type ?

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

OlaN
Giga Sage
Giga Sage

Hi,

Your question is a bit confusing, in the script you supplied there is no statement around:
"gs.info('Business Rule triggered for asset: ' + current.u_carear_product_name);"
Which you said is causing the issue.

Care to explain a bit more ?

Ankur Bawiskar
Tera Patron
Tera Patron

@VirendraKuD 

this should work in before insert & update BR provided your field name is correct

I am using GlideAggregate for optimization

Note: you should use correct table name and field

(function executeRule(current, previous /*null when async*/) {
    // Log the sys_id of asset reference
    gs.info('Business Rule triggered for asset sys_id: ' + current.u_asset_name);

    // Use GlideAggregate to count how many records already map this asset excluding current record (for update)
    var ga = new GlideAggregate('x_xyz_data');
    ga.addQuery('u_asset_name', current.u_asset_name);
    ga.addQuery('sys_id', '!=', current.sys_id);
    ga.addAggregate('COUNT');
    ga.query();

    if (ga.next()) {
        var count = parseInt(ga.getAggregate('COUNT'), 10);
        if (count > 0) {
            gs.info('Duplicate mapping found for asset sys_id: ' + current.u_asset_name);
            gs.addErrorMessage('This asset is already mapped to another XYZ data.');
            current.setAbortAction(true);
        } else {
            gs.info('No duplicate mapping found. Proceeding with save.');
        }
    }
})(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