5 Replies Latest reply on Jul 14, 2014 2:02 PM by philmodjunk

    Duplicate all child portal records related to a selected parental portal record

    tays01s

      Title

      Duplicate all child portal records related to a selected parental portal record

      Post

           Relationship: Prescription < Drugs_rx

           I have a Prescription portal button/script to select specific Prescription records. These will pull up Drugs_rx records related to that particular Prescription (Prescription::__PrescriptionID = Drug_rx::_PrescriptionID).

            

           It's worth bearing in mind that if there's already a Prescription record, when I activate a new button/script it returns a duplicate of the last Prescription. If this occurs, I'd like it to also duplicate all the Drug_rx records related to that prescription. However, if there's no record in the Prescription portal, 'new Prescription' will simply create a new blank Prescription record and no Drug_rx records.

        • 1. Re: Duplicate all child portal records related to a selected parental portal record
          philmodjunk

               Take a look at the sample script near the end of this thread: Duplicating Bill Of Materials (duplicating portal line items)

               It duplicates a parent record and all related child records.

          • 2. Re: Duplicate all child portal records related to a selected parental portal record
            tays01s

                 In the following script you mentioned, please could I ask you to

                 a) explicitly state what the 2 'set variable' lines achieve and

                 b) I can see how the loop sequentially goes through a list of child records, duplicating then hiding each one until it has duplicated the entire list once. However, when I try it, it duplicates it for the original parent record. Doesn't there need to be a line in the loop where you duplicate from the original parent record but assign the new (later) parent record fk to each of the duplicate child records?

                 #Starts from BOM based layout
                 Freeze Window
            Set variable [$RecordNumber ; Value: Get ( RecordNumber ) ]  [[is the record number that of BOM? So it would be a value of 'BOM::pk_BOMID?]]
                 Duplicate Record
                 Set Variable [$BOMID ; BOM::kp_BOMID]
            Go To Record/Request/Page [$RecordNumber]
                 If [ Not IsEmpty ( LineItems::kf_BOMID ) /* there are line items to duplicate */]
                    Go To Related Record [Show only related records; From table: lineItems; Using layout: "lineItems" (lineItems)]
                    Go To Record/Request/Page [first]
                    Loop
                       Duplicate Record
                       Set Field [ LineItems::kf_BOMID ; $BOMID]
                       Omit Record
                       Go To Record/Request/Page [first]
                       Omit Record
                       Exit loop If [ get ( FoundCount ) = 0 ]
                    End Loop
                    Go To Layout [original layout]
                    Go To Record/Request/Page [Last]
                 End IF

            • 3. Re: Duplicate all child portal records related to a selected parental portal record
              philmodjunk

                   a) If you were to run this script in the debugger or put a pause/resume step between each script step so that you could see each script step execute one step at a time, you'll find the the go to record steps put the focus back on the correct record needed in order for subsequent script steps to interact with the correct record. Duplicate Record creates a new record at the bottom of an unsorted found set, so go to record is needed in order to get back to the original copy of the record.

                   Go to record/request/page last is used to move the focus to the new parent record created by this script instead of leaving the focus on the original copy used to produce it.

                   b) Set Field [ LineItems::kf_BOMID ; $BOMID] Does exactly that. It links the newly created child record to the new Parent record by setting the kf field of that record to the ID of the new Parent record..

              • 4. Re: Duplicate all child portal records related to a selected parental portal record
                tays01s

                     I think it's now working OK.

                • 5. Re: Duplicate all child portal records related to a selected parental portal record
                  philmodjunk

                       One thing that crops up is at the step 'Go to record' a dialogue pops-up asking what record I want. Can this be avoid, ie. it all the automated.

                       There is a check box at the bottom of the script editor that appears when the Go to record step is highlignted. It controls whether or not a confirmation dialog appears.