Check Note field and replace value

Forums SPJS-Utility Check Note field and replace value

Viewing 13 reply threads
  • Author
    Posts
    • #35786
      Paul Heffner
      Participant

      I have a JS script that I would like to look at a note field(multiple lines of text), and look to see if it is a specific value. If it is I would like to replace the text. Here is what I have that doesn’t seem to do anything. I am doing this on the edit form if that matters.

      function dffs_PreSaveAction() {

          var sc = getFieldValue(“NewComment”);

          if(sc === “/Intro”){

              setFieldValue(“NewComment”,”<desired text>”);

          }

          return true;

      }

      I did confirm the fields internal name is indeed NewComment

    • #35787
      Alexander Bautz
      Keymaster

      Hi,
      Is this a plain text field or a rich text field?

      Also, is the value (the entire value in the field) “/Intro” or do you want to search-replace that text among other text?

      Alexander

    • #35792
      Paul Heffner
      Participant

      It is a rich text field and I want it if the field is only /intro

    • #35793
      Alexander Bautz
      Keymaster

      When you use getFieldValue on a rich text field it return the full HTML code and not only the text contents – like this:

      <p>/intro<span id="ms-rterangecursor-start" rtenodeid="1"></span><span id="ms-rterangecursor-end"></span><br></p>
      

      To see if the value is only /intro we must use some code to read the text content:

      
      var html = getFieldValue("NewComment");
      var text = jQuery(html).text();
      if(text === "/intro"){
          setFieldValue("NewComment", "<p>The new content</p>");
      }
      

      Alexander

    • #35795
      Paul Heffner
      Participant

      Does it make a difference if it is an enhanced rich text?

      First test didnt work quit right

    • #35796
      Alexander Bautz
      Keymaster

      Should not matter what kind of rich text field it is. Try using alert or console.log to see the value of the field. You can run the code directly in the browser dev tools by hitting F12 > Console to see the output directly.

      Alexander

    • #35805
      Paul Heffner
      Participant

      this is the error:

      Uncaught Error: Syntax error, unrecognized expression: /Intro<br>

          at Function.fa.error (jQuery.js?v=1618860436703:2:12733)

          at fa.tokenize (jQuery.js?v=1618860436703:2:18786)

          at fa.select (jQuery.js?v=1618860436703:2:21594)

          at Function.fa [as find] (jQuery.js?v=1618860436703:2:7320)

          at n.fn.init.find (jQuery.js?v=1618860436703:2:24109)

          at new n.fn.init (jQuery.js?v=1618860436703:2:24676)

          at n (jQuery.js?v=1618860436703:2:406)

          at dffs_PreSaveAction (eval at <anonymous> (jQuery.js?v=1618860436703:2:2651), <anonymous>:5:11)

          at PreSaveItem (eval at <anonymous> (DFFS_frontend_min.js:9:1), <anonymous>:1:271281)

          at HTMLInputElement.onclick (EditForm.aspx?ID=3691&Source=http%3A%2F%2Fsharepoint2.bankofamerica.com%2Fsites%2FCCSSMandR%2FMIS%2FLists%2FCCSS%20MIS%20Request%20Form%2FPersonalViews.aspx%3FPageView%3DPersonal%26ShowWebPart%3D{D510D88B-5708-477A-B27F-2EFEC05DD796}:1654:710)

      fa.error @ jQuery.js?v=1618860436703:2

      fa.tokenize @ jQuery.js?v=1618860436703:2

      fa.select @ jQuery.js?v=1618860436703:2

      fa @ jQuery.js?v=1618860436703:2

      find @ jQuery.js?v=1618860436703:2

      n.fn.init @ jQuery.js?v=1618860436703:2

      n @ jQuery.js?v=1618860436703:2

      dffs_PreSaveAction @ VM1646:5

      PreSaveItem @ VM1617:1

      onclick @ EditForm.aspx?ID=3691&Source=http%3A%2F%2Fsharepoint2.bankofamerica.com%2Fsites%2FCCSSMandR%2FMIS%2FLists%2FCCSS%20MIS%20Request%20Form%2FPersonalViews.aspx%3FPageView%3DPersonal%26ShowWebPart%3D{D510D88B-5708-477A-B27F-2EFEC05DD796}:1654

    • #35807
      Alexander Bautz
      Keymaster

      It looks like you might have an error in your code – if you add the custom js here I can take a look at it.

      Alexander

    • #35808
      Paul Heffner
      Participant

      function dffs_PreSaveAction() {

          var html = getFieldValue(“NewComment”);

                      var sc = jQuery(html).text();

          if(sc === “/Intro”){

              setFieldValue(“NewComment”,”<p>Your request is now in progress. Please keep an eye on your e-mail for additional communication from BSE Reporting & Insights. If we have any additional questions about your request we will contact you through this form as well. You can also add comments to this request if you have any questions or additional information to provide and we will automatically be notified.</p>”);

          }

          return true;

      }

    • #35818
      Alexander Bautz
      Keymaster

      It looks like this method didn’t work when the HTML had a
      tag. Change this line like this to use a regex instead:

      var sc = jQuery(html).text();
      
      var sc = html.replace(/<.*?>/g, "");
      

      Alexander

    • #35825
      Paul Heffner
      Participant

      That seems to work but still having an issue. I have another rule that appends the NewComment field to historical comments. It is set to trigger on form save. I think that is triggering first, so in the historical comments I see /Intro but I do now see the updated New Comment.

      I just need the PreSave action to fire off before the rule save trigger. Or a better way to do this

      Just adding some context. We use to use a field that would append on its own in SharePoint but it wouldn’t export and ended up doing this other method. NewComment is only there to provide a space to put info that then gets appended to historical comments.

    • #35826
      Alexander Bautz
      Keymaster

      Rules triggering on save are triggered before the dffs_PreSaveAction. I suggest you do the appending all in custom js instead – something like this:

      var currVal = getFieldValue("The_append_field");
      var newValue = getFieldValue("The_new_value_field");
      setFieldValue("The_append_field", currVal + "

      " + newValue);

      Alexander

    • #35827
      Paul Heffner
      Participant

      You have been awesome but on last question. If I was using {currentUser} and timestamp in the rule, how does that translate into the JS?

      • #35828
        Alexander Bautz
        Keymaster

        You can get those values like this:

        var currUserDisplayName = spjs.utility.userInfo(spjs.dffs.data.currUserID).Title;
        var timestamp = spjs.dffs.formatDate(new Date(), "MM/dd/yyyy (HH:mm:ss)");
        

        Alexander

    • #35829
      Paul Heffner
      Participant

      Thanks again. The currUserInfo wasnt working but I did use spjs.utility.userprofile().preferredname

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