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

Hi @matthewmaxwell - that works! However, when I try adding your code to my existing widget it doesn't - is there something I need to amend in your code? 

Hey Michael,

 

If it will not violate any confidential policies with what you're doing, can you show me your widget's HTML Template, Client Script, Server Script, and Link, or even better if you can export the record's XML, I can drop it in my PDI and see what's happening.

 

 

Hey Matthew, 

 

No of course - it's just a standard quick link widget. I've attached the XML here. 

 

Thanks, 

In the Server Script, did you make sure that I didn't accidentally get your table name wrong (I named mine `Employee Service Center Clicks [sn_ex_sp_employee_service_center_clicks]` and it's in the Employee Center scope, and did you add a column to that table called `Quick Link`, which will have a name of `u_quick_link` and be a reference to the Quick Link [sn_ex_sp_quick_link] table?

Hey Michael,

 

Upon further investigation, I just noticed that your widget and mine are a little different. Mine have `id="{{data.instanceId}}-item-{{$index}}"` as an attribute on the anchor tag, and I don't see that in your XML there.

This is a screenshot from mine:

matthewmaxwell_0-1709731287526.png

 

I should have asked this initially - What version of ServiceNow is your instance? I wonder if they changed that between versions.

 

I can set my PDI to whatever version you're on and figure out the adjustment.