Check the write to Data Store after submit a interface

Hi All,

I have an interface where I use the function a!writeToDataStoreEntity when I click on the submit button.

In the function a!writeToDataStoreEntity, I declare a variable where save the value for the  onSuccess or onError . If this variable is onError, I don't want to submit the interface and advice the user about the error. Is there a way to make this?

This is an example of my case

a!formLayout(
firstColumnContents: {
allMyfields
},
buttons: {
a!buttonLayout(
primaryButtons: {
a!buttonWidgetSubmit(
label: "Start",
style: "PRIMARY",
value: "start",
saveInto: {
a!writeToDataStoreEntity(
dataStoreEntity: cons!myDataStore,
valueToStore: local!myCDT,
onSuccess: {
a!save(
local!resultWriteToDB,
"Data Inserted"
),
a!save(
ri!newBBSID,
fv!storedValues.ID
)
},
onError: {
a!save(
local!resultWriteToDB,
"Data not Inserted"
)

}
)

},
validate:true,
validationGroup: "checkSave"
)
}
),

},
validationGroup: "checkSave",
validations: {
a!validationMessage(
message:if(local!resultWriteToDB="Data Inserted","saved",""),
validateAfter:"SUBMIT"
)
}
)

 

 

Thanks in advance.

  Discussion posts and replies are publicly visible

  • Something that I've tried that seems to work is having a local variable such as local!error that defaults to false. In onSuccess use a!save(local!error,false) and in onError use a!save(local!error,true). Then in your a!buttonWidget(), have submit:not(local!error). This way if the evaluation results in an error, then the form won't submit.

  • 0
    A Score Level 1
    in reply to John M
    Hi johnm796,
    I modify my interface, as you suggested:

    = load(
    local!error:false,
    a!formLayout(
    label:"title",
    firstColumnContents: {
    myFields
    },
    buttons: {
    a!buttonLayout(
    primaryButtons: {
    a!buttonWidget(
    label: "Start",
    style: "PRIMARY",
    value: "start",
    saveInto:{
    a!writeToDataStoreEntity(
    dataStoreEntity: cons!myDataStore,
    valueToStore: local!mycdt,
    onSuccess: {
    a!save(
    local!error,false
    ),

    },
    onError: {
    a!save(
    local!error,true
    )
    }
    )

    },
    submit:not(local!error),
    validate:true,
    validationGroup: "checkSave"
    )
    }
    ),

    },
    validations: {
    a!validationMessage(
    message:if(toboolean(local!error),"not saved",""),
    validateAfter:"REFRESH"
    )
    },
    validationGroup: "checkSave"
    )

    )


    However,I test the error case in designer:it works and give me the message if local!error is true.
    If I test it on my process, the form has been submitted. Are there errors on my code?
    Thanks.
  • Can you use with function instead of load ?
    I think this might solve your issue.
  • 0
    Certified Lead Developer
    in reply to jurim409

     - I think you'll need to do the a!WTDS call earlier in your form if you want the submit button to not submit in the case of an error.

  • 0
    Certified Lead Developer
    in reply to sanjays0006

     - FYI, using the with() function would cause a failure in this case, as they are trying to write a value directly into the local!error variable. You'll find that if you try to manually set a value into a with() variable, in any situation, the form will give you an error.

  • on a user input task, there is no turn around after onError, as the button is about to submit the form. Work a round is to have two buttons, the button attached to a!WTDS does not submit but sets a local!errorOccured to true and use this value on actual submit button. If you do not like two buttons, then use some other user input field to trigger the a!WTDS.
  • 0
    A Score Level 1
    in reply to venkats533


    Hi,
    Thanks for your suggestions, but, in my situation, I can't add further buttons or other user input fields that manage the WTDS.
    I think I will make a control out of the interface and, in case of error, I will advise the user with another interface and I will send an alert to myself.
    Thanks again.
  • As others have mentioned, once the saveInto occurs for a submitting button, you don't really have a way of checking the saved values before submitting the form. I've run into this problem in the past, and my solution was to have a gateway immediately after the input task in the process model that checked for the error, and if there was one, it immediately chained back into the same task and notified the user of the error, so it has the exact same appearance as if it had validated before submitting. This won't work for every use case since it does recreate the task which could be problematic in some applications that maybe depend on the age of a task, but it could be a viable solution.
  • 0
    Certified Lead Developer
    the only way which i feel is (in case if you want to use writeToDataStoreEntity Function in Interface),

    Generally you will be having a process model where you might have configured this form so, in process you can check the response inside Gateway, if it's an error, then revert it back to the same Interface again, and assign the pv! into ac! so that your input data will be maintained, in order to avoid re-entering the same again
  • 0
    Certified Lead Developer
    Is there a specific reason you're using a!writeToDatastore in this instance? Are you unable to do this in process? You should do writes in process where possible.