7 Replies Latest reply on Nov 16, 2016 11:17 AM by thong127

    Script is not working to my invoice layout

    thong127

      I Have the following script that I want to used to delete my materials and invoices;

      My problem is when Im in my Invoice Layout (List View) when I click the delete, it delete all my records.

       

      Set Error Capture [On]

      If (Get (Scriptparameter) = "Delete Materials"

      Perform Find [Restore] (Materials::Checkbox  ==1)

      If [Get ( LastError ) ≠ 0]

      Show All Records

      Set Error Capture [Off]

      Exit Script [Text Result: ]

      End If

      End If

      Set Error Capture [Off]

       

      Show Custom Dialog "Delete Materials ** WARNING **"

      "Deleting these materials will IRREVERSIBLY REMOVE all their related records. You should ensure that you have backup  elsewhere.

       

      If [Get ( LastMessageChoice ) = 1]

      Go to Record/Request/Page [First]

      Loop

      Delete Record/Request [With dialog:off]\

      Exit Loop if [Get ( FoundCount ) = 0]

      Go to Record/Request/Page [First]

      End Loop

      End If

      Show All Records

       

       

      If (Get (Scriptparameter) = "Delete Invoices"

      Perform Find [Restore] (Invoices::Checkbox  ==1)

      If [Get ( LastError ) ≠ 0]

      Show All Records

      Set Error Capture [Off]

      Exit Script [Text Result: ]

      End If

      End If

      Set Error Capture [Off]

       

      Show Custom Dialog "Delete Invoices ** WARNING **"

      "Deleting these Invoices will IRREVERSIBLY REMOVE all their related records. You should ensure that you have backup  elsewhere.

       

      If [Get ( LastMessageChoice ) = 1]

      Go to Record/Request/Page [First]

      Loop

      Delete Record/Request [With dialog:off]\

      Exit Loop if [Get ( FoundCount ) = 0]

      Go to Record/Request/Page [First]

      End Loop

      End If

      Show All Records

       

      Please help me fix the problem.

       

      Thank you.

        • 1. Re: Script is not working to my invoice layout
          David Moyer

          Hi,

          Show All Records is the script step that is causing you to delete all records.  You don't want that in there.

          • 2. Re: Script is not working to my invoice layout
            mikebeargie

            In your schema, is the "delete related records" checkbox turned on anywhere between invoices and materials?

            You should always be on the right context by using Go To Layout before performing delete actions

             

            Your loop script steps are in the wrong order (check for the exit condition first, then delete, and no need for Go To Record in this loop use case). Actually, you don't need the "Go To Record [First]" before your loops either.

             

            You do not need to use "set error capture off" immediately before exiting a script.

             

            Also, your "end if" for the script parameters are in the wrong spot.

            Set Error Capture [On]

            If (Get (Scriptparameter) = "Delete Materials"
               Go To Layout [ Materials ]
               Perform Find [Restore] (Materials::Checkbox ==1)

               If [Get ( LastError ) ≠ 0]

                  Show All Records

                  Set Error Capture [Off]

                  Exit Script [Text Result: ]

               End If

            End If

               Set Error Capture [Off]

             

               Show Custom Dialog "Delete Materials ** WARNING **"

            "Deleting these materials will IRREVERSIBLY REMOVE all their related records. You should ensure that you have backup elsewhere.

             

               If [Get ( LastMessageChoice ) = 1]

                  Go to Record/Request/Page [First]

                  Loop
                     Exit Loop if [Get ( FoundCount ) = 0]

                     Delete Record/Request [With dialog:off]

                     Go to Record/Request/Page [First]

                  End Loop

               End If

               Show All Records

            End If

             

            If (Get (Scriptparameter) = "Delete Invoices"
               Go To Layout [ Invoices ]
               Perform Find [Restore] (Invoices::Checkbox ==1)

               If [Get ( LastError ) ≠ 0]

                  Show All Records

                  Set Error Capture [Off]

                  Exit Script [Text Result: ]

               End If

            End If

               Set Error Capture [Off]

             

               Show Custom Dialog "Delete Invoices ** WARNING **"

            "Deleting these Invoices will IRREVERSIBLY REMOVE all their related records. You should ensure that you have backup elsewhere.

             

               If [Get ( LastMessageChoice ) = 1]

                  Go to Record/Request/Page [First]

                  Loop

                     Exit Loop if [Get ( FoundCount ) = 0]

                     Delete Record/Request [With dialog:off]

                     Go to Record/Request/Page [First]

                  End Loop

               End If

               Show All Records
            End If

            1 of 1 people found this helpful
            • 3. Re: Script is not working to my invoice layout
              mikebeargie

              show all records after the delete loop exits is fine. And "Exit Script" is already being used after "show all records" in case of a find error.

               

              The position of the "End If" that I noted does have a bearing on the show all records though in the original script.

              • 4. Re: Script is not working to my invoice layout
                erolst

                Your logic is wrong; you should document your scripts better (or at all). Also note that you don't need a loop to delete all records in the found set:

                 

                If [ Get ( Scriptparameter ) = "Delete Materials" ]

                  Set Error Capture [ On ]

                  Perform Find [ Restore ] ( Materials::Checkbox  ==1)

                  If [ not Get ( FoundCount ) ]

                    Show All Records

                    Exit Script

                  End If

                  Show Custom Dialog [ "Delete Materials ** WARNING **"

                  "Deleting these materials will IRREVERSIBLY REMOVE all their related records. You should ensure that you   have backup  elsewhere." ]

                  # [ 1 = OK, 2 = CANCEL ]

                  If [ Get ( LastMessageChoice ) = 1 ]

                    Delete All Records [ no dialog ]

                  Else

                    Exit Script

                  End If

                End If

                • 5. Re: Script is not working to my invoice layout
                  David Moyer

                  I see now.  My eyes didn't register the last error <> 0 in that form (the slashed equals sign).

                  • 6. Re: Script is not working to my invoice layout
                    mikebeargie

                    In some cases a loop delete can perform faster than delete all records (but that’s a discussion for another day).

                    • 7. Re: Script is not working to my invoice layout
                      thong127

                      Hello All,

                       

                      Thank you very for all your inputs.

                       

                      Its working now.

                       

                      regards,

                       

                      LOLA