- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I am creating records in the Announcement [announcement] table using a Record Producer.
I have added this Record Producer to multiple portals (e.g., Service Portal (SP) and HR Portal). When a user submits the Record Producer, the Announcement record gets created successfully.
However, I want to automatically associate the Announcement with the correct portal in the Portals related list (m2m_announcement_portal) based on where the Record Producer was submitted.
How can this be done?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @George_1
Try this once:
- Create a hidden Single line text variable in your record producer
- Create a onLoad Catalog client script.
function onLoad() {
try {
var portal = spAnnouncement.getPortal(); // Native portal API
if (portal) {
g_form.setValue('current_portal', portal.sys_id);
}
} catch (e) {
var portalURL = top.location.href;
if (portalURL.indexOf('id=') > -1) {
var portalName = g_form.getParameter('id');
g_form.setValue('current_portal', portalName);
}
}
g_form.setDisplay('current_portal', false);
}
- Update producer script:
var ann = new GlideRecord('announcement');
ann.initialize();
ann.name = 'Announcement created by ' + current.number;
ann.title = producer.short_description;
ann.summary = producer.description;
ann.from = new GlideDateTime();
ann.insert();
var userPortal = producer.current_portal;
if (userPortal) {
var m2m = new GlideRecord('m2m_announcement_portal');
m2m.initialize();
m2m.announcement = ann.sys_id;
m2m.sp_portal = userPortal;
m2m.insert();
}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
the only option is this
1) create a hidden string variable to hold the name of portal
2) use onLoad catalog client script to grab the portal name from URL
function onLoad() {
// Check if we are in the Service Portal
var url = top.location.href;
if (url.indexOf('/sp') > -1)
g_form.setValue('hiddenvariable', 'sp');
else if (url.indexOf('/hr') > -1)
g_form.setValue('hiddenvariable', 'hr');
}
3) use record producer script to get the portal name and then grab the sys_id and use in your script
var portalName = producer.hiddenvariable;
var gr = new GlideRecord("sp_portal");
gr.addQuery("url_suffix", portalName);
gr.query();
if (gr.next()) {
// you got your portal sysid here
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @George_1
Though DM manipulation is not recommended approch , but you have seen top.location.href is part of DOM manipulation.
Ensure you unchecked Isolate Script on the Client Script record so DOM/Window objects can be modified.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Could you please share screenshot from your PDI if you got the portal name using the line you shared?
var portal = spAnnouncement.getPortal(); // Native portal API
it would be helpful for members.
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
the only option is this
1) create a hidden string variable to hold the name of portal
2) use onLoad catalog client script to grab the portal name from URL
function onLoad() {
// Check if we are in the Service Portal
var url = top.location.href;
if (url.indexOf('/sp') > -1)
g_form.setValue('hiddenvariable', 'sp');
else if (url.indexOf('/hr') > -1)
g_form.setValue('hiddenvariable', 'hr');
}
3) use record producer script to get the portal name and then grab the sys_id and use in your script
var portalName = producer.hiddenvariable;
var gr = new GlideRecord("sp_portal");
gr.addQuery("url_suffix", portalName);
gr.query();
if (gr.next()) {
// you got your portal sysid here
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I didn't get any error when used in portal
see below
share your complete script and config screenshots
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader