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

Hey Jesse do you have a full example of updating with a button.



The html, server, and client side sample code


Hi Dylan,
  You are very close to making these connections that you are attempting. One thing about Angular is that you can pass the whole data object in your function. What I mean by that is if you are using an ng-click function you can reference it like this:



<div ng-repeat="user in c.data.user_array">
      <div ng-click="deleteUser( user )" >{{user.first_name}}
      </div>
</div>


on your client controller you can reference that


function deleteUser( user ) {


      user.action = delete


      c.server.update()
}



I hope this helps and if you have any more questions let me know and I will be glad to help.



Philip Engles


find_real_file.png



Hello Phillip what if I want to update information in a record could I use the same code


Text in a pre element is displayed in a fixed-width font, and it preserves both spaces and line breaks


Snow consultan1
Tera Contributor

Hi All,

 

I have one issue with service portal widget. I have created one custom widget to cancel RITM and add cancel comments. functionality is working fine but comments are adding multiple times. can anyone suggest me on this.

 

Thanks