widget - action is not being pass to server side

OpalH
Tera Contributor

hi everyone,

 

I am trying to create todo widget. widget with 2 buttons. approve and reject.

when I click the buttons, the variable "action" is not being pass to server side. so nothing happens. 

 

Here is my code:

html:

<div class="widget-approval-widget">

<!-- Display Record Number and Description -->
<div>
<strong>Record Number:</strong> {{::data.record.number}} <br>
<strong>Description:</strong> {{::data.record.description}}
</div>

<!-- Command field (optional, if needed) -->
<div>
<textarea class="form-control" type="text" id="command" ng-model="data.command" placeholder="תכניס תגובה"></textarea>
</div>

<!-- Approve/Reject Buttons -->
<div>
<button ng-click="updateRecord('approve')" class="btn btn-primary button-width">Approve</button>
<button ng-click="updateRecord('reject')" class="btn btn-default button-width">Reject</button>
</div>
</div>

 

 

server script:

(function() {
var recordId = input && input.sysId ? input.sysId : options.sysId;
var action = input && input.action ? input.action : options.action;
var command = input && input.command;

data.record = {};
data.success = false;

if (recordId) {
// Fetch record details
var gr = new GlideRecord('x_1359466_malam_0_malam_request');
if (gr.get(recordId)) {
data.record = {
sys_id: gr.getValue('sys_id'),
number: gr.getValue('number'),
description: gr.getValue('short_description'),
};

// Perform action if input.action exists
if (action) {
gs.addInfoMessage('action ' + action);
if (action === 'approve' || action === 'reject') {
gr.setValue('state', action === 'approve' ? 2 : 4);
gr.update();
data.success = true;
} else {
data.error = 'Invalid action';
}
}
} else {
data.error = 'Record not found';
}
} else {
data.error = 'No record ID provided';
}
})();
 
 
client :
function($scope, $http) {
var c = this;

$scope.record = c.data.record;
$scope.command = '';
$scope.success = false;

// Handle approve/reject actions
$scope.updateRecord = function(act) {

console.log('Updating Record - Action:', act, 'Record ID:', c.data.record.sys_id); //when running log show action and sysid

$scope.data.action = act;

$scope.server.update(init).then(function(response) {
if (response.success) {
alert('record was update');
return true;
}
return false;
});
};

function init() {}
}

 

I could use some help, what's wrong with my code?

Thank you

 

1 REPLY 1

ChrisBurks
Mega Sage

Hi @OpalH ,

 

The "action" is making it over to the server. The reason "if (action)" is not working after the client side sends the update is because it's nested in the "if(recordId)" condition. This works on inital load because server side by default runs first on load and the first line of the server script is setting recordId with the options.sysid.

Yes, I can see that there is an assignment for input.sysid however, that isn't going to work when the client side sends the update because there is no data.sysid being set on the client side.

You have to remember that when ".server.update()" is used it sends over the data object as the input object.

So currently your client side script has c.data.record and $scope.data.action which means your input object looks like this:

{ 
    "action": "approve", //or reject depending on what was clicked
    "record": {  "description": "the description",  "number": "the number", "sys_id": "the sys id"  }
}

 

If the sys_id that you're looking for is the same that is for the "record" then changing the first line of the server script to something like the following should work:

//Change first line of server script
var recordId = input && input.record.sys_id ? input.record.sys_id : options.sysId;