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

Satishkumar B
Giga Sage
Giga Sage

@TanayaGavande Worked for me

SatishkumarB_0-1721927493786.png

 

Hi @Satishkumar B , thanks a lot! This worked for me. However, is there any way I can make this filter read only or hide it? I don't want someone to simply remove it and get access to all the records.

TanayaGavande_0-1721928190455.png

 

Satishkumar B
Giga Sage
Giga Sage

Hi @TanayaGavande 
can you please update your below code in your Edit button. below code worked for me to get the sysid of company and was able to filter based on that.

 

//Initialize URI and set parameters
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'));

 

 

SatishkumarB_0-1721923210864.png


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

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

Satishkumar B
Giga Sage
Giga Sage

Hi @TanayaGavande 
can you please update your below code in your Edit button. below code worked for me to get the sysid of company and was able to filter based on that.

 

//Initialize URI and set parameters
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'));

 

 

 

 

SatishkumarB_0-1721923210864.png

 

 

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

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

Sumanth16
Kilo Patron

Hi @TanayaGavande ,

 

 

Hi @TanayaGavande , 

 

Please refer to below thread:

https://www.servicenow.com/community/developer-forum/is-it-possible-to-show-related-list-conditional...

 

https://www.servicenow.com/community/developer-forum/how-to-hide-a-related-lists-based-on-certain-co...

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks & Regards,

Sumanth Meda