Home › Forums › SPJS-Utility › Check Note field and replace value
- This topic has 14 replies, 2 voices, and was last updated 2 years, 7 months ago by Paul Heffner.
-
AuthorPosts
-
-
April 22, 2022 at 18:14 #35786
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
-
April 22, 2022 at 22:36 #35787
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
-
April 24, 2022 at 00:32 #35792
It is a rich text field and I want it if the field is only /intro
-
April 25, 2022 at 16:55 #35793
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
- This reply was modified 2 years, 8 months ago by Alexander Bautz.
-
April 25, 2022 at 17:22 #35795
Does it make a difference if it is an enhanced rich text?
First test didnt work quit right
-
April 25, 2022 at 21:52 #35796
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
-
May 3, 2022 at 18:02 #35805
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
-
May 3, 2022 at 20:15 #35807
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
-
May 3, 2022 at 21:33 #35808
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;
}
-
May 4, 2022 at 16:48 #35818
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
-
May 5, 2022 at 12:18 #35825
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.
-
May 5, 2022 at 18:08 #35826
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
-
May 6, 2022 at 13:24 #35827
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?
-
May 6, 2022 at 14:14 #35828
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
-
-
May 6, 2022 at 15:36 #35829
Thanks again. The currUserInfo wasnt working but I did use spjs.utility.userprofile().preferredname
-
-
AuthorPosts
- You must be logged in to reply to this topic.