Jump to content
Sign in to follow this  
john9210

fmp17 Create Sunday records script

Recommended Posts

I’m trying to write a script that creates records for each Sunday in any two-month period. Below, the script creates records for January and February 2019.

It simply checks each day of the month to see if it is a Sunday and if so, it creates a record.

It does fine except that the second month portion of the script creates a record for the first Sunday in March! No doubt this is due to the fact that the Loop exits after 32 days. However it seems to me that since the DayOfWeek calculation is based $month2=2 (February) it should not result in a record with a March date. Is it necessary to make the loop dependent on the number of days in the month? I was hoping to avoid that additional complexity.

Or is there a simpler script do the job?

 

gYear = 2019

#CREATE RECORDS FOR FIRST MONTH

Set Variable [ $month1; Value:1 ]

Set Variable [ $day; Value:1 ]

Loop

If [ DayOfWeek ( Date($month1;$day;Schedule::gYear ))=1 ]

New Record/Request

Set Field [ Schedule::Date; Date($month1;$day;Schedule::gYear ) ]

Set Field [ Schedule::Flag; 1 ]

End If

Set Variable [ $day; Value:$day+1 ]

Exit Loop If [ $day=32 ]

End Loop

 

#CREATE RECORDS FOR SECOND MONTH

Set Variable [ $month2; Value:2 ]

Set Variable [ $day; Value:1 ]

Loop

If [ DayOfWeek ( Date($month2;$day;Schedule::gYear ))=1 ]

New Record/Request

Set Field [ Schedule::Date; Date($month2;$day;Schedule::gYear ) ]

Set Field [ Schedule::Flag; 1 ]

End If

Set Variable [ $day; Value:$day+1 ]

Exit Loop If [ $day=32 ]

End Loop

Share this post


Link to post
Share on other sites

You should base your loops on actual dates, rather than just an index number. Why not set a variable $date to

$date = GetAsDate ( Date ($month1, 1, gYear ) )

Then have a single loop that increments $startDate by one each time, tests if it is a Sunday, does it's creation if it is, and exits the loop when

Month($date) = $month1 + 2 

(i.e. it has reached the month that is two months after the start month). Remember to exit the loop before testing whether that last date is a Sunday.

There are probably better ways to do this anyway, like finding the first date that is a Sunday and then just adding 7 to it repeatedly to find subsequent Sundays, and stopping when you reach Month 3.

 

Hope this helps.

Share this post


Link to post
Share on other sites
8 hours ago, rwoods said:

$date = GetAsDate ( Date ($month1, 1, gYear ) )

Why do you find it necessary to convert a date to date?

 

8 hours ago, rwoods said:

exits the loop when


Month($date) = $month1 + 2 

(i.e. it has reached the month that is two months after the start month).

The problem with this method is that when the requested period is November-December, the value of $month1 will be 11. And that means the loop will never exit, because Month($date) will never return 13.

 

 

 

Edited by comment

Share this post


Link to post
Share on other sites
4 hours ago, john9210 said:

I’m trying to write a script that creates records for each Sunday in any two-month period.

Putting aside for a moment the unnecessary complexity of your script, one cannot help wondering:

1. Why is it necessary to pre-create records for days on which nothing of interest has yet happened?

2. Supposing it is necessary, why not create records for all Sundays from now till doomsday (doomsday in Filemaker is December 31, 4000, so that would make around 100k records) and be done with it?

 

 

Edited by comment

Share this post


Link to post
Share on other sites

Back to your script: the reason why your method fails is that the Date() function happily rolls over the month and the year. So for example:

Date ( 2 ; 32 ; 2019 )

returns March 4 , 2019 and similarly:

Date (12 ; 32 ; 2019 )

returns January 1, 2020. This is what makes the Date() function such a powerful tool in date calculations.

 

Now, I would suggest you simplify your script to something like (untested):

Set Variable [ $startDate; Value: Date ( Get(ScriptParameter) ; 1 ; Schedule::gYear ) ]
Set Variable [ $endDate; Value: Date ( Get(ScriptParameter) + 2 ; 0 ; Schedule::gYear ) ]
Loop
  Exit Loop If [ $startDate > endDate ]
  If [ DayOfWeek ( $startDate ) = 1 ]
    New Record/Request
    Set Field [ Schedule::Date; $startDate ]
    Set Field [ Schedule::Flag; 1 ]
  End If
  Set Variable [ $startDate; Value:$startDate + 1 ]
End Loop
Commit Records[]

Calling  this script with a parameter of 5 should create a record for every Sunday in the period of May - June of the year in gYear.

Note that there is no checking for duplicates. Calling the script twice with the same parameter could result in having 2 records for each Sunday in the given range.

 

7 hours ago, rwoods said:

There are probably better ways to do this anyway, like finding the first date that is a Sunday

Right. And that would go something like (again, untested):

Set Variable [ $startDate; Value: Let ( 
day1 = Date ( Get(ScriptParameter) ; 1 ; Schedule::gYear ) ;
;
day1 + Mod ( 1 - DayofWeek ( day1 ) ; 7 )
) ]
Set Variable [ $endDate; Value: Date ( Get(ScriptParameter) + 2 ; 0 ; Schedule::gYear ) ]
Loop
  Exit Loop If [ $startDate > endDate ]
  New Record/Request
  Set Field [ Schedule::Date; $startDate ]
  Set Field [ Schedule::Flag; 1 ]
  Set Variable [ $startDate; Value:$startDate + 7 ]
End Loop
Commit Records[]

 

Edited by comment

Share this post


Link to post
Share on other sites

@comment Well, I'm not 100% sure how the implicit type-casting works with FM variables, so I was just ramming home the point to FileMaker that this was definitely a date!

Share this post


Link to post
Share on other sites

@rwoods

The result of Date() is date. The result of GetAsDate() is also date. If type-casting works with the data type returned by the calculation, then it must work the same for both. If it doesn't, then no amount of repeating the message will help.

 

Edited by comment

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Similar Content

    • By flutegirl
      To try to explain the setup as succinctly as possible:
      Database is running on a FM17 Server (on a Windows virtual(?) machine... Our IT department handles that part).
      The FM database does a number of imports every morning from our primary (4D) database, using an ODBC driver, but I have not found a way to have those imports run successfully from the server, so I have them running on an instance on a secondary user account on my desktop computer.
      The scripts are triggered by adding a record to a table, which is checked every five minutes using an OnTimer script.  I run a separate instance of FM17 on my user account so that I can use the database even if there is an import running.
      This solution has been working (fairly) well for a number of years, but last week, my desktop computer was updated to Mojave (10.14.6), and since that upgrade, it seems that the imports on the secondary account will only run while that user account is active.
      1) Is anyone else doing anything similar, where they have an instance of FM running on a separate user account?
      2) If so, have you had any trouble with Mojave, or have suggestions on what may be going on?
      3) Any suggestions on how to fix this?  My initial thought is to just open the database using FMP on the Server system and try having the imports run from there, but am concerned that might also have problems.
      Any suggestions would be greatly appreciated.
       
    • By OneStop
      I have a field called Price. I need to create another field called calculated price that will add a percentage depending on the value of the original Price field.
       
      Example:
      If the Price is $100.00 or more Subtract 10% from the price
      If the price is $10.00 or less Subtract .25%
      If the Price is between $10 and $50.00 Subtract .5%
      If the Price is between $50 and %100.00 Subtract .75%
       
      and so on and so on...
      So if I have a record with a Price of $1.00 the calculation will fill in the Calculated Price field with $.75
    • By BAleiHi
      I have a layout calculation that looks like:
      Round(If($$NoGPABump = 1;Classes::GPANoBump; Classes::GPABumped);2)
      In most instances, this rounds the appropriate value to 2 decimals  and displays those two digits.  But on occasion, it displays only one decimal as in this one:
      .  Instead I want to see 4.20.  If I round to, say, 4 decimals, it works properly: .  Rounding to 3 digits gives 4.199.  How do I get the trailing zero to show up?  This seems to be consistent for all data that rounds to a last digit of zero.  Is this a bug?  Is there a workaround?  Thanks,
       
    • By vinnygi
      I have a layout with a number of button bars, all with the same style. Each button is set to have an inner shadow and a white fill on Hover. For any of the buttons that call a popover, after the popover is closed, the hover shadow no longer works, but the fill still changes to white.  I've tried adding a refresh object script trigger when closing the popover, but that doesn't work.
      Some of the buttons are currently placeholders, which just call a script that exits itself so I can test the hover state.  Those work fine.
    • By Will_Logic
      Hi, I am using the Base Elements plugin Execute SQL command to select SQL on a table in another FileMaker file. For a file with many fields and quite a lot data in some fields, this times as about 1 minute just for SQL SELECT command for 1,000 rows. I haven't used MBS (monkeybread software) plugin yet, but I wonder if anyone knows if it is significantly faster?
      I.e. BE_FileMakerSQL vs. MBS(FM.SQL.Execute..) - is one significantly faster?
      Here's the code to select 1,000 rows for reference, $timer_seconds comes as about 60 seconds here. If I export same table internally in FileMaker using the menu export to Excel, it takes 29 minutes for all 41,000 rows, so timing seems similar for the Base Elements SQL. (NB there is some flag going on here in the FileMaker database so internal export gets 41,000 rows but script SQL SELECT COUNT(*) gets 200,000 rows for same table, but I don't think that is relevant to my question).
       
      Set Variable [$time1; Value: Get(CurrentTimeUTCMilliseconds)] Set Variable [$part_ca; Value: BE_FileMakerSQL ( "SELECT * FROM Contact_ACTIVITY OFFSET 100000 ROWS FETCH FIRST 1000 ROWS ONLY" ; "&≠≠&" ; "EOR≠≠EOR" ; "DataContacts11" )] Set Variable [$timer_seconds; Value: (Get(CurrentTimeUTCMilliseconds) - $time1) / 1000] MBS command I think would be something like:
      MBS( "FM.SQL.Execute"; "DataContacts11"; "SELECT * FROM Contact_ACTIVITY OFFSET 100000 ROWS FETCH FIRST 1000 ROWS ONLY" ; ...delimiters here I'm assuming.. ) We only have base FileMaker here on machine I am using, which can run the Base Elements plugin, but to run MBS plugin, needs FileMaker Pro Advanced to install it I think, am getting several databases in roughly same FM format where script workspace disabled, hence need to use SQL select run from script in my own external FileMaker file. Might consider getting FMP Pro Advanced if MBS is faster.
      Tx any suggestions!
  • Who Viewed the Topic

    7 members have viewed this topic:
    creeper  nick360Works  ryan360Works  paja  temp007  Scott Pon  JerrySalem 
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.