- 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 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 05:49 AM
@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