Home › Forums › Classic DFFS › Custom JS problem
- This topic has 11 replies, 2 voices, and was last updated 4 years, 4 months ago by Alexander Bautz.
-
AuthorPosts
-
-
August 24, 2020 at 17:09 #31314
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, 4 months ago by Phil Grant.
- This topic was modified 4 years, 4 months ago by Phil Grant.
-
August 24, 2020 at 20:16 #31321
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
-
August 25, 2020 at 08:09 #31324
So I get his as a response
<DIV> <DIV></DIV></DIV>
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, 4 months ago by Phil Grant.
-
August 25, 2020 at 16:30 #31334
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
-
August 26, 2020 at 11:20 #31338
Thanks for that, I’ve tried a few different approaches, none succesfull so far.
-
August 26, 2020 at 12:44 #31340
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, 4 months ago by Phil Grant.
-
August 26, 2020 at 18:37 #31348
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
-
August 27, 2020 at 10:56 #31365
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?
-
August 27, 2020 at 19:42 #31380
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
-
August 28, 2020 at 09:04 #31405
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, 4 months ago by Phil Grant.
Attachments:
-
August 28, 2020 at 09:18 #31411
Sorry, I should have said the comment feilds are all multiline plain text fields.
-
August 28, 2020 at 15:38 #31422
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
-
-
AuthorPosts
- You must be logged in to reply to this topic.