Jump to content

open layout at a random record


This topic is 3394 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hi guys, thanks for the great forum - I have learned a great deal in the weeks since I have been using FM.

 

Can I ask a couple on data access when a layout is opened please.

 

It looks like the default position is for a layout to open and display the last entered record. Is there any control over this (i.e adding parameters like sql would). So only open recors where field x equals value z for example. 

 

Also I have a requirement to open a layout and display a record at random from any that are available in the table the layout is pointed to. The key is the random bit here! it must be different each time if possible!

 

Many many thanks

Link to comment
Share on other sites

The "Random" function will give you a number between 0 and 1 (not including 1).  So if you multiply that with the record count of the table (and round it up) it will give you a random record number.  Then use the Go To Record script step to take you there.

 

As to "So only open records where field x equals value z for example", that sounds like a find request that gets executed as part of the scripted workflow that takes the user to the layout.

Link to comment
Share on other sites

To open a layout and have a random record appear, use script trigger OnLayoutEnter with script step of:

Go To Record/Request/Page [ by calculation ; Random * Min ( Get ( FoundCount ) ; Get ( TotalRecordCount ) ) ]

... but do you want it to do this based upon total records in the table?  What if only a found set is being displayed?  You may wish to include a Show All Records and use Get ( TotalRecordCount ).  I included instead how to handle this situation by modifying the calculation above.

 

As for your first request, the best answer would depend upon the purpose and type of data.  For instance, if the record must hold a person's name in field called AccountName then it is best to use Security to restrict the User.  You could also use OnLayoutEnter to perform a find for the desired records.  Can you explain the purpose here?  


I didn't see Wim's response before posting. :-)

Link to comment
Share on other sites

BTW, I should mention that my calculation will include record 1 but will not include the highest record.  If you add a 1 to the end of the calc then it will include the highest record but not record 1 (as Wim indicates).  

 

Actually, this works and includes both record 1 and the highest record:

Int ( 
Random * Min ( Get ( FoundCount ) ; Get ( TotalRecordCount ) ) + 1
)
Link to comment
Share on other sites

The "Random" function will give you a number between 0 and 1 (not including 1).  So if you multiply that with the record count of the table (and round it up)

 

Not including 1,  but including 0. That's why you cannot round up. You must round down, then shift the 0 point to the minimum of the desired range (1 in this case, since record numbering starts from 1).

 

 

 

Go To Record/Request/Page [ by calculation ; Random * Min ( Get ( FoundCount ) ; Get ( TotalRecordCount ) ) ]

 

Why is this necessary::

Min ( Get ( FoundCount ) ; Get ( TotalRecordCount ) )

Is there a scenario where Get ( FoundCount ) can be larger than Get ( TotalRecordCount )?

Link to comment
Share on other sites

Why is this necessary::

Min ( Get ( FoundCount ) ; Get ( TotalRecordCount ) )

Is there a scenario where Get ( FoundCount ) can be larger than Get ( TotalRecordCount )?

 

AAAKKKK!!  Of course ... the found count IS the total count if all records are shown ... and I knew it but forgot in this instance.  Then the calculation could be simpler and will handle all record sets:

Int ( 
Random * Get ( FoundCount ) + 1
)

If you see a better way please do tell ... learning the best approach is ALWAYS the most important thing.  Thank you again!  :-)

BTW, I didn't want to use Get ( TotalRecordCount ) because if there was a smaller found set, the Go To Record step would fail if it tried to jump to a number higher than available.

Link to comment
Share on other sites

This topic is 3394 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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