- 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 07:55 PM
Hi @ChuanYanF ,
you don't have to modify the script the script looks correct
function onLoad() {
if (g_form) {
var profile = g_form.getValue('primary_profile');
alert('profile' + profile);
if (profile) {
alert('profile if ' + profile);
g_form.getReference('primary_profile', function(entity) {
alert('entity' + entity);
if (entity.owned_by) {
alert('entity.owned_by ' + entity.owned_by);
g_form.setValue('assigned_to', entity.owned_by);
}
});
}
}
}
put some alerts or any other debugging code in your client script and check
is primary_profile field available on the form?
if no this won't work and try to use g_scratchpad approach with display BR to fetch primary_profile info and use the g_scratchpad in the client script even better use g_scratchpad to fetch the entity.owned_by value and use that directly in client script as you don't have to use getReference get fetch data
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2025 08:21 PM
Hi Chaitanya,
Thanks for the advise but i put in the alert info and it only shows profile when I try to create a new risk event, others alerts didnt show. And for your next step which is the gscracth.pad i am not sure how it works
- 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:51 PM
it means that owned_by field on profile is empty and hence it didn't enter and didn't give the further alert
did you check that part?
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