Portal page record not saved
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2024 08:57 PM
Hello Community,
I have a page with a widget. Whenever I click on update on the page, I want the record to saved, removed from the list and update record from the backend.
The backend update is working as expected. But the record did not save on the page and the selected value on state field clear out. Once saved, it should be removed from the list of the records for the user.
Below are the scripts for my widget.
Widget Screenshot below
In Stock state was selected. But once I click update, the In Stock value will be cleared.
The requirement is to removed the asset from the list once the state is updated.
HTML
<div>
<table>
<tr>
<th>Asset</th>
<th>Requested For</th>
<th>State</th>
</tr>
<tr ng-repeat="data in c.data.arr">
<td>{{data.u_asset}}</td>
<td>{{data.u_requested_for}}</td>
<td>
<select ng-model="data.selectedState">
<option value="1">In Stock</option>
<option value="2">Out Stock</option>
</select>
</td>
<td>
<button type="button" ng-click="updatetable('escalate', data)">Update</button>
</td>
</tr>
</table>
</div>
CSS
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
Server Script
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
var gar = new GlideRecord('u_asset_request');
var curruser = gs.getUserID();
//gar.addQuery('caller_id', curruser);
gar.addQuery('u_requested_for', curruser);
gar.query();
data.arr = [];
while (gar.next()) {
var obj = {}; // Create a new object for each record
obj.u_asset = gar.getDisplayValue('u_asset');
obj.u_requested_for = gar.getDisplayValue('u_requested_for');
obj.u_state = gar.getDisplayValue('u_state');
obj.sys_id = gar.getUniqueValue(); // Get sys_id correctly
data.arr.push(obj); // Add the object to the array
}
// if (input && input.action === "updateState") {
if (input) {
var gr = new GlideRecord('u_asset_request');
if (gr.get(input.id)) {
gr.u_state = input.optionvalue; // Assuming 'state' is a valid field in the table
gr.u_validated = true;
gr.update();
}
}
})();
Client Controller
api.controller = function($scope) {
/* widget controller */
var c = this;
// Function to update a single record
$scope.updatetable = function(action, record) {
c.data.action = action;
c.data.id = record.sys_id; // Use the specific record's sys_id
c.data.optionvalue = record.selectedState; // Get selected state
//alert(c.data.optionvalue);
c.server.update().then(function(response) {
console.log("State updated for record:", record.sys_id);
});
};
// Function to update all records
/* $scope.updateAll = function() {
c.data.arr.forEach(function(record) {
if (record.selectedState) {
$scope.updatetable('updateState', record);
}
});
console.log("All states updated.");
};*/
};
- Labels:
-
Architect
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2024 10:29 PM
Hi @Ken61 ,
You are querying the 'u_asset_request' table with the filter condition: 'u_requested_for' is current logged in user. Below is your script:
var gar = new GlideRecord('u_asset_request');
var curruser = gs.getUserID();
//gar.addQuery('caller_id', curruser);
gar.addQuery('u_requested_for', curruser);
gar.query();
Hence, if you want to remove the record from the table when it's state will be updated, you also need to remove or change the 'u_requested_for' field so that it will not come up in the next query.
Also, rearrange the server-side script as below:
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
// if (input && input.action === "updateState") {
if (input) {
var gr = new GlideRecord('u_asset_request');
if (gr.get(input.id)) {
gr.u_state = input.optionvalue; // Assuming 'state' is a valid field in the table
gr.u_validated = true;
gr.update();
}
}
var gar = new GlideRecord('u_asset_request');
var curruser = gs.getUserID();
//gar.addQuery('caller_id', curruser);
gar.addQuery('u_requested_for', curruser);
gar.query();
data.arr = [];
while (gar.next()) {
var obj = {}; // Create a new object for each record
obj.u_asset = gar.getDisplayValue('u_asset');
obj.u_requested_for = gar.getDisplayValue('u_requested_for');
obj.u_state = gar.getDisplayValue('u_state');
obj.sys_id = gar.getUniqueValue(); // Get sys_id correctly
data.arr.push(obj); // Add the object to the array
}
})();