Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Cannot correct function error

Contributor ,
Mar 15, 2021 Mar 15, 2021

The script below is located in the custom calc of  AutoForHire dateObject field. The goal is to examine the value from NewBusinessName text field and ultimately assign it to a date using the script below. I have flipped this around every which way I could think of, but it just won't work. In the different scenarios I have tried, error messages included recognizing variable setDate statements as functions, which I have been stuck trying to correct. My latest attempt uses if/else conditions but assigns variables to the date modifications, and I've tried combining setDate and setMonth with and without as variables to clean it up also, but nothing works.  Any help greatly appreciated! File attached.

 

var firstLetter = this.getField("NewbusinessName").value;
currAFHDate = new Date();
var stAFHREGA = util.scand("mm/dd/yyyy", currAFHDate);
var stAFHREGA = currAFHDate.setMonth(05);
var stAFHREGA = currAFHDate.setDate(01);

var stAFHREGBC = util.scand("mm/dd/yyyy", currAFHDate);
var stAFHREGBC = currAFHDate.setMonth(06);
var stAFHREGBC = currAFHDate.setDate(01);

var stAFHREGDE = util.scand("mm/dd/yyyy", currAFHDate);
var stAFHREGDE = currAFHDate.setMonth(07);
var stAFHREGDE = currAFHDate.setDate(01);


if(firstLetter === "") {event.value = "";}

else if (firstLetter === "a"||"A") {event.value = util.printd("mm/dd/yyyy", stAFHREGA);}
else if (firstLetter === "b"||"B"||"c"||"C") {event.value = util.printd("mm/dd/yyyy", stAFHREGBC);}
else if (firstLetter === "d"||"D"||"e"||"E") {event.value = util.printd("mm/dd/yyyy", stAFHREGDE);}

TOPICS
How to , JavaScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Contributor ,
Mar 15, 2021 Mar 15, 2021

found this mistake and thought I solved it- not!

var currAFHDate = new Date();

 

yes, that did it Bernd! Thank you!! I haven't seen it done that way before- thanks for the education!!!

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2021 Mar 15, 2021

You can't use the functions setMonth and setDate on text strings. You must use date objects.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 15, 2021 Mar 15, 2021

I thought the util.scand converted it into a date object acceptable value, and then this is folllowed by the util.printd to display the field's value. Where am I going wrong?

 

var firstLetter = this.getField("NewbusinessName").value;
currAFHDate = new Date();
var stAFHREGA = util.scand("mm/dd/yyyy", currAFHDate);

stAFHREGA = currAFHDate.setMonth(05);
stAFHREGA = currAFHDate.setDate(01);

event.value = util.printd("mm/dd/yyyy", stAFHREGDE);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 15, 2021 Mar 15, 2021

my latest attempt. Cannot get past this function error. Do I have to declare a variable for the individual date and month when setting them? Do I need to insert a util.scand following the setDate script? Nothing I seem to try works- I know I must be missing something simple but I just can't see it? Extra eyes please!!!!

 

 

var firstLetter = this.getField("NewbusinessName").value;
currAFHDate = new Date();
var startAFHDate = util.scand("mm/dd/yyyy", currAFHDate);

if (firstLetter == "") event.value = "";

else if (firstLetter.toLowerCase() == "a") startAFHDate.setMonth(05).setDate(01);
else if ((firstLetter.toLowerCase() == "b")||(firstLetter.toLowerCase() == "c")) startAFHDate.setMonth(06).setDate(01);
else if ((firstLetter.toLowerCase() == "d")||(firstLetter.toLowerCase() == "e")) startAFHDate.setMonth(07).setDate(01);

event.value =util.printd("mm/dd/yyyy", startAFHDate);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2021 Mar 15, 2021

You can set month and day like this:

startAFHDate.setMonth(5,1);

or

startAFHDate.setMonth(5);

startAFHDate.setDate(1);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 15, 2021 Mar 15, 2021

found this mistake and thought I solved it- not!

var currAFHDate = new Date();

 

yes, that did it Bernd! Thank you!! I haven't seen it done that way before- thanks for the education!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2021 Mar 15, 2021
LATEST

You can make your code more legible if you moved to the toLowerCase command to the first line, and then you could remove it from all the next lines...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2021 Mar 15, 2021

This is not valid syntax:

else if (firstLetter === "a"||"A")

It needs to be:

else if (firstLetter == "a" || firstLetter == "A")

 

Although, an easier way to do it would be:

else if (firstLetter.toLowerCase() == "a")

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 15, 2021 Mar 15, 2021

yes, that's much more efficient. Thank you try67!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines