Custom JS problem

Home Forums Classic DFFS Custom JS problem

Viewing 11 reply threads
  • Author
    Posts
    • #31314
      Phil Grant
      Participant

        Hi,
        I have an issue with some custon JS, I’m running a function on form save to check if there is some text in a comments box and if there is then move the comments to another field and add a timedate stamp.
        The if statement seeems to be firing regardless of wether the comments field is empty or not.

        Am I using the correct method to check the field has no text in it, I also tried

        if(a !="")
        function ETLogSigs(){
        	var a = getFieldValue("ET_x0020_Comments"), b = getFieldValue("CCB_x0020_Comments"), c, d = getFieldValue("ET_x0020_Approver"), e = getFieldValue("ET_x0020_Approval");
        if(a !== ""){
        c = new Date().toLocaleString(_spPageContextInfo.currentCultureName);
        setFieldValue("CCB_x0020_Comments", "["+c+" - "+e+" - "+d+"]"+"\n"+a+(b!==""?"\n"+"================================================"+b:""));
        // Clear the Log field
        setFieldValue("ET_x0020_Comments","");
        }
        }
        • This topic was modified 4 years, 3 months ago by Phil Grant.
        • This topic was modified 4 years, 3 months ago by Phil Grant.
      • #31321
        Alexander Bautz
        Keymaster

          If your comments field is rich text it is NOT empty like “”, but might actually contain an empty p-tag like this:

          <p></p>

          Try using console.log like this (hit F12 > Console and paste it at the cursor):

          console.log(getFieldValue("ET_x0020_Comments"));

          Alexander

        • #31324
          Phil Grant
          Participant

            So I get his as a response

            <DIV>
            <DIV></DIV></DIV>&nbsp;

            Should I check for this text then? if so then how? or is there another way, should I make the field plain text?

            Thanks for the help,
            Phil

            • This reply was modified 4 years, 3 months ago by Phil Grant.
          • #31334
            Alexander Bautz
            Keymaster

              You can do it like this:

              var a = jQuery(getFieldValue("ET_x0020_Comments")).text();

              This will take only the text content and strip away any HTML.

              Alexander

            • #31338
              Phil Grant
              Participant

                Thanks for that, I’ve tried a few different approaches, none succesfull so far.

              • #31340
                Phil Grant
                Participant

                  I tried that but it wouldn’t work, the if statement never triggered so I changed it to

                  var a = jQuery(getFieldValue("Additional_x0020_CCB_x0020_Comme")).val();

                  But this then triggered every if statement, there are four for different comment boxes.
                  I have to admit I’m out of my comfort zone with Javascript !!
                  I realise it is probably triggering on some html in the field but it’s now a plain text field.

                  • This reply was modified 4 years, 3 months ago by Phil Grant.
                • #31348
                  Alexander Bautz
                  Keymaster

                    It’s hard to tell exactly without looking at it, but if you open the form in a full page (not in a dialog), open the console and then types in this:

                    var a = jQuery(getFieldValue("YOUR_FIELD_INTERNAL_NAME_HERE")).text();
                    console.log(a);

                    Replace YOUR_FIELD_INTERNAL_NAME_HERE with the name of your rich text field. What does it show?

                    Also, if you have to have input in all (a, b, d and e variables) you must change your if like this:

                    if(a !== "" && b !== "" && d !== "" && e !== ""){
                      // All fields have a value - do your thing
                    }else{
                      // At least one field missing info - consider an alert to the user to inform
                    }

                    Alexander

                  • #31365
                    Phil Grant
                    Participant

                      I seem to be getting to the bottom of this, when in Dev Console the jQuery will return the text from an already populated feild in Edit view but doesn’t return the text in a field that has had text entered but not yet saved, does this sound right?

                      Should jQuery be able to read the data from a field before the form has been saved?

                    • #31380
                      Alexander Bautz
                      Keymaster

                        That doesn’t sound right. What kind of field type is it – a normal multiline rich or enhanced rich text? – can you include a few sceenshots of the form and the console when you try the getFieldValue.

                        Alexander

                      • #31405
                        Phil Grant
                        Participant

                          All the comments fields are no plain text fields.
                          The JS checks the comments fields (in Red ellipses) and if there is some text in one then the text is added to the Comments history field at the bottom along with a timestamp and the approver (Blue elipses), each comments field is checked independantly.
                          The attached images show the form layout and the results in the console log window after typing in some text.
                          If I query another field that already has text in it it returns the text (second screenshot)

                          • This reply was modified 4 years, 2 months ago by Phil Grant.
                        • #31411
                          Phil Grant
                          Participant

                            Sorry, I should have said the comment feilds are all multiline plain text fields.

                          • #31422
                            Alexander Bautz
                            Keymaster

                              If all fields are multiline plain text you can use

                              var a = getFieldValue("Name_of_field");
                              if(a !== ""){
                                // Not empty - do your thing here
                              }

                              I’m not sure why the dev tool in IE does not show your the value of the variable a, but you can just type in a in the console and hit enter to see the value.

                              I understand that you might be stuck with internet explorer as the default browser, but when developing you have a much better debugging tool in Google Chrome or the new Chromium based Edge browser.

                              Alexander

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