Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to pass client side input to server side in ServiceNow portal Widget

DB1
Tera Contributor

Hello All,

 

I have client side and server side scripting and I am trying to pass the input from client to server (script attached below).

I do get the first info message however not the second one. Need help on the same to pass the sysid of firstname from client to Server

Client:

 

	c.openexistingUserform=function(){  

		c.n={
			"firstname" :'',
			"busiphone" : '',
			"email1": '',
			"city1": ''		
		}			
		console.log("openexistingUserform" ,c.n);	
		c.data.action = 'addphUser';	
		c.server.update();
			
		document.getElementById("addexUserForm").style.display = "block";

	}	

 

I get the following output from the console

 

  1. busiphone: "(555) 555-0004"
  2. city1: ""
  3. email1: "abraham.lincoln@example.com"
  4. firstname: {value: 'a8f98bb0eb32010045e1a5115206fe3a', displayValue: 'Abraham Lincoln'}
  5. [[Prototype]]: Object

Server

 

 

if(input&&input.action=='addphUser')
	{
		gs.addInfoMessage("addphUser");
		var graddexuser2 = new GlideRecord('sys_user');
		graddexuser2.addQuery('sys_id',input.firstname);
		graddexuser2.query();
		if(graddexuser2.next())
		{
			gs.addInfoMessage("User " +graddexuser2.user_name);
			//data.existingUser.push({email:graddexuser2.email.toString(),busphone:graddexuser2.phone.toString()});
		}
	}

 

 

 

6 REPLIES 6

palanikumar
Giga Sage
Giga Sage

Try the below change:

 

Client Script

 

	c.openexistingUserform=function(){  
		c.data.n={
			"firstname" :'',
			"busiphone" : '',
			"email1": '',
			"city1": ''		
		};		
		console.log("openexistingUserform" ,c.n);	
		c.data.action = 'addphUser';	
		c.server.update();	
		document.getElementById("addexUserForm").style.display = "block";
	}	

 

Server Script:

if(input&&input.action=='addphUser')
	{
		gs.addInfoMessage("addphUser");
		var graddexuser2 = new GlideRecord('sys_user');
		graddexuser2.addQuery('first_name',input.n.firstname);
		graddexuser2.query();
		if(graddexuser2.next())
		{
			gs.addInfoMessage("User " +graddexuser2.user_name);
			//data.existingUser.push({email:graddexuser2.email.toString(),busphone:graddexuser2.phone.toString()});
		}
	}
Thank you,
Palani

DB1
Tera Contributor

Thanks @Ankur9 , @Mohith Devatte   and @palanikumar Actually the above solution did not work for some reason. I also have an Onload script to populate the data everytime the name is selected from the form as below

var c = this;
$scope.$on("field.change", function(evt, parms) {
//console.log(parms.newValue);
c.data.usersysid = parms.newValue;
if(parms.newValue =='')
{
c.n.email1 = '';
c.n.busiphone = '';
c.n.city1 = '';
c.n.firstname = '';
}
else{
c.data.action = 'fetchexistingUser';
c.server.update().then(function(){
c.n.email1 = c.data.existingUser[0].email;
c.n.busiphone = c.data.existingUser[0].busphone;
});
}
});

 

And then I have the script to hold the values of the data

 

c.openexistingUserform=function(){
c.data.n={
"firstname" :'',
"busiphone" : '',
"email1": '',
"city1": ''
};
console.log("openexistingUserform" ,c.n);
c.data.action = 'addphUser';
c.server.update();
document.getElementById("addexUserForm").style.display = "block";
}