Service Portal: Client Side Data to Server Side

Dhruv Chandan
Giga Guru

Hi All,

I've created a custom field using HTML and now i want to get the value being input in that field towards the server side in Service Portal.

HTML Part:-

<div>

  <img ng-src="{{::c.data.logo}}" />

  <a href="?id={{::c.data.homepage}}">Click here to go home</a>

</div>

<div>Enter your Last name:

<input type="text" ng-model="c.data.lastname" id="LN" ng-change="c.displayNew()"/>

<h2>{{ c.data.messageNew }}</h2>

</div>

Client Side Part:-

function() {
var c = this;
c.displayNew = function(){
c.data.messageNew = (c.data.lastname) ? 'Hello ' + c.data.lastname + '!' : '';
}
c.display();
c.server.update();

}

Server Side part:-

(function() {

  /* populate the 'data' object */

  /* e.g., data.table = $sp.getValue('table'); */

  console.log("Server Side"+input);

  var portalGr = $sp.getPortalRecord();

  console.log("Server Side"+portalGr);

})();

Any idea how to get the "Last Name" which is being input to the server side for further validations.

Any help is appreciated.

Regards,

Dhruv Chandan

1 ACCEPTED SOLUTION

Hi Dhruv,



You're close here. When using server.update() the data object is represented by the input object, so you would typically do something like this on the server:



if (input) {


      console.log("Server Side: " + input.lastname);


}



You don't need to write input.data, and service portal is unique in that it lets you use console.log() on both the client and server side script areas.


View solution in original post

6 REPLIES 6

larstange
Mega Sage

Hi



You are quite close. On the server side you get the input via "input.data.lastname".



Also see this for inspiration:



https://serviceportal.io/communicating-between-the-client-script-and-the-server-script-of-a-widget/


Hi Lars,



I tried "console.log("Server Side"+input.data.lastname);"



but it showing up as undefined in the logs even with the entries filled in.



Regards,


Dhruv Chandan


console.log is a client-side only javascript command


input.data. is server-side only



You can't mix the two.



You can do a gs.log though, then look through your servicenow logs to find what   you're trying to see.


Hi David,



gs.log("Server Side"+input.data.lastname,"dc31");



Used the above statement in the sever side code and the system logs is still showing it as undefined.



Regards,


Dhruv Chandan