cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
hjaf
Level: Powered On

patching (Adding) items to a multi item lookup column when main item is known

I have seen a few examples, but they only patch the lookup column as a whole. I have a multi item lookup-column that I want to add and remove items from.

I am able to update single items with this code. patch is triggered from a gallery (hence the ThisItem-references) and the screens purpose is to edit the item set in the context as ("CurrentScope")

Patch(
    OFP_SCOPES, 
    LookUp(OFP_SCOPES, ID = CurrentScope.ID),
    {
        LINESECTIONS: Table({
            '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
            Id: ThisItem.ID,
            Value: ThisItem.LINESECTION
        })
    }
)

I guess I would need a second patch for that column to achieve this. But how can I use defaults() on the content of a lookup column? 

The idea i'm working on would be something like this:

Patch(
    OFP_SCOPES, 
    LookUp(OFP_SCOPES, ID = CurrentScope.ID),
    {
        LINESECTIONS: Patch(?(the current item and column)?,Defaults(?Thecurrent library and column?), Table({
/*LINESECTIONS: Patch(CurrentScope.LINESECTIONS, Defaults(CurrentScope.LINESECTIONS), Table({*/ '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference", Id: ThisItem.ID, Value: ThisItem.LINESECTION })) } )

I am probably way into the woods at this point, any suggestions would be greatly appreciated Smiley Very Happy 

 

2 REPLIES 2
hjaf
Level: Powered On

Re: patching (Adding) items to a multi item lookup column when main item is known

I think i found a way, not sure if this is anything close to best practice but here goes:
To append item: 

ClearCollect(
    CurrentScopeLineSections,
    LookUp(
        OFP_SCOPES,
        ID = CurrentScope.ID
    ).LINESECTIONS,
    Table(
        {
            '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
            Id: ThisItem.ID,
            Value: ThisItem.LINESECTION
        }
    )
);
Patch(
    OFP_SCOPES,
    LookUp(
        OFP_SCOPES,
        ID = CurrentScope.ID
    ),
    {LINESECTIONS: CurrentScopeLineSections}
)

To remove an item: 

Patch(
    OFP_SCOPES,
    LookUp(
        OFP_SCOPES,
        ID = CurrentScope.ID
    ),
    {
        LINESECTIONS: Filter(
            LookUp(
                OFP_SCOPES,
                ID = CurrentScope.ID
            ).LINESECTIONS,
            ThisItem.ID <> Id
        )
    }
)

I wanted to avoid creating a collection, but I can't seem to figure out how to add a row to a table any other way. Improvement suggestions are welcome Smiley Happy

seadude
Level 10

Re: patching (Adding) items to a multi item lookup column when main item is known

Hi @hjaf ,

I'm often grappling with these types of things too. Not to put more work on you, but a suggestion when you're looking for help:

  • If you can generalize your code a bit more so its easier to read, that is cool.

Example:

ClearCollect(
    colMyCollection,
    LookUp(
        sharepointList,
        ID = TextBox.ID
    ).Level2,
    Table(
        {
            '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
            Id: ThisItem.ID,
            Value: ThisItem.LINESECTION
        }
    )
)

etc.

I look forward to working with you on the forums!!

Peace

Helpful resources

Announcements
firstImage

PowerApps Monthly Community Call!

Join us next Wednesday for our Demo Extravaganza, October 16, 2019 8am PDT.

firstImage

Microsoft Business Applications Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

SecondImage

Power Platform Summit North America

Register by September 5 to save $200

Top Kudoed Authors
Users Online
Currently online: 360 members 4,389 guests
Please welcome our newest community members: