cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
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
Highlighted
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