- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 12:06 AM - edited 04-03-2024 01:31 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 04:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 01:30 AM
Hi @Community Alums ,
what exactly you trying to achieve?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 01:34 AM
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:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 01:38 AM
ok got it @Community Alums
i need to know both parent and sold products refers to the same table?? if yes Table name:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 03:09 AM
Yes. Its same table