Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Unanswered

Patch if record exists or else create new entry in SharePoint list

(0) ShareShare
ReportReport
Posted on by 559

Hi all,

 

I have a gallery which needs to Patch values to different SharePoint list when data s submitted. One list is working fine for the other list when only the Column "Status" value is "Completed" it needs to patch into the list.  When completed if that value exists it must update or else create new entry in SharePoint. 

My Patch code on Submit is:

ForAll(
    Gallery2_1.AllItems As Deployment,
If(
    !Is Blank(ComboBox3_14.Selected. Value) && ComboBox3_14.Selected. Value = "Completed",
    Patch(
        Deployment List,
        Lookup(
            Deployment_List,
            Account Name = ComboBox2_3.Selected.'Name (Title)' && 'Deployment ID' = Value(IDDeploymentLbl.Text)
        ),
        {
             Use Case Name: TextInput4_1.Text,
            Use Case Type: ComboBox3_Selected. Value,
            'Details': TextInput4_4.Text,
            Start Date: DatePicker1_2.Selected Date,
            End Date: DatePicker1_3.SelectedDate,
            Belt Members (Technical SPoCs): ComboBox3_6.SelectedItems,
            'Primary Beneficiary': ComboBox3_5.Selected.Value,
            'Benefit Impact Category': ComboBox3_7.Selected.Value,
            'Impacted processes': TextInput4_5.Text,
            Status: ComboBox3_14.Selected.Value
        }
    ),
        Patch(
           Deployment_List,
            Defaults(Deployment_List),
            {
                'Project Name': ComboBox3_15.Selected.Value,
                AccountName: ComboBox2_3.Selected.'Name (Title)',
               
Use Case Name: TextInput4_1.Text,
            Use Case Type: ComboBox3_Selected. Value,
            'Details': TextInput4_4.Text,
            Start Date: DatePicker1_2.Selected Date,
            End Date: DatePicker1_3.SelectedDate,
            Belt Members (Technical SPoCs): ComboBox3_6.SelectedItems,
            'Primary Beneficiary': ComboBox3_5.Selected.Value,
            'Benefit Impact Category': ComboBox3_7.Selected.Value,
            'Impacted processes': TextInput4_5.Text,
            Status: ComboBox3_14.Selected.Value,
                'Deployment ID': Last(GenAIDeployment_List).'Deployment ID' + 1
            }
        )
    )
);
 
But it is not creating and updating Properly. If I given For All is us creating Duplicate entries if not for all it is saving to SharePoint. From this gallery 2_1 I have to patch values to different SharePoint list.
Any help/suggestion on this.
Categories:
  • WarrenBelz Profile Picture
    146,702 Most Valuable Professional on at
    Re: Patch if record exists or else create new entry in SharePoint list

    @Uthhra ,

    Please go back to the label exercise I gave you - it should show true for the records to be updated and false for those needing a new record. If it does not, there is something wrong with the lookup logic.

  • Sri Profile Picture
    559 on at
    Re: Patch if record exists or else create new entry in SharePoint list

    @WarrenBelz 

    ForAll(
        Gallery2_1.AllItems As Deployment,
        If(
            !IsBlank(Deployment.ComboBox3_14.Selected.Value) && Deployment.ComboBox3_14.Selected.Value = "Completed",
            Patch(
                GenAIDeployment_List,
                LookUp(
                    GenAIDeployment_List,
                    'Account Name' = ComboBox2_3.Selected.'Name (Title)' && 'Deployment ID' = Value(DeploymentId.Text)
                ),
                {
                    'Project Name': Deployment.ComboBox3_15.Selected.Value,
                    'Account Name': ComboBox2_3.Selected.'Name (Title)',
                    'GenAI UseCase Name': Deployment.TextInput4_1.Text,
                    'GenAI UseCase Type': Deployment.ComboBox3_4.Selected.Value,
                    'Details of Pilot': Deployment.TextInput4_4.Text,
                    'Pilot Start Date': Deployment.DatePicker1_2.SelectedDate,
                    'Pilot End Date': Deployment.DatePicker1_3.SelectedDate,
                    'AI Black Belt Members (Technical SPoCs)': Deployment.ComboBox3_6.SelectedItems,
                    'Primary Beneficiary': Deployment.ComboBox3_5.Selected.Value,
                    'Benefit Impact Category': Deployment.ComboBox3_7.Selected.Value,
                    'Impacted processes': Deployment.TextInput4_5.Text,
                    'Pilot Status': Deployment.ComboBox3_14.Selected.Value,
                    'Scoped for Adoption': Deployment.ComboBox3_8.Selected.Value,
                    'KPIs/Metrics': Deployment.TextInput4_15.Text
                    //'Deployment ID': Last(GenAIDeployment_List).'Deployment ID' + 1
                }
            ),
            //!IsBlank(Deployment.ComboBox3_14.Selected.Value) && Deployment.ComboBox3_14.Selected.Value = "Completed",
            Patch(
                GenAIDeployment_List,
                Defaults(GenAIDeployment_List),
                {
                    'Project Name': Deployment.ComboBox3_15.Selected.Value,
                    'Account Name': ComboBox2_3.Selected.'Name (Title)',
                    'GenAI UseCase Name': Deployment.TextInput4_1.Text,
                    'GenAI UseCase Type': Deployment.ComboBox3_4.Selected.Value,
                    'Details of Pilot': Deployment.TextInput4_4.Text,
                    'Pilot Start Date': Deployment.DatePicker1_2.SelectedDate,
                    'Pilot End Date': Deployment.DatePicker1_3.SelectedDate,
                    'AI Black Belt Members (Technical SPoCs)': Deployment.ComboBox3_6.SelectedItems,
                    'Primary Beneficiary': Deployment.ComboBox3_5.Selected.Value,
                    'Benefit Impact Category': Deployment.ComboBox3_7.Selected.Value,
                    'Impacted processes': Deployment.TextInput4_5.Text,
                    'Pilot Status': Deployment.ComboBox3_14.Selected.Value,
                    'Scoped for Adoption': Deployment.ComboBox3_8.Selected.Value,
                    'KPIs/Metrics': Deployment.TextInput4_15.Text,
                    'Deployment ID': Last(GenAIDeployment_List).'Deployment ID' + 1
                }
            )
        )
    )
    I again changed few things in my code. But only the ones whose value is not Completed is saving and the Completed ones are not saving.
    I couldn't point out where it is going wrong?
  • WarrenBelz Profile Picture
    146,702 Most Valuable Professional on at
    Re: Patch if record exists or else create new entry in SharePoint list

    @Uthhra ,

    Your post suggests that true updates and false creates a new record. I was just ensuring the records in the gallery produced the required results.

  • Sri Profile Picture
    559 on at
    Re: Patch if record exists or else create new entry in SharePoint list

    @WarrenBelz 

    If this condition is true only it must lookup and update or else again if the condition is true only it must create new one. In both cases the condition must be true right and then only update or create new should work?

  • WarrenBelz Profile Picture
    146,702 Most Valuable Professional on at
    Re: Patch if record exists or else create new entry in SharePoint list

    @Uthhra ,

    You need to do some debugging of your LookUp logic - if you put a label in the gallery with this

    !IsBlank(ComboBox3_14.Selected.Value) && ComboBox3_14.Selected.Value = "Completed",

    you should see true returned on the matching records and false on the others. The true items will update and the false ones create new records.

     

  • Sri Profile Picture
    559 on at
    Re: Patch if record exists or else create new entry in SharePoint list

    @WarrenBelz 

    Only if I give the second Patch separately it is saving to SharePoint list. If I give with lookup nothing is getting saved to SharePoint

  • WarrenBelz Profile Picture
    146,702 Most Valuable Professional on at
    Re: Patch if record exists or else create new entry in SharePoint list

    @Uthhra ,

    To save a lot of time here, I will go back to your code and add the relevant value - if any of these controls are not in the Gallery, delete Deployment. from the reference

    ForAll(
     Gallery2_1.AllItems As Deployment,
     If(
     !IsBlank(Deployment.ComboBox3_14.Selected.Value) && Deployment.ComboBox3_14.Selected.Value = "Completed",
     Patch(
     Deployment List,
     Lookup(
     Deployment_List,
     AccountName = Deployment.ComboBox2_3.Selected.'Name (Title)' && 
     'Deployment ID' = Value(Deployment.IDDeploymentLbl.Text)
     ),
     {
     'Use Case Name': Deployment.TextInput4_1.Text,
     'Use Case Type': Deployment.ComboBox3_Selected. Value,
     'Details': Deployment.TextInput4_4.Text,
     'Start Date': Deployment.DatePicker1_2.Selected Date,
     'End Date': Deployment.DatePicker1_3.SelectedDate,
     'Belt Members (Technical SPoCs)': Deployment.ComboBox3_6.SelectedItems,
     'Primary Beneficiary': Deployment.ComboBox3_5.Selected.Value,
     'Benefit Impact Category': Deployment.ComboBox3_7.Selected.Value,
     'Impacted processes': Deployment.TextInput4_5.Text,
     Status: Deployment.ComboBox3_14.Selected.Value
     }
     ),
     Patch(
     Deployment_List,
     Defaults(Deployment_List),
     {
     'Project Name': Deployment.ComboBox3_15.Selected.Value,
     AccountName: Deployment.ComboBox2_3.Selected.'Name (Title)',
     'Use Case Name': Deployment.TextInput4_1.Text,
     'Use Case Type': Deployment.ComboBox3_Selected. Value,
     'Details': Deployment.TextInput4_4.Text,
     'Start Date': Deployment.DatePicker1_2.Selected Date,
     'End Date': Deployment.DatePicker1_3.SelectedDate,
     'Belt Members (Technical SPoCs)': Deployment.ComboBox3_6.SelectedItems,
     'Primary Beneficiary': Deployment.ComboBox3_5.Selected.Value,
     'Benefit Impact Category': Deployment.ComboBox3_7.Selected.Value,
     'Impacted processes': Deployment.TextInput4_5.Text,
     Status: Deployment.ComboBox3_14.Selected.Value,
     'Deployment ID': Last(GenAIDeployment_List).'Deployment ID' + 1
     }
     )
     )
    );

     

    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.

    MVP (Business Applications)   Visit my blog Practical Power Apps

  • Sri Profile Picture
    559 on at
    Re: Patch if record exists or else create new entry in SharePoint list

    @WarrenBelz 

    I am getting an error. Since in Deployment list I have created all the fields as Single line text and with function I am getting this record error.

  • Sri Profile Picture
    559 on at
    Re: Patch if record exists or else create new entry in SharePoint list

    @AndrewR1 

    For already existing record it is not updating but creating a duplicate record. If two rows are there whose status value is completed, it is updating/creating only 1 record rather than 2.

  • WarrenBelz Profile Picture
    146,702 Most Valuable Professional on at
    Re: Patch if record exists or else create new entry in SharePoint list

    Hi @Uthhra ,

    The fundamental issue I can see is that you have used an As statement to refer to the gallery records and then not used it in the Patch - so you will simply always patch the first record. The below addresses a few other things and I also do not know which controls are in the gallery

    With(
     {
     _Update: 
     !IsBlank(ComboBox3_14.Selected.Value) && 
     ComboBox3_14.Selected.Value = "Completed",
     _LastID: Last(GenAIDeployment_List).'Deployment ID' + 1
     },
     Patch(
     GenAIDeployment_List,
     ForAll(
     Gallery2_1.AllItems As Deployment,
     With(
     {
     _ID:
     LookUp(
     GenAIDeployment_List,
     AccountName = Deployment.ComboBox2_3.Selected.'Name (Title)' && 
     'Deployment ID' = Deployment.Value(IDDeploymentLbl.Text)
     )
     },
     If(
     _Update,
     {ID: _ID}
     ),
     {
     'Project Name': Deployment.ComboBox3_15.Selected.Value,
     AccountName: Deployment.ComboBox2_3.Selected.'Name (Title)',
     'GenAI UseCase Name': Deployment.TextInput4_1.Text,
     'GenAI UseCase Type': Deployment.ComboBox3_4.Selected.Value,
     'Details of Pilot': Deployment.TextInput4_4.Text,
     'Pilot Start Date': Deployment.DatePicker1_2.SelectedDate,
     'Pilot End Date': Deployment.DatePicker1_3.SelectedDate,
     'AI Black Belt Members (Technical SPoCs)': Deployment.ComboBox3_6.SelectedItems,
     'Primary Beneficiary': Deployment.ComboBox3_5.Selected.Value,
     'Benefit Impact Category': Deployment.ComboBox3_7.Selected.Value,
     'Impacted processes': Deployment.TextInput4_5.Text,
     'Pilot Status': Deployment.ComboBox3_14.Selected.Value,
     'Deployment ID': _LastID
     }
     )
     )
     )
    )

     

    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.

    MVP (Business Applications)   Visit my blog Practical Power Apps

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,702 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,015 Most Valuable Professional

Leaderboard