14 Replies Latest reply on Jan 27, 2016 5:24 AM by ElpicFayl

    Script creating a new record not working

    ElpicFayl

      Hi there,

      So I am trying to make a basket like thing.

      A client has access to some products after getting logged in depending of the Company he works in (so the display of available products is in a portal depending of the Company table),

      He can add them to his basket via a + button or substract them via a - button

      In order to avoid creating an order each time a client logs in, I only create my order when a client adds the first item to his basket.

      But my script appears to be broken.

      Here is the code of it:

      If [ IsEmpty ($$OrderId)]

         Set Variable [ $$ClientId; Value: Client::__id ]

         New Record/Request []

         Set Field [ Order:: ClientId ; $$ClientId]

         Commit Records/Requests []

         Set Variable [ $$OrderId; Value: Order:: __id]

      End if

       

      Each time I try to use my script I have a 415 Error with the script debugger and I don't understand where I made a mistake.

      If you have any idea or can point me to my error.

        • 1. Re: Script creating a new record not working
          ElpicFayl

          Apparently I managed to find a work around.

          I have to go to Order Layout and then create my record.

          Quite not simple.

          Is there a better way to do it?

          Because I think the question will be the same with the Order Lines table.

          • 2. Re: Script creating a new record not working
            DanielShanahan

            It sounds like a context issue, which would explain why you need to go to the Order layout.  (I suspect you are trying to call the script from the Client layout.)

             

            Error 415 reads:

            One or more required related records are not available

             

            More info would be helpful.  It seems to me that if I was on the Client layout I'd want to see:

            1. List of orders (in a portal)

            2. Which orders are still active.

            3. What is in those orders.

            4. Ability to complete an uncompleted order or create a new order.

             

            Can you post screenshots of the layout where you originally called the script (the one that gave you the 415 error) as well as a screenshot of your relationship graph?

            • 3. Re: Script creating a new record not working
              ElpicFayl

              Sure I'll do this as soon I am at home.

              Le 4 janv. 2016 18:37, "DanielShanahan" <noreply@filemaker.com> a écrit :

               

              <https://community.filemaker.com/?et=watches.email.thread> Script

              creating a new record not working

               

              reply from DanielShanahan

              <https://community.filemaker.com/people/DanielShanahan?et=watches.email.thread>

              in Discussions - View the full discussion

              <https://community.filemaker.com/message/532334?et=watches.email.thread#532334>

               

              • 4. Re: Script creating a new record not working
                ElpicFayl

                I quite agree with you that there is a question on context here, my relationships are perhaps quite messy, but I don't seem to manage to find where I made a mistake, I really need a more competent eye on it.

                 

                The list of the orders and the active ones are present on two separate layouts, that the client can access via an account parameters button, what was on those and can print them or download a .pdf of any order he made.

                 

                In fact the layout where I am is like a personalised products page for each of the clients, depending on the company they work for, some products are tailored for a specific company.

                The client clicks on a button near each of the products to add them to a basket.

                He can also click on the basket to see the products already in it, and then return shopping or order it.

                I'll post screenshots of the layouts and relationshiph graph, and also the full script, not a snipet of it, as soon as I am at work, since I got home too late to work on my own computer.

                It would be clearer than a written explanation

                • 5. Re: Script creating a new record not working
                  ElpicFayl

                  Hello here are the long awaited screenshots:

                  The layout:

                  Layout Screen.jpg

                  The Relationship Graph:

                  Relationships Graph.jpg

                   

                  The  Add Script:

                  Add Script.jpg

                   

                  I'd really like to script it in a better way.

                  The layout is in french but i don't think anything is not understandable, if you have difficulties just tell me I'd be happy to translate it for you.

                  FYI: Bon is the Order table

                  My actual script is now doing something strange, apparently I don't use the right way to get the values of a find request in variables.

                  • 6. Re: Script creating a new record not working
                    DanielShanahan

                    Thanks for the screenshots.  That helps.

                     

                    I'll just concentrate on one thing at this point.  There may be a few fixes needed but I find it helpful to work on one thing at a time.

                     

                    That one thing is if $$IdBon has a value.  In other words, If ( not is empty ( $$IdBon )...  It seems to me that your goal here is to gather the information when a user clicks on the green plus button.  You don't need to enter find mode to do this.  Since the button is inside the portal, it knows the portal row and the values.  So you can simple write:

                     

                    Set Variable [$NomProduit; Value: TYP_PRO::nom]

                    Set Variable [$PrixProduit; Value: TYP_PRO::prix]

                    etc.

                     

                    In other words, remove

                    Enter Find Mode []

                    Set Field [TYP_PRO::__id; $IdProduit]

                    Perform Find []

                     

                    There are a couple of other things that I would do for aesthetic reasons.  These are my personal preferences, so feel free to disregard.

                     

                    1. Add white space (i.e. blank lines) in your script.  It helps with readability.  I usually section off IF clauses, Loops, Variables, etc.

                    2. Add comments (e.g. "Verify the user is signed in" ; "User is signed in - add to selected items to cart").

                    3. The two If clauses can be combined into one.  For example:

                     

                    If [IsEmpty ( $$IdBon )]

                    ...

                    Else

                    ...

                    End If

                     

                    Since you only have two options, you don't have to specify if $$IdBon is not empty, but you certainly can if you want (it may help for readability).  In  that case, you'd write:

                     

                    If [IsEmpty ( $$IdBon )]

                    ...

                    Else If [not IsEmpty ( $$IdBon )]

                    ...

                    End If

                     

                    I'd start with that and see how that works.  One question: where/how is $$IdBon set?

                    • 7. Re: Script creating a new record not working
                      ElpicFayl

                      I tried not using the find mode but this way, my Set Variables didn't manage to get the right values, but those of my first product in my product table.

                       

                      About the aesthetic I totally agree with you with points one and two. I am a bit in a hurry right now because I have to produce a working product ASAP, or MTIQQQ (Muddle Through It Quick Quick Quick!) but they both are on my todo List.

                      As for point 3, it is way cleaner the way you did it than mine, and didn't thought about the If...Else step.

                      I am going to apply it immediatly.

                      • 8. Re: Script creating a new record not working
                        DanielShanahan

                        ElpicFayl wrote:

                         

                        I tried not using the find mode but this way, my Set Variables didn't manage to get the right values, but those of my first product in my product table.

                         

                        Try setting them before the "If" statements.

                         

                        Take a look at this Event Management file.  Click the "Select" button in the portal and you'll see the proper row is returned.

                        • 9. Re: Script creating a new record not working
                          ElpicFayl

                          Thanks a lot,

                          It's much smoother and less longer this way.

                          But I think I am still stuck with my workaround going to my Order list layout to add the product in my order list.

                          Do you see any other way to do it?

                          • 10. Re: Script creating a new record not working
                            DanielShanahan

                            It seems like you could add the order to a portal if you have "Allow creation of records in this table via this relationship" checked.

                             

                            To do that, you'll need to give the portal an object name (Inspector/Position/Name), then script:

                             

                            Go to Object [Portal Name]

                            Go to Portal Row Last

                            Set Field [Order::ID; $orderID]

                            etc.

                            • 11. Re: Script creating a new record not working
                              ElpicFayl

                              Yes, I have  relationships between my products, my orders and my orders list.

                              But my portal use the Typ_Pro version of my Product Table, will this script still work?

                              Thanks for the info, I'll try this tomorrow at work.

                              • 12. Re: Script creating a new record not working
                                DanielShanahan

                                ElpicFayl wrote:

                                 

                                Yes, I have  relationships between my products, my orders and my orders list.

                                But my portal use the Typ_Pro version of my Product Table, will this script still work?

                                The script segment I wrote in #10 above is for the Basket table.  Clicking on the green plus icon in Typ_Pro gathers the product info that the customer wants.  You still need to make a record in the Basket table with that info.  If you have a relationship with Customer you could add a Basket portal and go to that object, go to the last portal row, set the field, etc.

                                 

                                A couple of other suggestions:

                                1. Use full names of the base table.  For example, instead of CLI use CLIENT, instead of TYP_PRO use TYPE_pro or TYPE_program.

                                 

                                2. If you want to keep your abbreviations, add a key in the Relationship Graph using the Text Note Tool (with an "A" icon).  For example:

                                CLI = Client

                                TYP = Type

                                etc.

                                 

                                3. Move the TYP table occurrence to the right of TYP_PRO and then align the table occurrences into rows and columns

                                • 13. Re: Script creating a new record not working
                                  ElpicFayl

                                  I think I'll need time to implement and comprehend fully what you thaught.

                                  I think I also need to push my coworker to establish more readable naming rules, as it's quite hard for someone who don't know the file to comprehend everything on a first glance.

                                  I'll come back to you once I tried to implement everything.

                                  Thanks again DanielShanahan!

                                  • 14. Re: Script creating a new record not working
                                    ElpicFayl

                                    Finally I reworked my db and its relations, it works a lot better.

                                    I will add some screenshots once I am finished with this thing.