AddQuery is not working on Business Rule

Avee678
Tera Contributor

Hello Everyone,
I have a requirement to update the start_date field on the cmdb_software_product_model table using the start_date from version records (). These version records are created under the related list of the sam_custom_product_lifecycle table.
Whenever a version record is created, the corresponding record's start_date should be updated with the version's start_date.
To achieve this, I wrote an "After Insert" Business Rule on the sam_custom_product_lifecycle table. In the script, I'm using GlideRecord to query .  I’m using the product field to query due to the relationship between the two tables.
The Product field from both tables are referring to same product table. The product from cmdb_software_product_model  and product form sam_custom_product_lifecycle are matching in the logs. but still not going to IF loop.
I've exhausted all approaches I could think of, but I'm unable to resolve this. I'm attaching my script below—please suggest possible solutions or improvements. Your help would be greatly appreciated!

( function executeRule ( current , previous /*null when async*/ )
var lifecycleGR = new GlideRecord ( ' sam_sw_product_lifecycle ' ) ;
lifecycleGR.addQuery ( ' norm_product ' , current.norm_product ) ; lifecycleGR.query ( ) ;
if ( lifecycleGR.next ( ) ) { gs.info ( ' SPM - SCL : ' + lifecycleGR.sys_id ) ;
gs.info ( ' SPM - SCL : 1+ lifecycleGR.norm_product ) ;
var plSysID = lifecycleGR.sys_id ;
var softwareModelGR = new GlideRecord ( ' cmdb_software_product_model ' ) ;
softwareModelGR.addQuery ( ' product' , lifecycleGR.norm_product) ;
softwareModelGR.query ( ) ;
if ( softwareModelGR.next ( ) ) {
gs.info ( '
Found related Software model record : ' + softwareModelGR.sys_id ) ;
softwareModelGR.u_start_of_life_date = current.start_date ;
softwareModeIGR.update ( ) ;
gs.info ( '
Update End date for Software model record : ' + softwareModelGR.sys_id ) ;
else{
gs.info ( '
No related Software model record found for product : ' + current.norm_product ) ;
}
}
} ) ( current , previous )

 

7 REPLIES 7

sunil maddheshi
Tera Guru

@Avee678 

 

  • Table Name Typo

    • You're querying 'sam_sw_product_lifecycle', but your requirement states 'sam_custom_product_lifecycle'. Ensure the correct table name.

  • Field Name Mismatch

    • You're using 'norm_product' in both tables. Verify that:

      • 'norm_product' is the correct field in both tables.

      • It stores the same value type (sys_id vs. display name vs. reference).

  • Syntax Issues

    • gs.info(' SPM - SCL : 1+ lifecycleGR.norm_product ) ; has a misplaced + and missing quotes.

    • softwareModeIGR.update(); has a typo (softwareModelGR should be used).

Try with below code:

(function executeRule(current, previous) {
    // Query sam_custom_product_lifecycle table based on norm_product
    var lifecycleGR = new GlideRecord('sam_custom_product_lifecycle');
    lifecycleGR.addQuery('norm_product', current.norm_product);
    lifecycleGR.query();
    
    if (lifecycleGR.next()) {
        gs.info('SPM - SCL Found: ' + lifecycleGR.sys_id);
        gs.info('SPM - SCL norm_product: ' + lifecycleGR.norm_product);

        // Query cmdb_software_product_model using norm_product
        var softwareModelGR = new GlideRecord('cmdb_software_product_model');
        softwareModelGR.addQuery('product', lifecycleGR.norm_product);
        softwareModelGR.query();
        
        if (softwareModelGR.next()) {
            gs.info('Found related Software model record: ' + softwareModelGR.sys_id);
            softwareModelGR.u_start_of_life_date = current.start_date; // Ensure the correct field name
            softwareModelGR.update();
            gs.info('Updated start_date for Software model record: ' + softwareModelGR.sys_id);
        } else {
            gs.info('No related Software model record found for product: ' + current.norm_product);
        }
    } else {
        gs.info('No matching lifecycle record found for product: ' + current.norm_product);
    }
})(current, previous);

Please mark correct helpful if this helps you!

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Avee678 

try this -> are you using correct field names?

check what came in logs?

Any query business rule on "sam_sw_product_lifecycle" is restricting the query?

(function executeRule(current, previous /*null when async*/) {
    // Query the sam_sw_product_lifecycle table
    var lifecycleGR = new GlideRecord('sam_sw_product_lifecycle');
    lifecycleGR.addQuery('norm_product', current.norm_product);
    lifecycleGR.query();

    if (lifecycleGR.next()) {
        gs.info('SPM - SCL : ' + lifecycleGR.sys_id);
        gs.info('SPM - SCL : ' + lifecycleGR.norm_product);

        // Query the cmdb_software_product_model table
        var softwareModelGR = new GlideRecord('cmdb_software_product_model');
        softwareModelGR.addQuery('product', lifecycleGR.norm_product);
        softwareModelGR.query();

        if (softwareModelGR.next()) {
            gs.info('Found related Software model record : ' + softwareModelGR.sys_id);
            softwareModelGR.u_start_of_life_date = current.start_date;
            softwareModelGR.update();
            gs.info('Updated start_date for Software model record : ' + softwareModelGR.sys_id);
        } else {
            gs.info('No related Software model record found for product : ' + current.norm_product);
        }
    } else {
        gs.info('No related lifecycle record found for product : ' + current.norm_product);
    }
})(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

@Avee678 

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