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 Apps
Suggested Answer

Formula Help

(0) ShareShare
ReportReport
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
);
Categories:
  • MichaelFP Profile Picture
    2,008 Moderator on at
    Hi!
     
    where did you set the variable EquipmentQty with the quantity?
  • Suggested answer
    jrletner Profile Picture
    720 Moderator on at

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

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 243 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 190 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 135

Last 30 days Overall leaderboard