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.

Related list based on Account

TanayaGavande
Tera Expert

Hi All,

 

I have a requirement on the Incident form related list, i.e. the list of incidents with their parent as the form incident. I only have the Edit UI action present on the list, and I want to add filter based on the Account of the main incident, i.e. If the Account of the parent incident is XYZ company, I want the list to show incidents of the XYZ company only, when I click 'Edit' on the related list.

 

I tried adding filter on the list control, but I don't know how to make it dynamic according to the form. Any help is appreciated.

 

Thanks.

1 ACCEPTED SOLUTION

Satishkumar B
Giga Sage
Giga Sage

@TanayaGavande Worked for me. check the below code .

var uri = action.getGlideURI();
uri.set('sysparm_m2m_ref', current.getTableName());
uri.set('sysparm_collection_related_file', current.getTableName());
uri.set('sysparm_form_type', 'o2m');
uri.set('sysparm_stack', 'no');

// Generate redirect URL
var redirectUrl = uri.toString('sys_m2m_template.do');

// Extract sys_id from the URL
function extractSysId(url) {
    var start = url.indexOf('sysparm_collectionID=') + 'sysparm_collectionID='.length;
    return start === -1 ? null : url.substring(start, start + 32);
}

var extractedSysId = extractSysId(redirectUrl);
gs.info('Extracted Sys ID: ' + extractedSysId);

// Query and update URI based on the extracted sys_id
var gr = new GlideRecord(current.getTableName());
if (gr.get(extractedSysId)) {
    gs.info('Service Offering Field Value: ' + serviceOffering);
    uri.set('sysparm_query', 'company=' + gr.getValue('company'));
} else {
    uri.set('sysparm_query', '');
}

action.setRedirectURL(uri.toString('sys_m2m_template.do'));

 

……………………………………………………………………………………………………

Please Mark it helpful and Accept Solution!! If this helps you!!

View solution in original post

11 REPLIES 11

Shivam_Kumar
Tera Contributor

Hello @TanayaGavande , you can add query to the edit UI action itself. Parsing "uri" variable you will get the sys_id of the incident record and you can add script on UI action to get company of the current incident and add query for company in "uri.set('sysparm_query', '');". This sysparm_query is responsible for the filter on the list opening after clicking edit button.

ShivamKumar5_0-1721713046110.png

 

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

 

TanayaGavande
Tera Expert

Thanks for your reply @Shivam_Kumar and @Satishkumar B ! This works, but not for the reference field. The company is a Reference field. When I log current.sys_id, I get the incident sys id as expected, but if i dot walk to current.company.sys_id, the result shows null, so the filter does not work. I also tried with GlideRecord on the Incident table to get the current's account, but it shows null still.

Here's my script:

TanayaGavande_0-1721753497570.png

Am I doing anything wrong?

Also, is there any way we can hide this filter?

TanayaGavande_1-1721733622199.png

 

Hello @TanayaGavande  -  Using "current.sys_id" you will not get current incident sysID. you can add the below code to get incident sysId and use this sys_id to get account.

var uri = action.getGlideURI();
var path = uri.getFileFromPath();
// Get the parameter value
var url = uri.toString();
var urlParams = url.split('&');
var incidentsysId;
for (var i = 0; i < urlParams.length; i++) {
    var param = urlParams[i];
    if (param.startsWith('sysparm_collectionID=')) {
        incidentsysId = param.split('=')[1];
        break;
    }
}
gs.addInfoMessage("INCIDENT sysId: "+incidentsysId);
uri.set('sysparm_m2m_ref', current.getTableName());
uri.set('sysparm_collection_related_file', current.getTableName());
uri.set('sysparm_form_type', 'o2m');
uri.set('sysparm_stack', 'no');
uri.set('sysparm_query', 'active=true');
action.setRedirectURL(uri.toString('sys_m2m_template.do'));


Shivam_Kumar_0-1721812747635.png

 

 

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Thanks for your reply @Shivam_Kumar , however, I have no problem getting the current incident's sys id. What I need is the sys id of the Company field of the incident, as I want to add the filter based on incident company, i.e. 

uri.set('sysparm_query', 'company='+{incident.company.sys_id});

This is just a sample but I want something like this.

I tried dot-walking and also Glide Query, and even when I have the Incident sys id, I get the Company's sys id as null in the logs. 

 

Is there any way to do this?

 

Thanks