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

Hi @Sumanth16 ,

Your suggested solution, to hide/show the related list itself, is very helpful, but not quite what I am looking for. I want the list to be visible, just not all the records that appear after I click the 'Edit...' UI Action. So I was looking for a way I can dot-walk to the company field. 

Thanks for your response.

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!!