FileMaker-side CWP scripts

posted by Jeremiah Small Monday, July 31, 2006

Q: Given a file with just base table references can I use the script and go to a layout in the script and create the records.

A: Sure. No problem, although Having relationships between your FileMaker TOs gives you lots of additinal options. What might help in terms of visualizing what you can do is to remember that you are always acting on layouts in virtual windows from CWP. You still have a found set and multiple windows! (nice.) Check the web compatibility bit in ScriptMaker and you will see that the “New Window” step turns into “New Window (VIRTUAL WINDOW ON WEB)” when used in a script.

One trick I use commonly to preserve a session record context with CWP is I use a CWP find to isolate my base record, and then run a FileMaker script on that record (as needed, invoking new virtual windows to create processing sets i.e line items or what have you). This can make your PHP scripting pretty simple, and really helps performance, because all the scripting becomes FileMaker side. That is to say, if you need to loop over a found set or what have you, controlling that from PHP would be very inefficient, as each iteration of the loop would require a new database transaction. If the loop is created as a FileMaker script, you simply have to find or create your base record, and then call the FileMaker script as a follow-on.

So for example a generic find function using the filemaker.php (assumes you have defined some named constants):
function findRecord($LayoutName,$CriterionArray,$scriptName="")
{
$fm = new FileMaker(FMDB_WEB,FMWPE_HOST,FMWPE_USER,FMWPE_PASS);

$findReq =& $fm->newFindCommand($LayoutName);

foreach($CriterionArray as $k=>$v)
$findReq->addFindCriterion($k,"==" . $v);

if($scriptName)
$findReq->setScript($scriptName);

$result = $findReq->execute();

return $result;
}
With this function, you can establish a found record and call a
FileMaker-side processing script that may look something like this in a
simple case:
If [ not IsEmpty ( app_RNK__RankJoin::_kf_applicantID ) ]
Go to Related Record [ From table: “app_RNK__RankJoin”; Using layout:
“web_rank” (RNK__RankJoin) ]
[ Show only related records ]
Go to Record/Request/Page
[ First ]
Loop
If [ GetAsNumber ( RNK__RankJoin::applicantChoice ) = "" ]
Set Field [ RNK__RankJoin::applicantChoice; "NA" ]
Omit Record
Exit Loop If [ Get ( FoundCount ) < 1 ]
Else
Go to Record/Request/Page
[ Next; Exit after last ]
End If
End Loop
Sort Records [ Specified Sort Order: RNK__RankJoin::applicantChoice; ascending
rnk_SCH_Schools::schoolName; ascending ]
[ Restore; No dialog ]
Replace Field Contents [ RNK__RankJoin::applicantChoice; Replace with
serial numbers: Custom values; Initial value: 1; Increment value: 1 ]
[ No dialog ]
End If
Go to Layout [ original layout ]
In a more complicated case, you might use virtual windows for subroutines:
#  Check for non-unique account name.
Close Window [ Name: "temp"; Current file ]
New Window (VIRTUAL WINDOW ON WEB) [ Name: "temp" ]
Set Error Capture [ On ]
Enter Find Mode [ ]
Set Field [ APP__Applicants::_ka_username; $username ]
New Record/Request
Omit Record
Set Field [ APP__Applicants::__kp_applicantID; $appID ]
Perform Find [ ]
Set Error Capture [ Off ]
If [ Get ( FoundCount ) > 0 ]
Close Window [ Name: "temp"; Current file ]
Set Field [ APP__Applicants::scriptResult; $result ]
Exit Script [ Result: $result ]
Else
Close Window [ Name: "temp"; Current file ]
End If

0 Comments:

Post a Comment

Links to this post:

Create a Link