3 Replies Latest reply on Apr 30, 2012 11:59 AM by philmodjunk

    Add Record If Search Fails

    ZoocMan85

      Title

      Add Record If Search Fails

      Post

       Hey Everyone,

      Im trying to write a script if when a user selects search and filemaker goes into find mode. If a clients information is not found in the database...Id like filemaker to prompt the user and say...this user is not found in the datebase. Would you like to add this user. Right now all that happens is you will get modify find or cancel. If you hit cancel everything gets deleted. Whats the easiest way to do this? Thanks

        • 1. Re: Add Record If Search Fails
          philmodjunk

          Use set error capture [on] to suppress this dialog so that it does not interrupt your script. Use Get ( lastError ) to determine why no records were found.

          Enter Find mode [pause]
          Set Error Capture [on]
          Perform Find[]
          Set Variable [$ErrorCode ; get ( LastError ) ]
          If [$ErrorCode = 401 // no records were found ]
             Show Custom dialog ["This user was not found in the database. Add this user?"] // with button 1 as "yes" and button 2 as "No"
             If [ get ( lastmessagechoice ) = 1 // "yes" was clicked.]
                 New Record/Request
                 #add any other steps needed to add a new user here.
             End If
          Else If [ $ErrorCode ≠ 0 //some other error occurred such as not entering any find criteria ]
              Show custom dialog ["Please enter valid find criteria"]
          End If

          • 2. Re: Add Record If Search Fails
            ZoocMan85

            Phil That worked but normally they will search by the users full name. How do i have it create a new record then paste the users full name after a new record was created that failed in find mode ?

            • 3. Re: Add Record If Search Fails
              philmodjunk

              Assuming that this is a single field called "Name" in your table...

              Enter Find mode [pause]
              Set Variable [$Name ; value: YourTable::Name ]
              Set Error Capture [on]
              Perform Find[]
              Set Variable [$ErrorCode ; get ( LastError ) ]
              If [$ErrorCode = 401 // no records were found ]
                 Show Custom dialog ["This user was not found in the database. Add this user?"] // with button 1 as "yes" and button 2 as "No"
                 If [ get ( lastmessagechoice ) = 1 // "yes" was clicked.]
                     New Record/Request
                     Set Field [YourTable::Name ; $Name ]
                     #add any other steps needed to add a new user here.
                 End If
              Else If [ $ErrorCode ≠ 0 //some other error occurred such as not entering any find criteria ]
                  Show custom dialog ["Please enter valid find criteria"]
              End If

              You can also use a script where the user enters the name into a global text field. Such a script would not need to pause while in find mode and the script enters find mode without pausing, then sets up the criteria with a set field step and performs the find. With that approach, the name is already present in the global text field for entering into the field of a new record.