How to tell if Save was triggered by User or Javascript function

1k views Asked by At

I have an issue in which users aren't updating case status when saving cases. The status field is required and usually always populated. I have a requirement from the client to prompt the user to make sure the case status is updated and correct. Without building a bunch of code for this, I leveraged a form, made everything required not visible (it will be there anyways initially) and only made the case status fields visible.

I did this so that it's always navigated to OnSave as seen as follows:

The following is the JS:

function OnSave(context)
{
    debugger;
    
    var formContext = context.getFormContext();
    var saveCounter = 0;
    
    LoadCaseStatusModal(formContext, saveCounter);
}

function OnLoad(context)
{
    var formContext = context.getFormContext;
}

function LoadCaseStatusModal(formContext, saveCounter)
{
    debugger;
    
    formContext.data.entity.save();
    
    var formContext = formContext;
    
    if (formContext.ui.getFormType()==2)
    {
        var lblForm = "Case Status Modal";

        if (formContext.ui.formSelector.getCurrentItem().getLabel() != lblForm) 
        {
            var items = formContext.ui.formSelector.items.get();            
            
            for (var i in items) 
            {
                var item = items[i];
                var itemId = item.getId();
                var itemLabel = item.getLabel()

                if (itemLabel == lblForm) 
                {
                    item.navigate();        
                } 
            }
        
        } 
    }

}

The problem here is when I navigate here:

function LoadCaseStatusModal(formContext, saveCounter)
    {
        debugger;
        
        formContext.data.entity.save();

The formContext.data.entity.save();kicks the OnSave() off again and the user gets a prompt asking them to save again when they already saved. Totally kills the whole flow.

So I thought I'd create an OnSave helper variable like so: var saveCounter = 0;

I immediately knew this would cause probs.

Then I found this: https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/save-event-arguments/getsavemode

The problem here is that it doesn't same to tell me if the user executed this or was this kicked off by JS? -- The OnSave function.

Am I thinking to hard? am I missing something? any extra eye would help.

Regards,

1

There are 1 answers

0
Louys Patrice Bessette On BEST ANSWER

I suppose .save() to be async... That is why I suggest a setTimeout() below. Anyway, using a global flag to make an exception on the programmatic save is a way to investigate.

let savingProgramatically = false

function OnSave(context)
{
    debugger;
    
    // return if the flag is on
    if(savingProgramatically) return

    var formContext = context.getFormContext();
    var saveCounter = 0;
    
    LoadCaseStatusModal(formContext, saveCounter);
}

function OnLoad(context)
{
    var formContext = context.getFormContext;
}

function LoadCaseStatusModal(formContext, saveCounter)
{
    debugger;
    
    // Set the flag true
    savingProgramatically = true

    // save
    formContext.data.entity.save();

    // set the flag back to false asap -- Adjust the delay
    setTimeout(()=>{savingProgramatically = false}, 100)  // maybe 0 delay works... Or no setTimeout at all.

    ...