function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sachin Bhalerao 17Sachin Bhalerao 17 

Difference between onSubmit and onSuccess

Dear Team ,

Greetings !!!

I created one lightning web component to update records using Lightning web component i used lightning-record-edit-form inside template . 

 <lightning-record-edit-form record-id={recordId} object-api-name="Contact"
    onsuccess={handleSuccess} onsubmit ={handleSubmit} >

M unable to unserstand exact difference between onsuccess and onsubmit .

One more thing in js file i used following code :

  handleSubmit(event) {
        // eslint-disable-next-line no-console
        console.log('onsubmit: '+ event.detail.fields);//Array of record field names or field IDs.
            
    }
    handleSuccess(event) {
        const updatedRecord = event.detail.id;
      // eslint-disable-next-line no-console
      console.log('onsuccess: ', updatedRecord);

M unable to unserstand the difference between event.detail.fields and event.detail.id .

Please provide me some easy explanation on these syntaxes.

Thanks & Regards
Sachin Bhalerao


 
Best Answer chosen by Sachin Bhalerao 17
Ajay K DubediAjay K Dubedi
Hi Sachin,

I have understood your problem and here I am trying to explain the OnSubmit and OnSuccess.

OnSubmit : Onsubmit events fire when a form is submitted. The origin of this event can sometimes be traced back to an on click (like clicking the "submit" button) but it can also come from a keyboard event (like pressing enter).
This implies that using onclick on a submit button on a form might miss some cases that a onsubmit on the form would catch.

OnSuccess : In onSuccess(), a function is called inside onSuccess(function).As the name suggests, onSuccess() will fire when a task is completed successfully you can be certain that isSuccessful() will return true, and getException() will return null (so there's not much point calling them).

In onComplete() isSuccessful() may be false, and you have the opportunity to deal with the failure, perhaps using getException() to obtain more detail.

Here is some referral link that will help you too.

https://stackoverflow.com/questions/46649162/how-do-i-know-whether-to-use-oncomplete-or-onsuccess
https://developers.marketo.com/rest-api/assets/forms/examples/


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com  (http://www.ajaydubedi.com )

All Answers

Ajay K DubediAjay K Dubedi
Hi Sachin,

I have understood your problem and here I am trying to explain the OnSubmit and OnSuccess.

OnSubmit : Onsubmit events fire when a form is submitted. The origin of this event can sometimes be traced back to an on click (like clicking the "submit" button) but it can also come from a keyboard event (like pressing enter).
This implies that using onclick on a submit button on a form might miss some cases that a onsubmit on the form would catch.

OnSuccess : In onSuccess(), a function is called inside onSuccess(function).As the name suggests, onSuccess() will fire when a task is completed successfully you can be certain that isSuccessful() will return true, and getException() will return null (so there's not much point calling them).

In onComplete() isSuccessful() may be false, and you have the opportunity to deal with the failure, perhaps using getException() to obtain more detail.

Here is some referral link that will help you too.

https://stackoverflow.com/questions/46649162/how-do-i-know-whether-to-use-oncomplete-or-onsuccess
https://developers.marketo.com/rest-api/assets/forms/examples/


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com  (http://www.ajaydubedi.com )
This was selected as the best answer
Sachin Bhalerao 17Sachin Bhalerao 17
Thank you Ajay , it was nice explanation .