set the value using gform.setvalue not working

MMKK
Tera Expert

Hi Team,

I have a requirement:

- To Populate the user groups based   on the userlogin

- to take a portion of user group after "-" and populate it in text field.

This should happen onSubmitting the form.

However, While debugging my script , I observed that the value populates on the field but once the form is saved, the value seems to be blank.

Please help me   with this. Here are my scripts:

Script Include:

var LookupUserGroups = Class.create();

LookupUserGroups.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

      type: 'LookupUserGroups',

getUserGroups : function (){

  // get parameters

  var user_id = this.getParameter('sysparm_user_id')+'';

  // query sys user group table for records

  var group_ids = []; // declare array

  var grm_rec = new GlideRecord('sys_user_grmember');

  grm_rec.addQuery('user',user_id);

  grm_rec.query();

  while(grm_rec.next()){

  // collect group sys_id's into array

  group_ids.push(grm_rec.group.name+'');

  }

  var group_ids_str = group_ids+'';

  // return results

  var ajax_result = this.newItem("result");

  ajax_result.setAttribute("group_ids", group_ids_str);

  }

});

Client Script:

function onSubmit() {

    //Type appropriate comment here, and begin script below

  //Type appropriate comment here, and begin script below

    g_form.setReadOnly('users_groups',true);

// get user_id

  //var user_id = g_form.getValue('u_reference_2')+'';

//alert(user_id);

var user_id = g_user.userID;

// call ajax getter function

  if(user_id!=''){

  lookupGroups(user_id);

  }

// ajax getter

function lookupGroups(user_id){

  var ajax = new GlideAjax('LookupUserGroups');

  ajax.addParam('sysparm_name', 'getUserGroups');

  ajax.addParam('sysparm_user_id', user_id);

  // get results

  ajax.getXML(getGroupsResult);

}

// ajax callback

function getGroupsResult(serverResponse){

  var result = serverResponse.responseXML.getElementsByTagName("result");

  var group_ids = result[0].getAttribute("group_ids");

  if(group_ids!=''){

  debugger;

  alert(group_ids);

  var array = group_ids.split(',');

  var regPattern=/^[L][1-3]/ig;

  var memberFirm=[];

  array.forEach(function(item){

  if(regPattern.test(item)){

  var memberFirmarrFromItem=item.split('-');

  if(memberFirmarrFromItem.length > 1){

  // memberFirm.push(trim(memberFirmarrFromItem));

  if(memberFirm.indexOf(memberFirmarrFromItem[1])== -1)//memberFirm.push(trim(memberFirmarrFromItem)):;

  {

  memberFirm.push(memberFirmarrFromItem[1].trim());

  }

  }

  }

  });

// if(memberFirm.length ==1){

  //debugger;

  g_form.setValue('logged_in_user_member_firm',memberFirm[0]);

  g_form.submit();

 

// }//

  //g_form.setValue('users_groups',group_ids);

  // this stores a comma separated string of sys_ids in the List field

  }

}

   

}

I see that every thing works fine with the script except setting the values. Please let me know what is wrong here or any best practise .

4 REPLIES 4

ruzzty06
Tera Expert

I don't quite get your requirement. If you need to do some data manipulation using an onSubmit script, why not just do this in a business rule before insert?


And why are you trying to do   g_form.setvalue() on an onSubmit script, maybe you only need to change something on the form when you select a user group? so maybe what you need is an onChange script?


Hi Rutsy,



I wanted to get the logged in users groups and take a text in it to perform some actions.



If I use on_change then I have to define on a field which will be triggered   when a field is changed. In my case, the details should be calculated every time you save the record.



I wanted to extract say "Test User Group - Australia". I want to extract Australia and keep it in a field. Based on which I do some operations.


ajax.getXML(getGroupsResult);



This is an Async function that your submission might have taken place before the answer arrives.


GlideAjax - ServiceNow Wiki


See this for synchronously calls


This is bad practice though and will not work in Service portal


Hi Manasa,




A business rule before insert and update would be the best solution for you.




here's what the code should look like.




var tmp=current.user_group_field; //assign the text to process to tmp variable




if(tmp)


{


        if(tmp.indexOf('-')>-1)


        {


                  var tmp =tmp.substring(tmp.indexOf('-')+1, tmp.length);


                current.new_field=tmp; //replace new_field with your field name to put the extracted text



        }


}