calling server.get() & server.update() -Server Script from Client controller

Ian Elrick1
Kilo Expert

Hi

I am under the impression that you call server.get() to retrieve data from the server via the server script.

Likewise,  you call server.update() to save\update data to the server via the server script.

 

Can anyone illustrate how you differentiate between both sets of code in a server script when the view form needs to do both save and view?

ie where do you group the server script code for the server . get so an update does not call it and vice versa?

 

Thanks in advance

Regards

 

Ian

5 REPLIES 5

ChrisBurks
Mega Sage

Hi Ian,

 

With the two different methods you can actually perform similar actions. But the real difference is that server.get() would be used to place properties on the input object and expect to "get" variable values and possibly specifically assign those values.

Where the server.update() you may not be expecting anything back and can ignore the promise.

In the example I provide it's a widget that does some basic things.

The server.get() method is going to place a static sysid on the input object and send it to the server where I expect to grab some information from an incident record and when retrieved I will place that in a variable of my choosing. Client-side controller lines 5-14; Server-side script lines 4-6;

With the server.update(), I'm basically doing the same thing except the input properties being sent across are automatically taken from my ng-models in the html markup. And when it gets to the server side it will handle the data and I don't really care to do anything else with it aside from the data object being set with the new data. Notice that server.update() actually just updates the data object back to the server. It doesn't actually update any records. You specifically have to script updating a record. (In the video when I fill in the description field is when I want to update the record).

find_real_file.png

 

 

This is not really a practical example but it should give you an idea how to use the methods. But really each method could be used to pass data to the server and update a record as well as retrieve data back.

One more loop to throw in. server.get() and server.update() can also be called from spUtil. The difference here is that $scope can be specified when sending.

References:

Client Script example

Client Script APIs

Server Script example

Server Script APIs

 

 

Hi Chris,

 

Fantastic example. Thank you very much. But I have one doubt. I see that you use "c.data.updateMe" for one ng-model, and "data.sdescription" for another ng-model. What exactly the the prefix "c." is. I know it is the controller, but I am not able to grasp when to use it and when not to. Could you please explain with an example. Thank you again. 

Hey Vinay,

The data object is from the Server Side and can be used in both the HTML Body template and the Controller.

"data" can be accessed directly in the HTML Body Template because the template is already in the $scope or you can use c.data.

In the controller you have to use data through $scope or if c is defined in the controller (sometimes it's not). ie. $scope.data or c.data

When to use:

Of course from the description above if working in the controller client script you have to use $scope.data or c.data. But why do this when the template can use either or?

The reason is that more control of the data might be necessary. Especially when you have a flow. Let's say initially you have a list of users coming from the server-side: data.userlist.

However, that list might need to change in some ways during client slide and not server-side. One might do something like this in the controller:

// server-side defines data.userlist as an array

c.userlist = c.data.userlist.map(function(item){
     // adding an avatar retrieved from a fictitious client side service
    item.image = FictitiousService().getAvatarId(item.av.id);

      return item;
});

and in the HTML use c.userlist

<ul class="list-group">
  <li class="list-group-item" ng-repeat="u in c.userlist">
       {{u.name}}
       <span>
          <img src="{{item.image}}"  class="avatar-circle" />
       </span>
</li>

</ul>

 

Probably not a solid example but it should do.

Conclusion is that ServiceNow gave us access directly to server-side data if needed which is awesome combined with AngularJS' two-way binding feature.

So if you really didn't need to do anything with the data retrieved from the server side you can simply use it in the HTML template bypassing the controller.

I'm sure there's more to it than that but I'm continuously and definitely don't know everything about. Other community input is definitely welcome.

Hi Chris,

 

Thank you very much. I had already started creating my first widget, but I ran into an issue. I already posted a question regarding this, but I haven't received any solution yet. Could you please look into this. 

 

https://community.servicenow.com/community?id=community_question&sys_id=3842bf601b84d014ada243f6fe4b...