spjs.utility.addItem creating 2 entries when called

Home Forums Classic DFFS spjs.utility.addItem creating 2 entries when called

Viewing 2 reply threads
  • Author
    Posts
    • #32830
      Amal Vellappillil
      Participant

        Hi Alex,

        I have a requirement to create a new list item in another list when a certain boolean field is marked as True. I have a rule that sets a variable to true when the boolean field is marked Yes. It also queues up an email to be sent. I have added the logic of creating new list item in dffs_PreSaveAction method.

        I am using DFFS v4.4.5.17
        Here is that function.

        function dffs_PreSaveAction(){
            if (!readyToCreateRecordInTrainingEvents) { return true; }
        
            var data = {
                "Title": getFieldValue("EventCode"),
                "EventBillingCode": getFieldValue("EventBillingCode"),
                "EventCategory": getFieldValue("EventCategory"),
                "EventTitle": getFieldValue("EventTitle"),
                "NumberofeventsinSeries_x002c_ifa": getFieldValue("Numberofeventsinseries_x002c_ifa"),
                "InstitutionInformation_x002c_Ins": getFieldValue("Institution_x0020_Information_x0"),
                "InstitutionInformation_x002c_Dep": getFieldValue("InstitutionInformation_x002c_Dep"),
                "InstitutionInformation_x002c_Con": getFieldValue("InstitutionInformation_x002c_Con"),
                "InstitutionInformation_x002c_Str": getFieldValue("InstitutionInformation_x002c_Str"),
                "InstitutionInformation_x002c_Cit": getFieldValue("InstitutionInformation_x002c_Cit"),
                "InstitutionInformation_x002c_Sta": getFieldValue("InstitutionInformation_x002c_Sta"),
                "InstitutionInformation_x002c_Zip": getFieldValue("InstitutionInformation_x002c_Zip"),
                "ContactPersonE_x002d_mailAddress": getFieldValue("ContactPersonE_x002d_mailAddress"),
                "ContactPersonPhoneNumber": getFieldValue("ContactPersonPhoneNumber"),
                "TakingRegistrationsorFlatFee_x00" :getFieldValue("LTPTakingRegistrationstoFlatFee_"),
                "FlatFeeAmount": getFieldValue("FlatFeeAmount"),
                "ParticipantRegistrationPrice": getFieldValue("ParticipantRegistrationPrice"),
                "ParticipantMinimum": getFieldValue("ParticipantMinimum"),
                "ParticipantMaximum": getFieldValue("ParticipantMaximum"),
                "LTPtoprovidealistofparticipants_" : getFieldValue("LTPtoprovidealistofparticipants_"),
                "CancellationFee": getFieldValue("CancellationFee"),
                "ContractSigneeName": getFieldValue("ContractSigneeName"),
                "ContractSigneeRole": getFieldValue("ContractSigneeRole"),
                "ContractSigningDepartment": getFieldValue("ContractSigningDepartment")
            }
            
            var dataToSend = {
            "listName": "Training and Events",
            "listBaseUrl": "https://intranet.ltp.org/training-and-events",
            "data": data
            }
        
            var resp = spjs.utility.addItem(dataToSend);
            
            if (resp.success)  {
                newRecordId = resp.id;
                newRecordLink = "https://intranet.ltp.org/training-and-events/Lists/TrainingEvents/DispForm.aspx?ID=" + newRecordId;
        
                return true;
            } else {
                spjs.utility.setFieldValue_SPFieldBoolean({fin:"ContractSent", newVal: false})
                dataJson = JSON.stringify(dataToSend);
                respJson = JSON.stringify(resp);
                
                //send error email to admin immediately
                spjs.dffs.processEmailTemplate("email_error_creating_new_record");
                
                spjs.dffs.alert({
                            "title": "ERROR",
                            "msg": "There was an error creating new record in training and events list.<br>Contract Sent field value has been reverted. Please save again. <br><br>An email has been sent to Administrator with the following information.<br><br> Error Code: " + resp.errorCode + "<br>Error Text: " + resp.errorText
                            });
        
                return false;
            }
           
        }

        For some reason, this is creating 2 list items in that different list. Do you see any issues with the code I have written? Or is this a bug? Please confirm.

        Thank you,
        Amal Vellappillil

        • This topic was modified 3 years, 9 months ago by Amal Vellappillil. Reason: added DFFS version v4.4.5.17
      • #32833
        Alexander Bautz
        Keymaster

          Hi,
          Not sure what could be causing this issue. Try adding a line with the text debugger; at the top of the dffs_PreSaveAction function to have it pause in the developer tools so you can see if it is actually triggering two times.

          It could be an issue with how the save is handled when using DFFS email – if it does trigger two times you can modify it like this:

          var listItemCreated = false;
          function dffs_PreSaveAction(){
              if (!readyToCreateRecordInTrainingEvents) { return true; }
              if(listItemCreated){
                  return true;
              }
              ...
              ...
              ...
              if (resp.success)  {
                  newRecordId = resp.id;
                  newRecordLink = "https://intranet.ltp.org/training-and-events/Lists/TrainingEvents/DispForm.aspx?ID=" + newRecordId;
                  listItemCreated = true;
                  return true;
              } else {
              ...
              ...
              ...

          Alexander

        • #32835
          Amal Vellappillil
          Participant

            So I put debugger at the top of the presaveaction function and sure enough it got hit twice. So I still dont understand what is causing it to be called twice, but the workaround you mentioned skips the item creation the second time around.

            Thank you so much for the workaround and tips on debugging.

            • #32852
              Alexander Bautz
              Keymaster

                I had a look at my code and the dffs_PreSaveAction will fire two times. If it passes all custom code when you click save, it must exit the function to process emails and will then retrigger save when emails have been sent (obviously sending them after the item has been saved would be better, but because DFFS does not handle the save itself it is hard to do this after it is saved).

                I’ll look into skipping the dffs_PreSaveAction function entirely when it returns to save after processing the emails.

                Alexander

              • #32856
                Amal Vellappillil
                Participant

                  Glad you are able to look into this and possibly skip duplicate calls to presaveaction method. It would be nice if the library could handle the logic than users remembering the little quirk and do the workaround on each and every form.

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