How to update the record in a custom table(u_terms_and_conditions) from widget editor

Deepak Panigrah
Tera Contributor

I have created a widget terms and conditions on accepting it will update the record of the user in u_terms_and_conditions and field u_accepted_or_rejected to true initially it will be false server side script and angular template is working fine but was unable to update the record from client side.

Client Script

api.controller = function($uibModal, $scope, $http, $window) {
   var c = this;
   // Open the modal if the popup should be shown
   if (c.data.showPopup) {
       $uibModal.open({
           templateUrl: "terms-popup", // AngularJS template for the modal
           scope: $scope,
           backdrop: "static", // Prevent closing by clicking outside
           controller: function($scope, $uibModalInstance) {
               // Agree button: Update the record
               $scope.agree = function() {
                   $http.put('/api/now/table/u_terms_and_conditions/' + c.data.recordSysId, {
                       u_accepted_or_rejected: true // Set `u_accepted_or_rejected` to true
                   }).then(function(response) {
                       $uibModalInstance.close(); // Close the modal
                   }, function(error) {
                       console.error('Error updating the record:', error);
                   });
               };
               // Disagree button: Log out the user
               $scope.disagree = function() {
                   $uibModalInstance.dismiss();
                   $window.location = "/logout.do"; // Redirect to the logout URL
               };
           }
       });
   }
};

Server Script

(function() {
   data.showPopup = false; // Default: Don't show the popup
   var userId = gs.getUserID(); // Get current user's sys_id
   var gr = new GlideRecord('u_terms_and_conditions');
   gr.addQuery('u_name', userId); // Match the logged-in user by the `u_name` reference field
   gr.query();
   if (gr.next()) {
       if (!gr.u_accepted_or_rejected) { // Check if terms are not accepted
           data.showPopup = true; // Show the popup
           data.recordSysId = gr.sys_id; // Pass the record sys_id to client
       }
   }
})();

Angular Template

<div class="modal-header">
<h3 class="modal-title">Terms and Conditions</h3>
</div>
<div class="modal-body">
<p>Please read and accept the Terms and Conditions to proceed.</p>
<p>Your terms and conditions content goes here...</p>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="agree()">Agree</button>
<button class="btn btn-danger" ng-click="disagree()">Disagree</button>
</div>

 

 

1 REPLY 1

Siddhesh Jadhav
Kilo Sage

Hi @Deepak Panigrah ,

 

Possible Solutions:

  1. Fix the $http.put Request

    • Ensure the URL for updating the record is correctly formatted as /api/now/table/<table_name>/<sys_id>.
    • Add proper headers (Content-Type: application/json) to the $http request.
    • Pass the data object in the correct format, e.g., { u_accepted_or_rejected: true }.
  2. Check Permissions

    • Ensure the logged-in user has write access to the u_terms_and_conditions table.
    • Verify that the appropriate roles and ACLs are configured for the /api/now/table endpoint.
  3. Validate sys_id

    • Confirm that the sys_id of the record being passed from the server-side script (c.data.recordSysId) matches an existing record in the u_terms_and_conditions table.
    • Debug the sys_id being passed using console.log() in the client script.
  4. Use Debugging Tools

    • Check the Network tab in the browser developer tools to verify the API request and response.
    • Look for HTTP error codes (e.g., 403 for permission issues or 404 if the record is not found).
  5. Server-Side Validation

    • Add logs in the server script to confirm the correct record is being queried and the sys_id is passed to the client.
  6. Test the API Separately

    • Use a tool like Postman or ServiceNow's REST API Explorer to test the /api/now/table/u_terms_and_conditions/<sys_id> endpoint with the same payload.
    • Ensure the record is updated successfully outside the widget to isolate the problem.
  7. AngularJS Scope Issues

    • Confirm that $scope.agree is correctly bound in the modal. If there are any issues with the binding, the function may not execute properly.

If this answer helps you solve your query, please mark it as accepted and helpful.

 

Best Regards,
Siddhesh Jadhav