How do I populate assigned to field based on the entity's owner?

ChuanYanF
Tera Guru

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);
        }
      });
    }
  }
}
1 ACCEPTED SOLUTION

Hi @ChuanYanF ,

 

the other g_scratchpad approach

place this BR on your table

ChaitanyaILCR_0-1745465647090.png

 

 

 

ChaitanyaILCR_3-1745465870188.png

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

https://www.youtube.com/watch?v=77D7DMIaG24

https://www.servicenow.com/community/itsm-articles/quot-use-of-server-side-script-g-scratchpad-and-c...

View solution in original post

g_scratchpad object is used to pass information from service to client. I this video i have provided the brief demo that how we can use g_scratchpad in servicenow and how we can use g_scratchpad object in display business rule (server side) and in client script (client side). Please visit below ...
5 REPLIES 5

Chaitanya ILCR
Kilo Patron

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

 

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

 

Hi @ChuanYanF ,

 

the other g_scratchpad approach

place this BR on your table

ChaitanyaILCR_0-1745465647090.png

 

 

 

ChaitanyaILCR_3-1745465870188.png

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

https://www.youtube.com/watch?v=77D7DMIaG24

https://www.servicenow.com/community/itsm-articles/quot-use-of-server-side-script-g-scratchpad-and-c...

g_scratchpad object is used to pass information from service to client. I this video i have provided the brief demo that how we can use g_scratchpad in servicenow and how we can use g_scratchpad object in display business rule (server side) and in client script (client side). Please visit below ...

@ChuanYanF 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader