Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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
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
Kilo Sage

Hi @Community Alums ,

what exactly you trying to achieve?

 

Community Alums
Not applicable

HI Deepak,

 

I want to get the Parent of Sold product i.e top most parent. 

Sold product  can have more than one parent. If you see the screenshot below:

Ankur20_0-1712133150065.png

Basically suppose there is a sold product called 'X' and it has parent called 'Y' and it can have further parent and so on. Therefore I want to get the super or top most parent record.

In case sold Product 'X' has no parent then I want 'X' to return as it itself is the parent.

ok got it  @Community Alums 
i need to know both parent and sold products refers to the same table?? if yes Table name:

Community Alums
Not applicable

Yes. Its same table