FileMaker PHP returning the last recordID

 In FileMaker
FileMaker PHP returning the last record ID

A new record can be created using the FileMaker PHP API in two ways but only on method allows for a efficiently returning the last recordID.

  • createRecord()
  • newAddCommand()

The primary differences between these methods are the result they return. Plenty of other blogs posts go into the specifics between the two so I won’t, though what does concerned us is that using and executing the newAddCommand() will return an array of information, including the recordID.

Take this example:

$NewRecord = $fm->newAddCommand('web_my_account');
$NewRecord->setField('_SchoolID t',$_SESSION['SchoolID']);
$NewRecord->setfield('NameFirst t','New');
$NewRecord->setfield('NameLast t','User');
$result = $NewRecord->execute();
$tmp = current($result->getRecords());
$UserRecID = $tmp->getRecordID()

The user clicked a “new user” button, the page was refreshed and executed the above code. We are adding the new user record, setting a few fields and then committing the record. We then want them to see this new record on the page allowing them to make any further changes. We could now use newFindCommand() locate the new record and then getField() as required, but there is a simpler way, use the recordID in $result directly.

$result contains an array (which can be viewed with echo print_r($result);). We then use the php current() function to return the value of the $result array element that’s currently being pointed to by the internal pointer. From there we get the recordID from the new variable, in this case $tmp and set $UserRecID.

In two lines of code we have efficiently located the record we just created and can start setting or getting field data from $UserRecID.

If you have any questions or queries feel free to contact us

Recommended Posts
Showing 2 comments
  • Rob Riley

    Thank you!! That’s exactly what I was looking for. Really useful.

    • Daniel Smith

      Glad it helped Rob!

Leave a Comment