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

Ankur Bawiskar
Tera Patron
Tera Patron

@ChuanYanF 

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.

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