How Autopopulate assignment group field??

naveenmyana
Giga Expert

Hi guys,

I need to autopopulate the assignment group field on form load, I have wrtiten the below client script code.

I am able to populate the group field, But going down the road If we upgrade the instance to higher version does it cause any effect...Please correct me I am going in the right way or not???

function onLoad() {

    //Type appropriate comment here, and begin script below

  var x = 'eecdfd9bff131100a0a5ffffffffffe9'; // sys_id of the group name

  g_form.setValue('assignment_group',x);

}

1 ACCEPTED SOLUTION

Hi Naveen,,



Although I am against the use of gliderecords on client script as earlier mentioned, indirectly you are hardcoding scripts which is even less reliable. But nevertheless in the code,please modify to


function onLoad() {


if(g_form.isNewRecord()){


  //Type appropriate comment here, and begin script below


  var gr = new GlideRecord('sys_user_group');


  gr.addQuery('name',"Facilities request assignment group");   //as per my experience group name changes later during the time.


  gr.query();


  if(gr.next()){


  g_form.setValue('assignment_group',gr.sys_id);


  }


}


}


View solution in original post

26 REPLIES 26

Use Dictionary Override for default value. that wont affect anything.


Sharique Azim
Mega Sage

Hi Naveen,



Keep in your mind the client script would keep assigning the group   whenever   it is opened,which I dont think would be ever expected.



I would rather restrict it   only new records on the table   . try this:



function onLoad() {


    //Type appropriate comment here, and begin script below


if(g_form.isNewRecord()){


  var x = 'eecdfd9bff131100a0a5ffffffffffe9'; // sys_id of the group name



  g_form.setValue('assignment_group',x);


}


}



Pro tip : Use an existing onLoad script to append lines rather creating one.


Hi Azim,



Thank you so much for your response.. You have added a valuable point here. But I have used the below script here as hardcoding using sys_id is not a good practise.



Can u please suggest me where i can do this change you requested..


function onLoad() {


  //Type appropriate comment here, and begin script below


  var gr = new GlideRecord('sys_user_group');


  gr.addQuery('name',"Facilities request assignment group");


  gr.query();


  if(gr.next()){


  g_form.setValue('assignment_group',gr.sys_id);


  }


}


Hi Naveen,,



Although I am against the use of gliderecords on client script as earlier mentioned, indirectly you are hardcoding scripts which is even less reliable. But nevertheless in the code,please modify to


function onLoad() {


if(g_form.isNewRecord()){


  //Type appropriate comment here, and begin script below


  var gr = new GlideRecord('sys_user_group');


  gr.addQuery('name',"Facilities request assignment group");   //as per my experience group name changes later during the time.


  gr.query();


  if(gr.next()){


  g_form.setValue('assignment_group',gr.sys_id);


  }


}


}


Hi Azim,



Thank you so much for stopping by...


For now I will use this scrpit and may be if I use glideajax will that fix the issue... I will try that as well and let u know



Thanks,


Naveen