Not able to find the root of sold product

Community Alums
Not applicable

HI Team,

 

I need to find the parent of sold product . I have written below script to achieve this. But when I run it , I get message in logs as "I am Parent: Undefined"

 

 

Please help in fixing the issue

 

 

 

var parentProd = getParent(current.sold_product);
gs.info('I am Parent:' + parentProd);
 
function getParent(sp) {
   
    var grb = new GlideRecord('sn_install_base_sold_product');
    grb.addQuery('sys_id', sp);
    grb.query();
    if (grb.next()) {
        if (!grb.parent_sold_product.sys_id.toString()) {
            return grb.sys_id.toString();
        } else {
           getParent(grb.parent_sold_product.sys_id.toString());
        }
    }
1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @Community Alums 
your requirement needs server side validation and for this -- First you need to create the Script include then a business rule or other script include.
Script Include: Please check the fields and table name according to your correct names:

 getTopParent: function(productSysId) {
        var currentSysId = productSysId; // Starting record
        var parentSysId;
        
        // Safety check to avoid infinite loop
        var safetyCounter = 0;
        
        // Continue looping until there’s no parent record found
        do {
            var gr = new GlideRecord('product'); // Replace ‘product’ with your actual table name
            if (gr.get(currentSysId)) {
                parentSysId = gr.getValue('parent_record'); // Assuming ‘parent_record’ is your reference field name
                // If there’s a parent, set it to be the next record to get in the next iteration
                if (parentSysId) {
                    currentSysId = parentSysId;
                }
            } else {
                // In case the record doesn’t exist or other errors
                return null;
            }
            
            safetyCounter++;
            if(safetyCounter > 100) { // Prevent too many iterations
                return 'Error: Too many iterations, please check the data integrity';
            }
            
        } while (parentSysId);
        // When no more parent records are found, return the ID of the top most parent record
        return currentSysId;
    },

Once the Script Include is ready, you can use it from a Business Rule,

var topParentSysId = new FindTopParent().getTopParent('YOUR_PRODUCT_SYS_ID');
gs.info('Top most parent sys_id: ' + topParentSysId);



Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma

View solution in original post

6 REPLIES 6

Deepak Shaerma
Kilo Sage

Hi @Community Alums 
your requirement needs server side validation and for this -- First you need to create the Script include then a business rule or other script include.
Script Include: Please check the fields and table name according to your correct names:

 getTopParent: function(productSysId) {
        var currentSysId = productSysId; // Starting record
        var parentSysId;
        
        // Safety check to avoid infinite loop
        var safetyCounter = 0;
        
        // Continue looping until there’s no parent record found
        do {
            var gr = new GlideRecord('product'); // Replace ‘product’ with your actual table name
            if (gr.get(currentSysId)) {
                parentSysId = gr.getValue('parent_record'); // Assuming ‘parent_record’ is your reference field name
                // If there’s a parent, set it to be the next record to get in the next iteration
                if (parentSysId) {
                    currentSysId = parentSysId;
                }
            } else {
                // In case the record doesn’t exist or other errors
                return null;
            }
            
            safetyCounter++;
            if(safetyCounter > 100) { // Prevent too many iterations
                return 'Error: Too many iterations, please check the data integrity';
            }
            
        } while (parentSysId);
        // When no more parent records are found, return the ID of the top most parent record
        return currentSysId;
    },

Once the Script Include is ready, you can use it from a Business Rule,

var topParentSysId = new FindTopParent().getTopParent('YOUR_PRODUCT_SYS_ID');
gs.info('Top most parent sys_id: ' + topParentSysId);



Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma

Deepak Shaerma
Kilo Sage

@Community Alums  can you mark the replies and answer as Helpful also, This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma