Updating a record in a service portal widget

martinsk
Mega Expert

Hi

I'm in need of some help with a service portal widget. Here's what I've got so far: the widget displays a list of assets issued to the current user (using a glide record in the server script and ng-repeat in the HTML template). For each asset listed, I've also got a button which doesn't do much at the moment other than display a message (just to prove the click works).

What I want is for the user to be able to click the button along side a particular asset, to confirm they still have it, and for this to update the status / last confirmed date in the respective asset record.

Please can someone confirm the syntax:

  1. in the HTML template, to pass the sys_id back to the client script (via ng-click?)
  2. in the client script, to grab the sys_id and pass it on to the server script (via a function and c.server.update?)
  3. in the server script, to get the sys_id and update a glide record for the correct asset (via input?)

Thanks in advance.

Martin 

1 ACCEPTED SOLUTION

matthewmaxwell
Kilo Guru

If you're using an ng-repeat with the buttons inside (I assume you are), you have access to the current model that you can pass into the ng-click, like this:

<div ng-repeat="asset in controller.assets">
  <button ng-click="confirmAsset(asset);">Confirm!</button>
</div>

In your controller's ng-click handler (I named it confirmAsset here), you would do something like this:

// This is the confirmAsset method on your controller
this.confirmAsset = function(asset) {
    // Asset is being bassed from the ng-click
    // The controller has a server property with a method called get on it.
    // The object you pass to the get() call is passed as "input" on the server-side code.
    this.server.get({
        // NOTE: This method property is totally unneeded technically.
        //  I am adding it here so that my server-side code can know exactly what it's supposed to do.
        method: "confirmAsset",
        asset_sys_id: asset.sys_id
    });
}

The server side script would do something like this:

if (input) {
    // Adding a method here just in case anything else sends information to the
    // client side script, we can distinguish and only handle what we need to.
    if (input.method === "confirmAsset") {
        // NOTE: tableName here should be the name of the table
        var record = new GlideRecord(tableName);
        // NOTE: asset_sys_id should be the name of the field you want to update for
        //  confirmation
        record.addQuery("asset_sys_id", input.asset_sys_id);
        record.query();
        if (record.next()) {
            // NOTE: Set whatever field you're using to confirm the asset
            record.confirmed = true;
            // Update the record.
            record.update();
        }
    }
}

Hope this helps.

View solution in original post

25 REPLIES 25

Hey Michael,

 

I apologize for the delay; it's been a busy start of the week. Let me test this and get back to you shortly.

 

m

martinsk
Mega Expert

OK, got it.

The update code is now working.

Many thanks for your help with this.

Martin

 

jxsaxton421
Tera Guru

Hi guys, I have a similar issue and I was wondering why in the client script you could not just call c.server.update() and that would send over the pet object from the button pressed to the server code.

Hi Joshua,

`c.server.update` will work for sending information to the server-side script; however, it requires you have data stored on the `data` property of the controller. In my opinion, because we are not needing to send over persistent data, it feels clunky to me to set properties on the controller for a one-off send. That's why I prefer using a `get()` call in this scenario.

I have an issue with updating the record.

If I press the button, everything is working fine but the comments are updating multiple times. Can you please suggest at below community thread.

 

https://community.servicenow.com/community?id=community_question&sys_id=3cccc8e4db5c6b84fff8a345ca96191c

 

Thanks