web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / If condition to patch ...
Power Apps
Unanswered

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

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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?

Categories:
  • CU-18081211-6 Profile Picture
    9,272 Moderator on at

    Hi @Anonymous,

     

    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 !

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

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

  • CU-18081211-6 Profile Picture
    9,272 Moderator on at

    @Anonymous ,

    Can I see your formula ?

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    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});

  • petewalburn Profile Picture
    195 on at

    Hi gabibalaban,

     

    I've just managed to patch my collection to a SharePoint list using your method.

     

    The only issue I do have is the Person column will not update.

    This is the code I am using to update or add to a list:

    ForAll(
     _colImplementationPlan As item,
     Patch(
     'MOC BusMgmt Implementation Plan Test',
     Coalesce(LookUp('MOC BusMgmt Implementation Plan Test',MOCnumber=item.MOCnumber && Number=item.Number),
     Defaults('MOC BusMgmt Implementation Plan Test')),
     {
     MOCnumber:item.MOCnumber,
     Number:item.Number,
     //Assigned:item.Assigned,
     Status:item.Status,
     Action:item.Action,
     Deadline:item.Deadline,
     Completion:item.Completion,
     Comments:item.Comments
     }
     )
     )

    This works well, until I uncomment the 'Assigned:item.Assigned' line.

    Any ideas how I can update or edit the Assigner person?

     

    Pete

     

  • petewalburn Profile Picture
    195 on at

    I managed to do this, by changing the code to:

     

    ForAll(
     _colImplementationPlan As item,
     Patch(
     'MOC BusMgmt Implementation Plan Test',
     Coalesce(LookUp('MOC BusMgmt Implementation Plan Test',MOCnumber=item.MOCnumber && Number=item.Number),
     Defaults('MOC BusMgmt Implementation Plan Test')),
     {
     MOCnumber:item.MOCnumber,
     Number:item.Number,
     Status:item.Status,
     Action:item.Action,
     Deadline:item.Deadline,
     Completion:item.Completion,
     Comments:item.Comments
     }
     );
    
     UpdateIf('MOC BusMgmt Implementation Plan Test',MOCnumber=item.MOCnumber && Number=item.Number && Assigned.DisplayName<>item.Assigned.DisplayName,
     {
     Assigned:item.Assigned
     }
     )
     );

     Just thought I'd put the solution in case it helps anyone else.

     

    Pete

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 354 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 320

#3
Mohsin Ali Profile Picture

Mohsin Ali 246

Last 30 days Overall leaderboard