Make a "Copy" button

Forums Dynamic Forms for SharePoint Make a "Copy" button

Viewing 14 reply threads
  • Author
    Posts
    • #23991
      Steve Jonasson
      Participant

      Hi guys,
      I need some help and have a few questions about a “copy” button.

      I have a list of items – when I click on “create a new item” the form is initiated. At the bottom side, there are two types of buttons – cancel and save. I would like to add another button titled “Copy” which would work like – “save this item to the current list and copy “selected (10 fields max)” fields and initiate another new form where the copied fields would be automatically imported”.

      Does DFFS support this? Is it possible to make it? The attachement is just an example. Select, for example, two first fields — click on “Copy” — automatically save the current item and initiate a new form with filled fields.

      Thank you very much for any response.

    • #24002
      Alexander Bautz
      Keymaster

      Hi,
      Add this to your Custom JS in NewForm:

      // If creating a copy
      if(GetUrlKeyValue("CreateCopy") === "1"){
          var str = sessionStorage.getItem("SaveAndCreateNewData"), data;
          if(str !== null){
              data = JSON.parse(str);
              jQuery.each(data,function(fin,val){
                  setFieldValue(fin,val); 
              });
          }
      }
      
      // Add button
      jQuery("input[id$='_diidIOSaveItem']").before("<input type='button' class='ms-ButtonHeightWidth' style='margin-right:4px;' value='Save and Copy' onclick='saveAndCopy()' />");
      
      var createCopyWhenSaving = false;
      function saveAndCopy(){
          createCopyWhenSaving = true;
          spjs.dffs.triggerSave();
      }
      
      function dffs_PreSaveAction(){
          if(createCopyWhenSaving){
              var arr = [
                  "EmailAddress",
                  "FirstName",
                  "Floor",
                  "ID",
                  "LastName",
                  "LeaseStartDate",
                  "PhoneNumber",
                  "ShortTermLease",
                  "Title"
              ];
              var data = {};
              jQuery.each(arr,function(i,fin){
                  data[fin] = getFieldValue(fin);
              });
              sessionStorage.setItem("SaveAndCreateNewData",JSON.stringify(data));
              // Redirect
              spjs.dffs.redirect(location.pathname + "?CreateCopy=1",false);
          }
          return true;
      }

      It will store the input in sessionStorage and pull it into the NewForm based on an URL parameter that is set when hitting the copy-button.

      You must ensure the array of fields have the correct FieldInternalNames.

      Let me know how this works out.

      Alexander

    • #24015
      Steve Jonasson
      Participant

      Thank you very much for the code. I’m not great in javascript and coding such as. Please, could you tell me where I should put my “IDs (URLs)” of columns etc? At this moment, it does not work. But I probably have some mistakes in the code. Could you check it? Thank you once again.

      
      
      // If creating a copy
      if(GetUrlKeyValue("CreateCopy") === "1"){
          var str = sessionStorage.getItem("SaveAndCreateNewData"), data;
          if(str !== null){
              data = JSON.parse(str);
              jQuery.each(data,function(fin,val){
                  setFieldValue(fin,val);
              });
          }
      }
      
      // Add button
      jQuery("input[id$='_diidIOSaveItem']").before("<input type='button'
      class='ms-ButtonHeightWidth' style='margin-right:4px;' value='Save and Copy' onclick='saveAndCopy()' />");
      
      var createCopyWhenSaving = false;
      function saveAndCopy(){
          createCopyWhenSaving = true;
          spjs.dffs.triggerSave();
      }
      
      function dffs_PreSaveAction(){
          if(createCopyWhenSaving){
              var arr = [
                  "Cena"
                  "CenoveHladiny"
                  "Cena"
                  "StavSchvalovani"
                  "CenaZaPrepravku"
              ];
              var data = {};
              jQuery.each(arr,function(i,fin){
                  data[fin] = getFieldValue(fin);
              });
              sessionStorage.setItem("SaveAndCreateNewData",JSON.stringify(data));
              // Redirect
              spjs.dffs.redirect(location.pathname + "?CreateCopy=1",false);
          }
          return true;
      }
    • #24033
      Alexander Bautz
      Keymaster

      I’m not sure I understand – you have added the correct field internal names to the “arr” like this it should work

      var arr = [
                  "Cena"
                  "CenoveHladiny"
                  "Cena"
                  "StavSchvalovani"
                  "CenaZaPrepravku"
              ];

      Do you see the new button at the bottom of the form?

      Please note that if you already have a function named dffs_PreSaveAction in your form, you must merge the content of these two so you don’t have duplicate function names.

      Alexander

    • #24052
      Steve Jonasson
      Participant

      Hi Alex,

      I’ve done everything. I don’t see a button at the bottom of the form. An error pops up – as you can see in the picture.

      In accordance with your last paragraph, dffs_PreSaveAction is not included in my form.
      One sidelight, I have a button “Load data” that is made in javascript above the form in webpart in content editor. It’s not made in DFFS. Could that be a problem?

      Thank you for your time Alex you spend with this article.

      Steve

      Attachments:
    • #24056
      Alexander Bautz
      Keymaster

      Not sure what the error message says – can you translate?

      You might need to correct the quotes used in the code snippet because they sometimes are replaced with the wrong type of quotes when copying and pasting. Please retype all single and double quotes and try again.

      Alexander

    • #24058
      Alexander Bautz
      Keymaster

      I had another look and you are missing commas in your array:

      var arr = [
                  "Cena",
                  "CenoveHladiny",
                  "Cena",
                  "StavSchvalovani",
                  "CenaZaPrepravku"
              ];

      Alexander

      • #24060
        Steve Jonasson
        Participant

        I’m sorry Alexander but still I cannot see a button at the bottom of the form and the same error pops up whenever I put commas, for example: “Cena”, “StavSchvalovani”,.. Even if I remove them.
        The error says: The operation cannot be completed due to error 80020101. The Code looks like:

        
        
        // If creating a copy
        if(GetUrlKeyValue("CreateCopy") === "1"){
            var str = sessionStorage.getItem("SaveAndCreateNewData"), data;
            if(str !== null){
                data = JSON.parse(str);
                jQuery.each(data,function(fin,val){
                    setFieldValue(fin,val);
                });
            }
        }
        
        // Add button
        jQuery("input[id$='_diidIOSaveItem']").before("<input type='button'
        class='ms-ButtonHeightWidth' style='margin-right:4px;' value='Save and Copy' onclick='saveAndCopy()' />");
        
        var createCopyWhenSaving = false;
        function saveAndCopy(){
            createCopyWhenSaving = true;
            spjs.dffs.triggerSave();
        }
        
        function dffs_PreSaveAction(){
            if(createCopyWhenSaving){
                var arr = [
                    "%5Fx0031%5FatributPolozky",
                    "NazevPolozky",
                    "DruhPolozky",
                    "SubdruhPolozky",
                    "BaleniAhmotnost",
                ];
                var data = {};
                jQuery.each(arr,function(i,fin){
                    data[fin] = getFieldValue(fin);
                });
                sessionStorage.setItem("SaveAndCreateNewData",JSON.stringify(data));
                // Redirect
                spjs.dffs.redirect(location.pathname + "?CreateCopy=1",false);
            }
            return true;
        }
      • #24062
        Alexander Bautz
        Keymaster

        You have a line break in this line that you must remove:

        jQuery("input[id$='_diidIOSaveItem']").before("<input type='button' class='ms-ButtonHeightWidth' style='margin-right:4px;' value='Save and Copy' onclick='saveAndCopy()' />");

        Also, your field name “%5Fx0031%5FatributPolozky” is not correct – find the correct fieldinternalnames in the Fields tab in DFFS backend.

        When you write code you can use this site to check the syntax: https://jshint.com

        Alexander

    • #24064
      Steve Jonasson
      Participant

      Alexander, it works! The break in that line was a mistake and that’s why it did not work out. Now, it’s working, but…

      If I create a new form, then I fill in columns, then I hit “Save and Copy” – the new for is initiated but it hasn’t saved.

    • #24077
      Alexander Bautz
      Keymaster

      I’m glad you go it running. I’m not sure I understand – is it the first form that doesn’t save?

      This code should save the initial item, and open a new “NewForm” after it is saved, but the new “copy” is not automatically saved because I assumed you wanted to have the opportunity to make changes to the item before saving it.

      If you want it to automatically save the second item, you can add spjs.dffs.triggerSave(); to the function like this:

      if(GetUrlKeyValue("CreateCopy") === "1"){
          var str = sessionStorage.getItem("SaveAndCreateNewData"), data;
          if(str !== null){
              data = JSON.parse(str);
              jQuery.each(data,function(fin,val){
                  setFieldValue(fin,val);
              });
              spjs.dffs.triggerSave();
          }
      }

      Alexander

    • #24112
      Steve Jonasson
      Participant

      Thanks Alex, the first form doesn’t save. I do not need the second form to be saved automatically. I fill the columns in the NewForm, then I hit “Save and Copy”, another NewForm is initiated but its predecessor (first form) did not save at all.

      And you were right, I wan to make changes in the “copied” form.

      Steve

    • #24114
      Alexander Bautz
      Keymaster

      OK, do you have any other custom js running in this form that could interfere?

      If you hit F12 and select “Console” before saving – do you see any error messages when you hit the “Copy and save” button?

      You might have to select “Preserve log” in the console window so that the log isn’t cleared when you navigate to the new “NewForm”.

      Alexander

      • #24124
        Steve Jonasson
        Participant

        Alex, It’s working so, there is no problem with those errors. I changed nothing, I was just looking at the wrong list on the same page. My mistake. When I clicked on the web “xxxxx” on SharePoint, the list has shown up where I use the javascript. But I had to click on the list at navigation panel.

        Now, all of it is working. Thank you very much Alex.

    • #24134
      Alexander Bautz
      Keymaster

      I’m glad you figured it out. I removed your two latest comments because you figured out what caused the problems and they no longer are relevant.

      Alexander

    • #24188
      Keith Hudson
      Participant

      As an alternative, would it be possible to use the vlookup functionality to create new items passing in fields from the current item, by selecting the current list as the target of the vlookup? It seems to me that I have used vlookup to SEE related items on the same list — would that also work for creating new items on the same list?

      • #24203
        Alexander Bautz
        Keymaster

        Yes, you should be able to do that with vLookup.

        Alexander

      • #24212
        Steve Jonasson
        Participant

        If I understand the vLookup function, it won’t help me at all. Initiation of a form offers the author specific columns like -Type of product, the number of the product, the validity of price since, expiration dates etc.… If I always wanted to have the same data taking from another list (vLookup) then it would be a perfect way how to do it. But…

        There are too many specific data filling in the columns in the form, that means I need only some columns to be copied, but They always change. It’s hard for me to describe the situation, but the Copy button is the best way how to solve my problem out.

    • #27417
      John Garrido
      Participant

      Is there a way to modify this copy feature to include multi lookup fields? It seems like it should work as it appears to be in the correct format setFieldValue(“MultiLookup”,[“Option number 1″,”Option number 2”]). Thanks for any guidance!

    • #27422
      Alexander Bautz
      Keymaster

      This looks like it should work already – do you get an error?

      Which version of spjs-utility.js are you using (hover over the Enhanced with DFFS link to show the version info)?

      Alexander

Viewing 14 reply threads
  • You must be logged in to reply to this topic.