Loop through multiple risk hierarchy until main parent is found

Chenab Khanna
Tera Expert

Hi all,

 

I have a requirement to show all Risk statements of a level 0 risk statement in the reference qualifier. 

For instance - If Risk Statement A is a level 0 risk that is selected, then the other field should display list of all Risk statements having parent as A in all hierarchies. 

If A is a parent of B, B is a parent of C and so on...

then If A is selected, the other field will display these records - B, C...and so on

 

Can anyone help me in scripting this?

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Chenab Khanna 

try this in your script include and call that in ref qualifier

var RiskHierarchyUtils = Class.create();
RiskHierarchyUtils.prototype = {
    initialize: function() {},

    getChildRisks: function(parentRiskId) {
        var childRisks = [];
        this._getChildRisksRecursive(parentRiskId, childRisks);
        return childRisks;
    },

    _getChildRisksRecursive: function(parentRiskId, childRisks) {
        var gr = new GlideRecord('risk_statement');
        gr.addQuery('parent', parentRiskId);
        gr.query();
        while (gr.next()) {
            childRisks.push(gr.sys_id.toString());
            this._getChildRisksRecursive(gr.sys_id, childRisks);
        }
    },

    type: 'RiskHierarchyUtils'
};
javascript: 'sys_idIN' + new RiskHierarchyUtils().getChildRisks(current.risk);

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Chenab Khanna 

also check this link where I shared solution few years ago for something similar but for manager

Recursive script 

also check this link

Fetch records using recursion 

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

Phil Swann
Tera Guru
Tera Guru

the risk statement table [sn_risk_definition] has a root field risk_statement_root