Skip to main content

Notifications

Power Apps - Power Apps Experimental Features
Suggested answer

Using multiple patch fuctions to save data differently

Posted on 2 Dec 2024 22:53:57 by 4

I have a button that validates certain requirements before saving data. When saving, I need the data fields to be saved in two different tables:

  1. First Table: This table should always update existing rows. For example, if a customer reassesses a specific product, the assessment should be updated with the latest data, ensuring only one assessment per product is saved. This functionality currently works as intended in my code.

  2. Second Table: This table should always save a new assessment, regardless of whether an assessment for that specific product already exists. However, I am encountering an error message: "Element already exists on server." I have tried various solutions, including loops and the Collect function, but the issue persists.

I would greatly appreciate any help in resolving this issue!

 
Collect(
                        All_assessments;
                        {
 ForAll(
                        [1],
                        Collect(
                            All_assessments;
 
If(
    IsBlank(Field1.Selected.Value);
    Notify("Please select a value before saving"; NotificationType.Error);
    false;;
    
    If(
        (Field1.Selected.Value = "Option1" Or Field1.Selected.Value = "Option2") And IsBlank(Field2.Text);
        Notify("For Option1 and Option2, please provide a justification in the comment section before saving"; NotificationType.Error);
        false;;
        
        If(
            IsBlank(Field3.Selected.Value) And IsBlank(Field4.Selected.Value) And IsBlank(Field5.Selected.Value) And IsBlank(Field6.Selected.Value);
            Notify("Please select at least one option before saving"; NotificationType.Error);
            false;;
            
            If(
                IsBlank(Field7.Text);
                Notify("Field7 cannot be blank before saving"; NotificationType.Error);
                false;;
                
                If(
                    IsBlank(Field8.Text) Or IsBlank(LookUp(Table1; ID_Field = Field8.Text));
                    Notify("Field8 does not exist"; NotificationType.Error);
                    false;;
                    
                    Patch(
                        Table1;
                        LookUp(Table1; ID_Field = Field8.Text);
                        {
                            Field1: Field1.Selected.Value;
                            Field3: Field3.Selected.Value;
                            Field4: Field4.Selected.Value;
                            Field5: Field5.Selected.Value;
                            Field6: Field6.Selected.Value;
                            Field2: Field2.Text;
                            Field7: Field7.Text;
                            Field9: Field9.Text
                        }
                    );;
                    
                    Patch(
                        Table2;
                        Defaults(Table2);
                        {
                            ID_Field: Field8.Text;
                            Field1: Field1.Selected.Value;
                            Field3: Field3.Selected.Value;
                            Field4: Field4.Selected.Value;
                            Field5: Field5.Selected.Value;
                            Field6: Field6.Selected.Value;
                            Field2: Field2.Text;
                            Field7: Field7.Text;
                            Field9: Field9.Text
                        }
                    );;
                    
                    If(
                        !IsEmpty(Errors(Table2));
                        Notify("Error saving to Table2: " & First(Errors(Table2)).Message; NotificationType.Error);
                        Notify("Record successfully saved"; NotificationType.Success)
                    );;
                    
                    Reset(Field8);;
                    Reset(Field1);;
                    Reset(Field3);;
                    Reset(Field4);;
                    Reset(Field5);;
                    Reset(Field6);;
                    Reset(Field2)
                )
            )
        )
    )
)
  • Suggested answer
    Artur Stepniak Profile Picture
    Artur Stepniak 226 on 03 Dec 2024 at 08:50:43
    Using multiple patch fuctions to save data differently
    Hello,
     
    unfortunately, the code has a nested ifs structure which's really hard to understand. I'd need to know the output and the context first to understand what's going on behind the scenes. Nevertheless, the code that you've provided should be optimized - condition statements in ifs are too complicated. What you'd like to achieve? Maybe instead of using separate fields for each value, you could introduce a form? That way you won't need to check if each field value is blank.
     
    In case of any other questions, let me know. If the answer helped you, mark it, so that others can benefit from it.
     
    Best regards,
     
    Artur Stepniak

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

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,297

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,890

Leaderboard

Featured topics