- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2025 07:40 PM
Dear experts,
I would like to autopopulate the assigned_to field in my form based on the entity (primary_profile) that is in the table. Below is my onLoad client script. May I know which part should I modify to make it work?
function onLoad() {
if (g_form) {
var profile = g_form.getValue('primary_profile');
if (profile) {
g_form.getReference('primary_profile', function(entity) {
if (entity.owned_by) {
g_form.setValue('assigned_to', entity.owned_by);
}
});
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2025 08:47 PM
Hi @ChuanYanF ,
the other g_scratchpad approach
place this BR on your table
BR Script
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
g_scratchpad.primaryProfile = current.primary_profile;
g_scratchpad.primaryProfileOwnedBy = current.primary_profile.owned_by;
})(current, previous);
client script
function onLoad() {
var curAssignedTo = g_form.getValue('assigned_to');
if (g_form
&& !g_form.isNewRecord() /*making it not a new record*/
&& g_scratchpad.primaryProfile
&& !curAssignedTo /*making sure currently it's not assinged*/
&& curAssignedTo != g_scratchpad.primaryProfileOwnedBy
/*making sure it's value is different*/
) {
/*update the if conditions as per your need*/
g_form.setValue('assigned_to', g_scratchpad.primaryProfileOwnedBy);
}
}
you can refer this for g_scratchpad approach
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2025 08:08 PM
script looks fine.
try adding alert and see as mentioned by @Chaitanya ILCR if the field primary_profile is on form
if not then you need to take alternative approach
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader