Streamlined/best practices way to save in widget?

Bob Loblaw
Mega Guru

Hello,

 

Are there best practices for how to save only edited fields on a widget? My current widget works fine, but I feel like my way of saving edited fields is outdated and not streamlined. Here's basic pseudo-code of what I'm doing (am working in Paris):

 

- I have 12 text fields on the HTML widget of a custom form.
- When user clicks custom Save button, it calls the client controller which sets c.data.action = "saveIt" and then calls c.server.update.
- In server update, I have:

if (input.action == 'saveIt'){
- look up that record in table
- record.fieldname = input.fieldname
- record.update();
}

I know these are the basics and they work fine, but I feel like I could be using "$scope.$on(field.change)" and/or "server.get" (instead of server.update) to send only the fields that were updated to the server?

 

Any thoughts are welcomed, thanks. 🙂

1 ACCEPTED SOLUTION

Dan Ostler
Tera Guru

Hi Bob,

 

Many Service Portal developers, myself included, seem to have moved to using a $http to make asynchronous REST calls wherever possible. Most of the time you can use the Table API, but if you already have a custom Scripted REST API that's even better. While it doesn't always make sense I think this sounds like an appropriate scenario. If you're unfamiliar with what I'm talking about here is a quick example:

 

api.controller = function ($http) {
    c.updateRecord = function (sys_id) {
        $http({
            method: 'PATCH',
            url: '/api/now/table/<table_name>/<sys_di>',
            params: {}
        }).then(function (r) {
            if (r.hasOwnProperty('result')) {
                // update c.data here
            }
        });
    }
};

 

Just be sure to inject $http into your client controller if you haven't already.

 

I hope this helps!

- Dan

View solution in original post

1 REPLY 1

Dan Ostler
Tera Guru

Hi Bob,

 

Many Service Portal developers, myself included, seem to have moved to using a $http to make asynchronous REST calls wherever possible. Most of the time you can use the Table API, but if you already have a custom Scripted REST API that's even better. While it doesn't always make sense I think this sounds like an appropriate scenario. If you're unfamiliar with what I'm talking about here is a quick example:

 

api.controller = function ($http) {
    c.updateRecord = function (sys_id) {
        $http({
            method: 'PATCH',
            url: '/api/now/table/<table_name>/<sys_di>',
            params: {}
        }).then(function (r) {
            if (r.hasOwnProperty('result')) {
                // update c.data here
            }
        });
    }
};

 

Just be sure to inject $http into your client controller if you haven't already.

 

I hope this helps!

- Dan