Hi all, I want to `warn` a user of something when ...
# suitescript
d
Hi all, I want to
warn
a user of something when they change a value on a form. So, I want to display a
dialog.confirm
from the
validateField
function and ask them to confirm the change .. before allowing the change on the field (which will trigger the fieldChanged function. We all know that
dialog.confirm
runs asynchronously ... has anyone figured out a why to make it behave
synchronously
?
m
Have you tried using the native js window.confirm? That is what I am using on a use case I need to confirm a message
d
I was hoping to retain the NetSuite look'n'feel by using their dialog
m
you will need to use async and await to wait for the promise to return or use the .then syntax
Copy code
async checkDuplicates(options, modules) {
        await modules.https.post.promise({
            url: options.restletUrl,
            body: {
                command: "checkDuplicates",
                values: JSON.stringify({
                    selected: this.selected.records,
                    selectAccountingPeriod: this.selectAccountingPeriod(),
                }),
            },
            headers: [{"Content-Type": "application/json"}],
        }).then((response) => {
            this.duplicates = JSON.parse(response.body);
        }).catch((e) => {
            console.error("e", e);
            alert("An unexpected error has occurred\n" +
                ` name : ${e.name || ""}\n message : ${e.message || ""}\n stack : ${e.stack}`
            );
            this.duplicates = [{id: -1}];
        });
        return this.duplicates;
    }
}
this is an example i used https prmomise async and then i called in an async function and awaited the response
Copy code
async saveRecord(options, modules) {
    const checkDuplicatesResults = await this.checkDuplicates(options, modules);
}
It should also work for the dialog
d
I can't get it to work, but thatnks anway
g
i wouldn’t try to change it’s nature. but maybe we can take advantage of it. • create an object
const pendingChange={}
in client script closure • use
fieldChanged
to detect the fieldId of interest. X • for any change for X that doesn’t have a
pendingChange.X
entry… ◦ create a
pendingChange.X
object that has the values of interest. ▪︎
pendingChange.X ={ fieldId : 'X', dialog : null, oldvalue: 'foo', newvalue : 'bar' }
etc ◦ create a custom dialog in that object
X.dialog = dialog.create…
◦ set the buttons where the label is confirm but the value is the object.
buttons : [ { label:'confirm', value : pendingChange.X}, {label : 'cancel' ,value: null}]
where confirmation returns the object you created before ◦ undo the user’s change in X. ◦ if they confirm
then
will have an object you can use to update the value ◦ if they cancel then remove the entry from
pendingChange
• for any change for X that does have a
pendingChange[X]
entry… ◦ it must have been made by a confirmation and so just allow it if somehow a
save/submit
is initiated in the JS repl loop before the confirm runs it’s fine because afer all they didn’t confirm it. Or (i suggest don’t do this ) you could tick the save/submit at least one more time by putting the save/submit into a promise.
d
Hmm .. I think I follow. I'd be nice to see a working model though .. if I get the time, I'll give it a go. Thanks @Gerald Gillespie
g
A refinement…. since
pendingChange
is in scope for
fieldChanged
you could look for `pendingChange[fieldId]`and do
pendingChange[fieldId].dialog.then( choice=>{ /* process choice */ })
Because they are out of the box I use dialog (and message) quite a bit and in some creative ways
d
I smell a blog post with example @Gerald Gillespie... you gonna take a crack at it? Its a common ask here in the community to make better use of N/ui/dialog