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

Ned Chamberlain
Tera Contributor

@martinsk  - wondering if you can give more context around the asset review/audit you implemented.  I am looking for something similar