Skip to main content

Notifications

Power Apps - Building Power Apps
Suggested answer

Formula Help

Posted on by
I have a formula that is supposed to be taking the qty of the item in the shopping cart and only creating another line(s) if the qty >1, however, even if I have a qty of 1, I'm still getting two lines in my table.
Does anyone know where in this formula the problem is at?
// Initialize a variable to store the generated Request ID
Set(
    RequestID,
    Blank()
);
 
// Create the initial record to capture the Request ID
Set(
    RequestID,
    Patch(
        'Equipment Requests',
        Defaults('Equipment Requests'),
        {
            'Equipment Qty': 1,
            'Equipment Category': LookUp(
                'Equipment Categories',
                EquipCategories = First(CartItems).EquipmentCategory
            ),
            // Reference the first item in CartItems
            'Notes/Comments': First(CartItems).EquipmentNotes,
            Job: ctx_seljobitem,
            // Reference the form control directly
            'Cost Code': txt_ER_CCValue.Text,
            // Reference the form control directly
            'Date & Time Needed': DateValue(
                Text(
                    dp_ER_DateValue.SelectedDate,
                    "[$-en-US]yyyy-mm-dd"
                )
            ) + TimeValue(Text(dd_ER_HourValue.Selected.Value & ":" & dd_ER_MinValue.Selected.Value)),
            // Combine date and time
            'Anticipated End Date': dp_ER_EndDateValue.SelectedDate,
            // Reference the form control directly
            'Delivery Contact Name': txt_ER_ConNaValue.Text,
            // Reference the form control directly
            'Delivery Contact Phone #': txt_ER_ConNuValue.Text// Reference the form control directly
        }
    ).'Request ID #'
);
 
// Iterate over each item in the cart
ForAll(
    CartItems,
    // Check if the qty is greater than 1
    If(
        Value(EquipmentQty) > 1,
        // Create multiple rows for items with qty > 1
        ForAll(
            Sequence(Value(EquipmentQty)),
            Patch(
                'Equipment Requests',
                Defaults('Equipment Requests'),
                {
                    'Request ID #': RequestID,
                    'Equipment Qty': 1,
                    'Equipment Category': LookUp(
                        'Equipment Categories',
                        EquipCategories = EquipmentCategory
                    ),
                    'Notes/Comments': EquipmentNotes,
                    Job: ctx_seljobitem,
                    'Cost Code': txt_ER_CCValue.Text,
                    'Date & Time Needed': DateValue(
                        Text(
                            dp_ER_DateValue.SelectedDate,
                            "[$-en-US]yyyy-mm-dd"
                        )
                    ) + TimeValue(Text(dd_ER_HourValue.Selected.Value & ":" & dd_ER_MinValue.Selected.Value)),
                    'Anticipated End Date': dp_ER_EndDateValue.SelectedDate,
                    'Delivery Contact Name': txt_ER_ConNaValue.Text,
                    'Delivery Contact Phone #': txt_ER_ConNuValue.Text
                }
            )
        ),
        // Create a single row for items with quantity 1
        Patch(
            'Equipment Requests',
            Defaults('Equipment Requests'),
            {
                'Request ID #': RequestID,
                'Equipment Qty': 1,
                'Equipment Category': LookUp(
                    'Equipment Categories',
                    EquipCategories = EquipmentCategory
                ),
                'Notes/Comments': EquipmentNotes,
                Job: ctx_seljobitem,
                'Cost Code': txt_ER_CCValue.Text,
                'Date & Time Needed': DateValue(
                    Text(
                        dp_ER_DateValue.SelectedDate,
                        "[$-en-US]yyyy-mm-dd"
                    )
                ) + TimeValue(Text(dd_ER_HourValue.Selected.Value & ":" & dd_ER_MinValue.Selected.Value)),
                'Anticipated End Date': dp_ER_EndDateValue.SelectedDate,
                'Delivery Contact Name': txt_ER_ConNaValue.Text,
                'Delivery Contact Phone #': txt_ER_ConNuValue.Text
            }
        )
    )
);
 
// Clear the cart and forms after submission
Clear(CartItems);
UpdateContext({ctx_seljobitem: Blank()});
Reset(txt_ER_CCValue);
Reset(dp_ER_DateValue);
Reset(dd_ER_HourValue);
Reset(dd_ER_MinValue);
Reset(dp_ER_EndDateValue);
Reset(txt_ER_ConNaValue);
Reset(txt_ER_ConNuValue);
Reset(txt_ER_QTYValue);
Reset(cb_ER_EquCatValue);
Reset(txt_ER_NotesValue);
// Notify the user of successful submission
Notify(
    "Request submitted successfully",
    NotificationType.Success
);
Navigate(
    'Equipment Request Form',
    ScreenTransition.None
);
  • Suggested answer
    jrletner Profile Picture
    jrletner 46 on at
    Formula Help

    The issue seems to be in the structure of your ForAll loop. Currently, it’s iterating over every item in CartItems and creating an entry regardless of the quantity. If EquipmentQty is 1, it’s still creating a single record outside of the nested ForAll block. Let’s refine the logic to ensure that it creates one record only when EquipmentQty is 1.

    Let's attempt a different approach:

    1. Move the initial Patch statement (for quantity = 1) outside of the ForAll loop that iterates over CartItems.
       
    2. Modify the conditional within the ForAll loop so that it only creates records when EquipmentQty > 1.

    Try this code and see if that helps with the issue you're experiencing.
     
    // Initialize a variable to store the generated Request ID
    Set(
        RequestID,
        Blank()
    );
    // Create the initial record to capture the Request ID for items with qty = 1
    Set(
        RequestID,
        Patch(
            'Equipment Requests',
            Defaults('Equipment Requests'),
            {
                'Equipment Qty': 1,
                'Equipment Category': LookUp(
                    'Equipment Categories',
                    EquipCategories = First(CartItems).EquipmentCategory
                ),
                'Notes/Comments': First(CartItems).EquipmentNotes,
                Job: ctx_seljobitem,
                'Cost Code': txt_ER_CCValue.Text,
                'Date & Time Needed': DateValue(
                    Text(
                        dp_ER_DateValue.SelectedDate,
                        "[$-en-US]yyyy-mm-dd"
                    )
                ) + TimeValue(Text(dd_ER_HourValue.Selected.Value & ":" & dd_ER_MinValue.Selected.Value)),
                'Anticipated End Date': dp_ER_EndDateValue.SelectedDate,
                'Delivery Contact Name': txt_ER_ConNaValue.Text,
                'Delivery Contact Phone #': txt_ER_ConNuValue.Text
            }
        ).'Request ID #'
    );
    // Iterate over each item in the cart
    ForAll(
        CartItems,
        If(
            Value(EquipmentQty) > 1,
            // Create multiple rows for items with qty > 1
            ForAll(
                Sequence(Value(EquipmentQty)),
                Patch(
                    'Equipment Requests',
                    Defaults('Equipment Requests'),
                    {
                        'Request ID #': RequestID,
                        'Equipment Qty': 1,
                        'Equipment Category': LookUp(
                            'Equipment Categories',
                            EquipCategories = EquipmentCategory
                        ),
                        'Notes/Comments': EquipmentNotes,
                        Job: ctx_seljobitem,
                        'Cost Code': txt_ER_CCValue.Text,
                        'Date & Time Needed': DateValue(
                            Text(
                                dp_ER_DateValue.SelectedDate,
                                "[$-en-US]yyyy-mm-dd"
                            )
                        ) + TimeValue(Text(dd_ER_HourValue.Selected.Value & ":" & dd_ER_MinValue.Selected.Value)),
                        'Anticipated End Date': dp_ER_EndDateValue.SelectedDate,
                        'Delivery Contact Name': txt_ER_ConNaValue.Text,
                        'Delivery Contact Phone #': txt_ER_ConNuValue.Text
                    }
                )
            ),
            // If qty is 1, skip this since it's already added in the initial record above
            Blank()
        )
    );
    // Clear the cart and forms after submission
    Clear(CartItems);
    UpdateContext({ctx_seljobitem: Blank()});
    Reset(txt_ER_CCValue);
    Reset(dp_ER_DateValue);
    Reset(dd_ER_HourValue);
    Reset(dd_ER_MinValue);
    Reset(dp_ER_EndDateValue);
    Reset(txt_ER_ConNaValue);
    Reset(txt_ER_ConNuValue);
    Reset(txt_ER_QTYValue);
    Reset(cb_ER_EquCatValue);
    Reset(txt_ER_NotesValue);
    // Notify the user of successful submission
    Notify(
        "Request submitted successfully",
        NotificationType.Success
    );
    Navigate(
        'Equipment Request Form',
        ScreenTransition.None
    );
     
  • MichaelFP Profile Picture
    MichaelFP 1,484 on at
    Formula Help
    Hi!
     
    where did you set the variable EquipmentQty with the quantity?

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,517

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 64,037

Leaderboard