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

If condition to patch to create a new item or edit existing

Hello,

 

I've an app where users submit some info about their profile and save.
I need to place on save button an if condition to differentiate the patch command, because first-time people will save their profile the patch will create a new item on a sharepoint list, but then if the user wants to edit some field and save again the patch will update.

 

Patch to create a new item:

 

Patch(Users, Defaults(Users),
{ Name:
{
'@odata.type':"#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
Claims:"i:0#.f|membership|" & Lower(User().Email),
Department:"",
DisplayName:User().FullName,
Email:User().Email,
JobTitle:".",
Picture:User().Image
},
InternalNumber:Value(txtNInterno.Text),
PhoneNumber:Value(txtContacto.Text),
CostCenter:Value(txtCC.Text),
Company:drpEmpresa.SelectedText,
Manager:cmbChefia});

UpdateContext({varViewMode:true})

 

Patch to edit existing item based on if the user already exists on the list:

 

Patch(Users, LookUp(Users,Name.DisplayName = User().FullName), {
InternalNumber:Value(txtNInterno.Text),
PhoneNumber:Value(txtContacto.Text),
CostCenter:Value(txtCC.Text),
Company:drpEmpresa.SelectedText,
Manager:cmbChefia})

 

___

 

I've tried this on two different buttons and it worked, now I Need to join both on save button with the following logic:

 

If the User().Fullname already exists on sharepoint list, field name perform patch to create, otherwise patch to edit.

 

Someone can help me?

4 REPLIES 4
Super User II
Super User II

Hi @Miguel_MESP,

 

Try to adapt this to your needs:

Patch( my_data_source,

            Coalesce (

                             LookUp( my_data_source, condition),

                             Defaults( my_data_source)

                             ),

           {….values to create/update ….} 

         )

The coalesce part because there is our problem checks if there are any records and if found one use it else use Defaults() function.

 

Hope it helps !

it's not working, it doesn't perform any action

@Miguel_MESP ,

Can I see your formula ?

Sure! Thanks for the help!

 

Patch(Users, Coalesce(LookUp(Users,Name.DisplayName = User().FullName), Defaults(Users)),
{ Name:
{
'@odata.type':"#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
Claims:"i:0#.f|membership|" & Lower(User().Email),
Department:"",
DisplayName:User().FullName,
Email:User().Email,
JobTitle:".",
Picture:User().Image
},
InternalNumber:Value(txtNInterno.Text),
PhoneNumber:Value(txtContacto.Text),
CostCenter:Value(txtCC.Text),
Company:drpEmpresa.SelectedText,
Manager:cmbChefia});

Helpful resources

Announcements
News & Announcements

Community Blog

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

Power Apps Community Call

Power Apps Community Call- January

Mark your calendars and join us for the next Power Apps Community Call on January 20th, 8a PST

PP Bootcamp Carousel

Global Power Platform Bootcamp

Dive into the Power Platform stack with hands-on sessions and labs, virtually delivered to you by experts and community leaders.

secondImage

Power Platform Community Conference On Demand

Watch Nick Doelman's session from the 2020 Power Platform Community Conference on demand!

Top Solution Authors
Top Kudoed Authors
Users online (11,551)