Redirect from NewForm to EditForm in DFFS

Home Forums Classic DFFS Redirect from NewForm to EditForm in DFFS

Tagged: 

Viewing 22 reply threads
  • Author
    Posts
    • #8894
      soldier1733
      Participant

        Per your article here you mentioned to add more lines for the data object for each field to push to save. I wanted to move the questions to your forum instead of the article.

        After adding the code to your existing code, I keep receiving an error on the page (see attached image). I am using all Field Internal names for the data objects and there are no errors in the console only the pop up on the webpage. Not sure if this matters, but all fields beside Title and Office Location are people pickers.

        Here is the modified code:

        
        
        // Save NewForm.apsx and redirect to EditForm.aspx
        function saveAndRedir(){
        	setReqID();
        	var ok = spjs.dffs.check_Mandatory(["Title"]), data = {}, newItem;
        	if(ok){
        		data.Title = getFieldValue("Title");
        		data.Requestor = getFieldValue("Requestor");
        		data.Office_x0020_Location = getFieldValue("Office_x0020_Location");
        		data.Manager = getFieldValue("Manager");
        		data.GM = getFieldValue("GM");
        		data.COO = getFieldValue("COO");
        		data.CEO = getFieldValue("CEO");
        		newItem = spjs.utility.addItem(
        		{
        			"listName":_spPageContextInfo.pageListId,
        			"data":data
        		}
        		);
        		if(newItem.success){
        			location.href = location.pathname.substring(0,location.pathname.lastIndexOf("/")) + "/EditForm.aspx?ID="+newItem.id;
        		} else{
        			alert(newItem.errorText);
        		}
        	}
        }

        Thanks in advance!

        • This topic was modified 9 years, 1 month ago by soldier1733.
        Attachments:
      • #8898
        Alexander Bautz
        Keymaster

          This most likely has to do with wrong format of the value in the data object. Which type of fields are the ones you try to update?

          Use console.log to look at the data object before the “newItem =…” line – post the output here.

          Alexander

        • #8901
          soldier1733
          Participant

            The fields I am trying to update are People Picker fields for SP2013. Here is the output of the data object:
            [object Object]
            {
            [functions]: ,
            CEO: [
            0: “Jane Doe”,
            length: 1
            ],
            COO: [
            0: “John Doe”,
            length: 1
            ],
            GM: [
            0: “Janice Doe”,
            length: 1
            ],
            Manager: [
            0: “Jeff Doe”,
            length: 1
            ],
            Office_x0020_Location: “Corporate”,
            Requestor: [
            0: “Janet Doe”,
            length: 1
            ],
            Title: “REQ2015-208”
            }

          • #8903
            Alexander Bautz
            Keymaster

              Updating people pickers requires the ID and not the user display name – also the returned value is an array – so it fails on two fronts.

              You can try using this code to get the ID of the pp:

              data.GM = spjs.utility.userInfo(spjs.utility.getFieldValue({"fin":"GM","key":"loginName"})).ID

              Alexander

            • #8905
              soldier1733
              Participant

                Thank you Alex, that did the trick!!

              • #8921
                dinfante
                Participant

                  Thanks Alexander! that solved the same issue for me. What about field of type SPFieldDateTime? my field internal name is End_x0020_of_x0020_Week_x0020_Da

                • #8923
                  dinfante
                  Participant

                    I only need the date portion

                  • #8955
                    Alexander Bautz
                    Keymaster

                      The date and time in SharePoint DB is stored in ISO8601 format – October 25 would be entered like this:

                      2015-10-25 00:00:00

                      Alexander

                    • #9027
                      dinfante
                      Participant

                        I meant what is the custom string command I use for a Date field for the redirect to work? I’ve tried several things and nothing seems to work. I keep getting “Invalid date/time value. A date/time field contains invalid data. Please check the value and try again.”

                        data.End_x0020_of_x0020_Week_x0020_Da = ?

                      • #9034
                        Alexander Bautz
                        Keymaster

                          You must split the date and reassemble it in the correct format. If your date format is “US” like this: 10/27/2015 you can use something like this:

                          var d = getFieldValue("TheDateColName").split("/");
                          data.End_x0020_of_x0020_Week_x0020_Da = d[2]+"-"+d[0]+"-"+d[1]+" 12:00:00";

                          Alexander

                        • #10594
                          Penny Kingston
                          Participant

                            Hi Alex, I have used your code for saving and redirecting a form using the code below. It works great except that the required field of “SalesID” no longer seems to be required.

                            It appears that the code checks the field but there is no alert that forces someone to populate the field. Do you have any suggestions for making the salesID required using this code?

                            function GoToEdit(){
                            var ok = spjs.dffs.check_Mandatory([“SalesID”]),
                            data = {},
                            newItem,
                            url;
                            if(ok){
                            data.SalesID = getFieldValue(“SalesID”);
                            newItem = spjs.utility.addItem({“listName”:_spPageContextInfo.pageListId,”data”:data});
                            if(newItem.success){
                            url = location.pathname.substring(0,location.pathname.lastIndexOf(“/”)) + “/EditForm.aspx?ID=”+newItem.id;
                            if(GetUrlKeyValue(“IsDlg”)===”1″){
                            url += “&IsDlg=1”;
                            }
                            location.href = url;
                            }else{
                            alert(newItem.errorText);
                            }
                            }
                            }
                            function PreSaveAction(){
                            saveAndRedir();
                            }

                          • #10625
                            Alexander Bautz
                            Keymaster

                              Hi,
                              I suspect the field “SalesID” is not flagged as required in your list. If this is the case, either set it as required (in DFFS or in the list settings) or add this line of code to the top of your function:

                              spjs.dffs.data.requiredFields.push("SalesID");

                              Hope this helps,
                              Alexander

                            • #10629
                              Penny Kingston
                              Participant

                                Hi Alex,
                                I did have the field set as required but your line of code worked perfectly!
                                Many thanks!

                              • #10657
                                Penny Kingston
                                Participant

                                  Alex,
                                  I am now trying to use the same code on the edit form to save and stay on the edit form. However, using the code above does not work because it tries to create a new item as opposed to saving the existing item. My guess is that I need to modify the newItem line:

                                  NewItem = spjs.utility.addItem({“listName”:_spPageContextInfo.pageListId,”data”:data});

                                  I have seen “spjs.utility.addItem” and “spjs.utility.DeleteItem” used in your blogs, do you have a command to edit, save or update an item?
                                  Thanks!

                                • #10684
                                  Alexander Bautz
                                  Keymaster

                                    Hi,
                                    In editform you must use “spjs.utility.updateItem” like this:

                                    spjs.utility.updateItem({"listName":_spPageContextInfo.pageListId,"id":spjs.dffs.data.thisItemID,"data":{"Title":"New title value"}});

                                    Alexander

                                  • #10690
                                    Penny Kingston
                                    Participant

                                      Hi again,
                                      This code works and appears to update the “title” field with the pre-defined text “New Title Value”. How could I make this work to update any and all changes to any fields within the form? (There is no way to pre define them.)
                                      Thanks!

                                    • #10692
                                      Alexander Bautz
                                      Keymaster

                                        Hi,
                                        It was only an example – just replace the

                                        spjs.utility.addItem({...

                                        function with the one from the example, and use the “data” variable instead of the {“Title”:”New title value”} example.

                                        Alexander

                                      • #10694
                                        Penny Kingston
                                        Participant

                                          This is my new code for the “save and edit” button. It is not saving any new or updated data, it is simply refreshing the original data. Thanks again!

                                          function GoToEdit(){
                                          spjs.dffs.data.requiredFields.push(“SalesID”);
                                          data = {};
                                          updateItem = spjs.utility.updateItem({“listName”:_spPageContextInfo.pageListId,”id”:spjs.dffs.data.thisItemID,”data”: data });
                                          if(updateItem.success){location.href = location.href;
                                          }else{alert(updateItem.errorText);} }

                                        • #10709
                                          Alexander Bautz
                                          Keymaster

                                            Hi,
                                            To use this approach you skip the default save method in SharePoint and basically “inject” the data using the “updateItem” function. This means you must add ALL field data (or at least the fields that are changed) to the “data” object.

                                            In your code example you pass an empty object, and thus no data is updated.

                                            Alexander

                                          • #12115
                                            Colin
                                            Participant

                                              Would it be possible to get this redirect and save without close functionality built into the configuration forms?

                                            • #12161
                                              Alexander Bautz
                                              Keymaster

                                                I’m still on the lookout for a good method to handle the redirect in SP 2013, but will hopefully be able to solve this soon. When it comes to “save and stay in form”, I might be able to add it in a later version, but it’s not so easy to support all kind of fields in a consistent way so I cannot promise anything.

                                                Alexander

                                              • #19525
                                                Jeff Lynch
                                                Participant

                                                  I am having a very hard time getting a lookup field to populate as an additional field.
                                                  I’ve pulled it out in console.log and it is a string and it is populated with getFieldValue but i get a generic error “The operation failed because an unexpected error occured”.

                                                  I pulled the value out and set it as a variable…same result here is my code, any ideas on how to populate a Lookup field?

                                                  function saveAndRedir(){
                                                  var ok = spjs.dffs.check_Mandatory([“Title”]), data = {}, newItem, url;
                                                  var subcat = getFieldValue(“Sub_x0020_Category”);
                                                  if(ok){
                                                  data.Title = (“Title”);
                                                  data.Sub_x0020_Category = subcat;

                                                  newItem = spjs.utility.addItem({“listName”:_spPageContextInfo.pageListId,”data”:data});
                                                  if(newItem.success){
                                                  url = location.pathname.substring(0,location.pathname.lastIndexOf(“/”)) + “/EditForm.aspx?ID=”+newItem.id;
                                                  if(GetUrlKeyValue(“IsDlg”)===”1″){
                                                  url += “&IsDlg=1”;
                                                  }
                                                  location.href = url;
                                                  }else{
                                                  alert(newItem.errorText);
                                                  }
                                                  }
                                                  }

                                                  function PreSaveAction(){
                                                  saveAndRedir();
                                                  return false;
                                                  }

                                                • #19564
                                                  Alexander Bautz
                                                  Keymaster

                                                    Hi,
                                                    Setting a lookup column in code like this requires you to pass the ID of the lookup and not the label. This would be the correct format in your data object:

                                                    data.Sub_x0020_Category = "123;#;#";

                                                    You can try this snippet in your form:

                                                    var subcat = jQuery("#dffs_Sub_x0020_Category1").find("select option:selected").val();
                                                    data.Sub_x0020_Category = subcat + ";#;#";

                                                    Alexander

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