How to update a record for a table by using widget scripts in servicenow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 12:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 12:31 AM
Hi,
please provide the scripts here for HTML, client and server and don't add screenshots
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 01:22 AM
HTML (updating user table)
<div class="panel panel-default">
<div class="panel-body border background" ng-repeat="n in data.UserInfo">
<div class="form-group">
<label>Name</label>
<input class="form-control" ng-model="n.name">
</div>
<div class="form-group">
<label>Phone</label>
<input class="form-control" ng.model="n.phone">
</div>
<div class="form-group">
<label> Mobile Phone</label>
<input class="form-control" ng.model="n.mobile_phone">
</div>
<div class="form-group">
<label>Email</label>
<input class="form-control" ng.model="n.mobile_phone">
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-primary" ng-click="c.updateUser(n)">
</div>
</div>
.
<!-- your widget template -->
</div>
-----------------------------------------------------
Client Script
api.controller=function() {
/* widget controller */
var c = this;
c.updateUser=function(n) {
c.action="update"
c.data.phone=n.phone;
c.data.mobile_phone=n.mobile_phone;
c.data.email=n.email;
c.server.update();
location.reload();
}
}
---------------------------------------
Server Script
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
data.UserInfo=[];
var gr= new GlideRecord('sys_user');
gr.addQuery('sys_id', gs.getUserID());
gr.query();
if(gr.next())
{
data.UserInfo.push({
name:gr.name.toString(),
phone:gr.phone.getDisplayValue().toString(),
mobile_phone:gr.mobile_phone.toString(),
email:gr.email.toString(),
id:gr.sys_id.toString()
});
}
if(input && input.action=="update")
{
var upd= new GlideRecord('sys_user');
upd.addQuery('sys_id', gs.getUserID());
upd.query();
if(upd.next())
{
upd.phone=input.phone;
upd.mobile_phone=input.mobile_phone;
upd.email=input.email;
upd.update();
}
}
})();
HTML(creating a new record for incident table)
<div class="panel panel-default">
<div class="panel-body border background">
<div class="form-group">
<label>Description</label>
<input class="form-control" ng-model="n.description">
</div>
<div class="form-group">
<label>Short Description</label>
<input class="form-control" ng.model="n.short_description">
</div>
<div class="form-group">
<input type="submit" value="Create Incident" class="btn btn-primary" ng-click="c.test(n)">
</div>
</div>
<!-- your widget template -->
</div>
---------------------------------------
Client Scrip
api.controller=function() {
/* widget controller */
var c = this;
c.test = function(n) {
c.data.value1 = n.description;
c.data.value2 = n.short_description;
c.server.update();
//location.reload();
}
};
-------------------------------------
Server Script
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
var gr = new GlideRecord('incident');
gr.initialize();
gr.setvalue('descritption',input.value1);
gr.setvalue('short_descritption',input.value2);
gr.insert();
})();