How to get a value from a table using client script

ragz
Tera Expert

Hi All,

I need to get a value from another table based on the department value .

I have a list called abc contacts . It has 3 columns and all are reference fields .

1) Department 2) Telecom contact 3) Department head

find_real_file.png

 

below script

var answer=response.responseXML.documentElement.getAttribute("answer");

I am getting the value of answer is Product Management so I need to get Department Head value Carmel Overfelt (example).

How to get that ?

find_real_file.png

1 ACCEPTED SOLUTION

lunkki
Tera Guru

Hello,
Not sure if I understood this correctly.
You can't use GlideRecord query on client side.


You need to create Script include:
name: u_getDepartmentHead

Client callable [x]

Script:

// create class
var u_getDepartmentHead = Class.create();
u_getDepartmentHead.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	//function name
	getContact: function() {
		
		// search parameter for query
		var department = this.getParameter('sysparm_department');
	
		//query
		var gr = new GlideRecord('u_abc_contacts');
		gr.addQuery('u_department', department);
		gr.query();
		if (gr.next()) {
			//return data
			return gr.u_department_head;
		}
	}

});

 

Now you can create client script to get that data:


var department = g_form.getvalue('u_department'); 

var ga = new GlideAjax('u_getDepartmentHead'); //this is the script include
ga.addParam("sysparm_name", "getContact"); //this is the function within the script include
ga.addParam("sysparm_department", department);
ga.getXML(getResponse);


function getResponse(response) {
    //set u_department_head sys_id to variable data
    var data = response.responseXML.documentElement.getAttribute('answer'); 

    g_form.setValue("man", data);
}

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ragz,

you have missed gr.query() line after your gr.addQuery()

also use if condition instead of while if you are sure there would be only 1 record

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur

thanks , tried below still no luck .what am I missing ?

 

 var answer=response.responseXML.documentElement.getAttribute("answer");
  jslog(response.responseXML);
   var gr = new GlideRecord('u_abc_contacts'); //Indicate the table to query from

 gr.addQuery('u_department',answer);
 gr.query();
 //Execute the query
  alert(answer);
 
  
  if(gr.next())
   {
     alert(gr.department_head);
   }

Jim Coyne
Kilo Patron

So it looks like you are already using a GlideAjax to get some data, correct?  If so, you should actually send back all the data you need to make it simpler and quicker.  Can you share the Script Include code as well as all of the Client Script code?

Yes Jim

Here is the script include

 

var userDetails  = Class.create();

userDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getManager:function() {
   var gr=new GlideRecord("sys_user");
   gr.addQuery("sys_id",this.getParameter('sysparam_id'));
   gr.query();
   gr.next();
 if(gr.manager!=''){
   return gr.manager.name.toString();
      }
   else
   {
   return "No manager ";
   }
   },
 
 
 getDepartment:function() {
   var gr=new GlideRecord("sys_user");
   gr.addQuery("sys_id",this.getParameter('sysparam_id'));
   gr.query();
   gr.next();
 if(gr.department!=''){
   return gr.department.name.toString();
      }
   else
   {
   return "No department ";
   }
   },
 
 
 
 
type: 'userDetails'


});