- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2020 07:05 AM
Hello all,
I am new to Service Portal, creating my very first sample widget today. I have this piece of code below where after clicking "Edit'" button and then clicking on "Save" button, the record should get updated in the incident table. I am able to make changes after clicking "Edit" button, but I am stuck w.r.to updating the changes upon clicking "Save" button. Could you please help me on this.
Serverside:
(function() {
data.incidentdata=[];
var gr=new GlideRecord('incident');
gr.addEncodedQuery('state=2');
gr.query();
while(gr.next())
{
var dataobject={};
dataobject.priority=gr.getDisplayValue('priority');
dataobject.short_description=gr.getDisplayValue('short_description');
dataobject.state=gr.getDisplayValue('priority');
data.incidentdata.push(dataobject);
}
})();
Clientside:
function($scope) {
var c = this;
$scope.display=true;
$scope.saveRecord=function () {
}
}
HTML:
<div>
<p ng-repeat='incident in data.incidentdata'>
<span ng-if='display'>{{incident.short_description}}</span>
<input type='text' ng-model='incident.short_description' ng-if='!display'/>
<button ng-click='display=false'>Edit</button>
<button ng-if='!display' ng-click='saveRecord()'>Save</button>
</p>
</div>
Moreover, I've one more question. How can we use this controller "c" in client script? What is the difference between initializing variables/functions with "$scope" and "c". I did go through a few articles on these, but it will be really helpful if you can explain with an example.
Thank you.
Solved! Go to Solution.
- Labels:
-
Service Portal Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2020 11:52 AM
Hi Vinay,
To clear up when to use $scope or c or the difference between $scope or c:
AngularJs inherently has a $scope upon creating/define the ng-app. Each controller has their own scope, just like a function.
A directive (which is what Service Portal's widgets are) has access to scope and can have its own isolated scope. This is what ServiceNow has setup for Service Portal widgets (isolated scopes) so that each widget can have multiple instances on the same page.
But when you want access to the parent scope you need to inject that into the controller.
ie. function($scope) { ....
"c" in the widget is the same as a normal "this" in a function.
The HTML template is the view and it has access to the inherent $scope. So theoretically you can create and assign values in the HTML template.
If something like this is in the HTML template:
<input type="text" ng-model="myvar" ng-init="myvar ='hello'" />
You wouldn't be able to access it in the controller unless you injected $scope and called $scope.myvar
So instead of initializing the variable value in the view the better way to do it would be to first set it in the controller which can be done in a couple of ways:
Controller:
function($scope) {
...
$scope.myvar = 'hello';
...
<input type="text" ng-model="myvar" />
Notice the ng-init doesn't have to be used
The other way is to use c
Controller:
function(){
...
var c = this;
c.myvar = 'hello';
...
HTML:
<input type="text" ng-model="c.myvar" />
Notice $scope doesn't have to be injected.
However, both of these can exist together but they are two totally different objects and you would have to access them respectively to their "scope".
None of this has anything really to do with updating the actual database value if the value is coming from records.
In the previous posts, everyone is trying to tell you that the function "$scope.saveRecord" isn't doing anything.
The function would need to send that data back to the server script and then have logic to actually update the desired record.
An example:
Server Script:
(function(){
if(input){
var gr = new GlideRecord('incident');
gr.get(input.sys_id);
if(gr.getUniqueValue()){
gr.short_description = input.short_description;
gr.update();
}
}
})()
In the gif below you'll see an example of using some "update" functions that do different things.
You can see here that it doesn't necessarily matter whether you use $scope.server.update or c.server.update (there is also ".server.get" that allows you to add items to 'input').
But it does matter knowing how to update the variables' values properly. Another thing to know is that server.update/get has a .then/promise where you can refresh/update the view if necessary. That is where using data directly in the HTML template can come in handy. If the logic allows to limit to manipulating the properties on data, these would automatically get updated once data is updated if one-way binding isn't enforced.
Attached is the widget I created. Play around with it.
I hope this helps and didn't confuse you more.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2020 07:22 AM
Hi,
Use this server side script and client side script:
(function() {
data.incidentdata=[];
var gr=new GlideRecord('incident');
gr.addEncodedQuery('state=2');
gr.query();
while(gr.next())
{
var dataobject={};
dataobject.priority=gr.getDisplayValue('priority');
dataobject.short_description=gr.getDisplayValue('short_description');
dataobject.state=gr.getDisplayValue('priority');
data.incidentdata.push(dataobject);
}
if(input.action == 'saveRecord'){
here gliderecord the incident which you want update by using
incident.short_description //this will hold new value which is entered in that field.
}
})();
Client:
function($scope) {
var c = this;
$scope.display=true;
c.saveRecord=function () {
c.server.get({action:'saveRecord'}).then(function(r){
if(r=='success'){
spUtil.addInfoMessage('Updated Successfully');
}
});
}
}
For Scope and c: https://community.servicenow.com/community?id=community_blog&sys_id=7a6d2e29dbd0dbc01dcaf3231f961962
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2020 09:13 AM
Hi Ashutosh,
It's not working. My requirement is if I edit the short description of a record in HTML side, then using the sys id of the record, it should get updated in server side. I've tried in various ways, but in vain.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2020 09:45 AM
Hi,
Can you show me the UI how the incidents are displayed and how are you editing it.
Thanks
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2020 09:53 AM