Service Portal Widget, Delete or Update based on the button pressed

Dylan Mann1
Giga Guru

I'm trying to create a widget which displays a users information. I'm calling both the User Table and my custom 'members table' which references many of the fields within the user table. When a user clicks the delete button I want the record from the members table to be deleted and when the update button is pressed I want the User table to be updated. This isn't my issue though. My issue is that when I use the server.update() method it updates and then deletes the record no matter which button is pressed. I understand why it's not working but not how to fix it. How can I separate the calls to the server?

Here's my client and server side code:

$scope.deleteUser = function() {

$scope.server.update();

}

$scope.updateUser = function() {

$scope.server.update();

}

================================

if(this.input) {

memberRecord.deleteRecord();

}

if(this.input) {

userRecord.first_name = this.input.user.firstName.toString();

userRecord.last_name = this.input.user.lastName.toString();

userRecord.email = this.input.user.email.toString();

userRecord.roles = this.input.user.roles.toString();

userRecord.update();

data.user = this.input.user;

gs.addInfoMessage('Update Successful');

}

Thanks,

Dylan

1 ACCEPTED SOLUTION

jesseadams
ServiceNow Employee
ServiceNow Employee

One way to do this would be to add an action variable into your input object.


Set $scope.data.action to update or delete when the respective button is pressed, then on the server say if(input.action =="delete")... and so on.


View solution in original post

9 REPLIES 9

ajaylimaye
Tera Contributor

c.server.update().then(function(){


//Give your inputs here and it should solve your issue.


})



PS: Hit like, Helpful or Correct depending on the impact of the response



Thanks,


Ajay Limaye


Solugenix Corporation


jesseadams
ServiceNow Employee
ServiceNow Employee

One way to do this would be to add an action variable into your input object.


Set $scope.data.action to update or delete when the respective button is pressed, then on the server say if(input.action =="delete")... and so on.


What if the input object is all on the server side? I have a data.user on the server side which the html references in the ng-models. Am I adding an action field to that user object and trying to change it before the update?



Like so:



server side:



data.user = {


firstName: userRecord.first_name.toString(),


lastName: userRecord.last_name.toString(),


email: userRecord.email.toString(),


roles: userRecord.roles.toString(),


action: ''"


};



client side:



$scope.deleteUser = function() {


$scope.data.action = 'delete';


$scope.server.update();


}



$scope.updateUser = function() {


$scope.data.action = 'update';      


$scope.server.update();


}


You don't have to add it to the user object necessarily. You can if that's what you want to do but it isn't necessary. If you do, just change $scope.data.action to $scope.data.user.action in the controller.



Any changes you make to $scope.data should be copied to the input object when you call back to the server via server.update() or similar.


So, you should be able to set $scope.data.action like you are in the ng-click function for your button and then just have an if condition on the server checking what the value of input.action is.



I did this on a widget once and it went something like this:


// Client Controller


c.update = function(){


...


c.data.action = 'update';


c.server.update();


};


c.delete = function(){


...


c.data.action = 'delete';


c.server.update();


};



// Server script


if(input && input.action =='update' ){


... do your update stuff here ...


}



if(input && input.action =='delete' ){


... do your delete stuff here ...


}