0

After a user uploads a document the editform.aspx page is displayed. I need to force an update even if Cancel button is selected within the default editform.aspx form of a library. I know there is a preSaveAction function for save, is there a preCancelAction I can call to execute additional code on a cancel? Has anyone tried to force additional code to run even if the user selects cancel button within the default editform? Do i have to maybe capture that buttons event? What is that event? I have been searching and nothing found that matches what I want to do. I have successfully populated custom fields on the form so when the user selects save all is saved but when a user selects cancels i need some of the fields to still store the information. I tried and wrote code to do the save when the editform loads and it works great, but then if the user selects save i get error that the file is already updated by user. so i only want the code to run if i hit Cancel. Help!!!

<script type="text/javascript">

$(window).load(function(){
    try
    {
        // set custom fields on form so when user selects Save button these values are saved.  works great.

    // I can also save the items here as well using code below and when i select cancel they are saved.
    // but when I select save button I get an already saved error...  so i moved to only when hit cancel button below.

    }catch(ex)
    {
        alert(ex.toString());
    }

});



$(document).ready(function(){
    $(":button[value='Cancel']").click(function(){
    var context = SP.clientContext.get_current();
    var web = context.get_web();
    var listId = _spPageContextInfo.pageListId;
    var itemId = parseInt(GetUrlKeyValue('ID'));

    var list = web.get_lists().getById(listId);
    var listItem = list.getItemById(itemId);

    var urlValue = new SP.FieldUrlValue();
    urlValue.set_url(url);
    urlValue.det_description(descValue);

    listItem.set_item('Action',urlValue);
    listItem.set_item('ActionId',descValue);

    listItem.update();
    context.ExecuteQuery();

    });
});

</script>
  • You might be able to do what you want by capturing the Cancel button's click event, and then forcing a save. – Dylan Cristy Oct 2 '17 at 20:46
  • I added the code in my questions – Laura Oct 13 '17 at 17:51
0

Solution 1:

Use default values. So the values would store even if the user clicks the cancel button. This would work if the user closes the browser or terminate the web browser process. Please note this solution will not work for field types where setting default values is not available like "Person or Group".

Solution 2

Capture the cancel button's click event as @Dylan said and call the PreSaveAction

<script type="text/javascript">
$(document).ready(function(){
    $(":button[value='Cancel']").click(function(){
        saveFormFields();
    });
});
function saveFormFields()
{
    //set field values
}   
</script>
  • Thanks so much for this answer. It looks to be exactly what I want to do. I will implement this today and give you feedback. I think this will work. – Laura Oct 4 '17 at 12:29
  • Question, I am capturing the Cancel fine . I have the code to update the list item . I call listItem.update() and then call context.ExecuteQuery() but the field wasn't updated. WHen i put this same code in the window.load(function() section it worked fine. But don't want it there because then i get a conflick if the user saves. Which is the whole reason I am doing this. I should be able to perform an listItem.update() on a cancel right? – Laura Oct 5 '17 at 18:42
  • @Laura, I have renamed the method from PreSaveAction to saveFormFields in solution 2 of my answer. Now write your listItem.update() code and see if it saves form to the list on cancel button. Also please add your list item update code to the question. – Ahmad Zia Oct 6 '17 at 1:29
  • ahhh, ok that makes since. I will write the code here once i test. i am on a separate network so cant copy and paste which makes it hard. But I did plan on taking the time to place the code here for others once i got it working. Thanks for you help I will let you know if this works. – Laura Oct 6 '17 at 13:11
  • so here is a the code I am using when I select cancel. it works in the window.load block but not in my cancel catch... – Laura Oct 12 '17 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.