cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
CharlesS
Kudo Kingpin
Kudo Kingpin

Power Fx Command Bar - Cloning a child record and relating the clone to the parent

Hello!

There are a few blog posts that show for Power Fx can be used in the command bar editor (preview) to create a new record. Using the Patch function with Self.Selected.Item, it is easy enough to create a copy of a record. However, I can't seem to get the Patch function to update the value of a lookup field. 

 

To be more precise: the record I am trying to copy is a child record, in a N:1 relationship with a parent record. The child record is shown in a subgrid on a form for the parent record. As such, the child record has a lookup column that relates it to the parent record. I can easily use the Patch function with a custom button on the subgrid to create a copy of the child record... for every field except that lookup field. 

 

Here is an excerpt of the the formula I am using (all tables are custom tables, no Dynamics in this environment). For simplicity's sake, I have included only two columns, a text column and a lookup column. In my scenario, Many Quotes relate to One RFQ. Quote # is a text column (primary name), and 'RFQ ID' is a Lookup column.

Patch(Quotes, Defaults(Quotes),{'Quote #':Self.Selected.Item.Autonumber & Self.Selected.Item.QuoteVersion + 1, 'RFQ ID':Self.Selected.Item.'RFQ ID'})

 

The button shows up as it should:

CharlesS_0-1637635270450.png

 

The form of the original record (the one I tried to copy) shows the relationship to the parent record, Test Project (this is why it shows up on the subgrid):

CharlesS_1-1637635495566.png

 

When I click on the subgrid command button, it does indeed create a new record, assigning the proper name. However, it does not relate it to the parent record (therefore does not show up on the subgrid in the parent record's form):

CharlesS_2-1637635621797.png

 

I also tried to use the Relate() function, but I can't seem to get that to work either. Any ideas? Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PHalseyKMS
Regular Visitor

I know it's been a while since you posted this question but I thought I would reply as I just hit the same issue and found a work around/solution.

 

I noticed that in the Self.Selected.Item object all the lookup attributes aren't load correctly and so can't be used during the patch. 

The solution I found was to use the With and LookUp commands to get the full copy of the record you are cloning and then use this in the Patch.  Below is a quick example of how you would lay it out.

With(
{
recordToClone: LookUp(datasource, Key = Self.Selected.Item.Key)
},
Patch(
datasource,
Defaults(datasource),
{
Parent: recordToClone.Parent
}
)
);

 

Phil

KMS Software

www.kms-software.com 

View solution in original post

1 REPLY 1
PHalseyKMS
Regular Visitor

I know it's been a while since you posted this question but I thought I would reply as I just hit the same issue and found a work around/solution.

 

I noticed that in the Self.Selected.Item object all the lookup attributes aren't load correctly and so can't be used during the patch. 

The solution I found was to use the With and LookUp commands to get the full copy of the record you are cloning and then use this in the Patch.  Below is a quick example of how you would lay it out.

With(
{
recordToClone: LookUp(datasource, Key = Self.Selected.Item.Key)
},
Patch(
datasource,
Defaults(datasource),
{
Parent: recordToClone.Parent
}
)
);

 

Phil

KMS Software

www.kms-software.com 

Helpful resources

Top Solution Authors
Top Kudoed Authors
Users online (4,081)