cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper I
Helper I

Patch to existing record on sharepoint list based on person

Hello all,

 

I've a sharepoint list as the following:

Miguel_MESP_0-1605890208715.png

with the following types:

Miguel_MESP_1-1605890264150.png

 

 

I'm creating an app where users will populate our list at the first time they logged in. They will have to fill the following fields and save:

Miguel_MESP_2-1605890371664.png

 

My issue is at the Save button. I want to patch as a new item at the first time, but then if the user make some changes I want to update the item on sharepoint list. In order to do that I need to do the lookup based on the Name Column on Sharepoint because 

it represents the user and the user().FullName or User().Email will never change.

 

When I try to apply LookUp(Users, Name = User().FullName) on patch function it doesn't work.

 

Someone can help me with this, and also with the if function that I need to create based on the Name to see if it is a new record or an update?

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

@Miguel_MESP ,

Try

LookUp(
   Users, 
   Name.DisplayName = User().FullName
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

7 REPLIES 7
Highlighted
Memorable Member
Memorable Member

Hi @Miguel_MESP ,

Can you try to use a context variable:

UpdateContext({currentUser:User().FullName})

and use this variable inside LookUp formula.

 

Hope it helps ! 

 

Highlighted

Sorry, I'm a newbie on this, I didn't understand your suggestion.

 

The idea is to use update context on lookup how?

Patch(Users,LookUp(Users, UpdateContext({Name: User().FullName})

 

I was trying something like this but I suppose syntax doesn't make sense

Highlighted
Frequent Visitor

I think I had a similar need to compare what was being entered to what was already in the list and found it in these very forums. The link below may help but you'll definitely have to do some tweaking to the code like I did for your needs. I hope this helps.

 

https://powerusers.microsoft.com/t5/Building-Power-Apps/Prevent-Duplicate-Records-from-being-submitt... 

Highlighted

@Miguel_MESP 
The UpdateContext() function is a way of setting a non-global variable (within the scope of the screen)

In the example by @gabibalaban the variable named currentUser was set to the value of what User().FullName returns, which should be a string (text) value which is the Full Name. My guess is that this function is called as the result the screen control that sets the full name or retrieves it from that control, setting the user's full name in currentUser.

 

I believe that you would then try your Patch() multi-function call, with currentUser within the LookUp() function as:

Patch(Users, LookUp(Users, Name = currentUser), {column1: column1value), {column2: column2value}, ...)

for the control that causes the update/modify save operation.

Highlighted

It stills gives me an error. The "=" on Name = CurrentUser is invalid argument. Probably because my Name variable is a SharePoint person or group type of column?

Highlighted

@Miguel_MESP ,

Try

LookUp(
   Users, 
   Name.DisplayName = User().FullName
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

Highlighted
Super User
Super User

I typically set at the Apps OnStart a variable that looks at current user : Set(_CurrentUser, User())

 

Then you have several directions you could go. One option would be to have a variable set that decides if it is new or not like : Set(_Type, If(CountIf(DataSource, 'Created By'.DisplayName=_CurrentUser.FullName)>0, "Edit","New"))

 

Then for your Display Mode of the Form it could be DisplayMode._Type

 

It is basically saying to count how many items you have that were created by the current user and if it is more than 0 make my form new and if not make it edit. 

 

The other piece to that will be you setting the Items of the Form to the one that was created by the current user. 

 

Something like: Items = LookUp(DataSource, 'Created By'.DisplayName=_CurrentUser.FullName)

 

Hope this helps!

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

News & Announcements

Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Community Highlights

Community Highlights

Check out the Power Platform Community Highlights

Top Solution Authors
Top Kudoed Authors
Users online (7,284)