cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Mo_Islam
Helper III
Helper III

Editing multiple Rows with same 'Title' using Patch/Collect

Hi,

I have an app which uses the SharePoint Calendar/List as the data source. This app is used for timetabling which allows to add courses for a teacher is a recurring event, but creates a new row per item (i.e. it’s own ID).

For the below Screen called ‘Add Event’, I have the following set of codes for the OnVisible property:

 

 

ClearCollect( timeEntriesCollection,
{
    LessonNo: 1,
    Attendees: "",
    Course: "",
    Category: "",
    Location: "",
    'Start Time': Now(),
    'End Time': Now(),
    Teacher: ""
})

 

 

 

Here is what the screen looks like:

Mo_Islam_0-1597520339608.png

 

The PLUS icon has the following code:

 

 

Select(Parent);
Patch(
    timeEntriesCollection,
    ThisItem,
    {
        LessonNo: Value(lblLessonNo.Text),
        'Start Time': StartDatePicker.SelectedDate + Time(Value(ddStartHour.Selected.Value), Value(ddStartMinutes.Selected.Value),0),
        'End Time': EndDatePicker.SelectedDate + Time(Value(ddEndHour.Selected.Value), Value(ddEndMinutes.Selected.Value),0)
    }
);

Collect(
    timeEntriesCollection,
    {
        LessonNo: lblLessonNo + 1,
        'Start Time': DateAdd(StartDatePicker.SelectedDate,7,Days) + Time(Value(ddStartHour.Selected.Value), Value(ddStartMinutes.Selected.Value),0),
        'End Time':DateAdd(EndDatePicker.SelectedDate,7,Days) + Time(Value(ddEndHour.Selected.Value), Value(ddEndMinutes.Selected.Value),0)
    }
)

 

 

 

The SAVE icon has this code:

 

 

ForAll(
    galleryClassAdd.AllItems,
    Patch(
        Timetable Test',
        Defaults(‘Timetable Test'),
        {
            Course: toptxtCourse.Text,
            Location: toptxtLocation.Text,
            'Start Time': If(Checkbox1.Value=false, StartDatePicker.SelectedDate + Time(Value(ddStartHour.Selected.Value), Value(ddStartMinutes.Selected.Value),0), StartDatePicker.SelectedDate + Time(0,0,0)),
            'End Time': If(Checkbox1.Value=false, EndDatePicker.SelectedDate + Time(Value(ddEndHour.Selected.Value), Value(ddEndMinutes.Selected.Value),0), EndDatePicker.SelectedDate + Time(23,59,59)),
            Attendees: TeacherSelector.Selected,
            Category: CategorySelector.Selected,
            LessonNo: If(Not(CategorySelector.Selected.Value="Weekend"),"Session - " & lblLessonNo.Text),
            Teacher: First(Split(TeacherSelector.Selected.Email,"@")).Result
        }
    )
)

 

 

 

PROBLEM

I now need a way to be able to edit the events in a similar way as adding by loading all related events under the same COURSE (title) name.

 

I have tried this with a gallery - But cannot get it to function:

Mo_Islam_1-1597520339612.png

 

Gallery Items:

 

 

Distinct(
    SortByColumns(
        Filter(
            'Timetable Test',
            Category.Value = Dropdown1.Selected.Value
        ),
        "EventDate",
        Descending
    ),
    Course
)

 

 

And for the chevron icon (>), on select code is:

 

 

Select(Parent);
Set(
    selectedEvent,
    ThisItem
);
ClearCollect(
    selectedTimeEntries,
    Filter(
        'Timetable Test',
        Course = selectedEvent.Result
    )
);
Navigate(EditEvent)

 

 

 

The highlighted gives me an error saying:

Mo_Islam_2-1597520339617.png

I want this now to take me to the EDIT Screen which should look very much like the Add screen but list all the events stored for the selected Course. But I do not know how to take it forward from here.

 

On the EDIT SCREEN, I have the gallery items set to selectedTimeEntries

 

The ADD icon as below but with errors:

Mo_Islam_4-1597520980776.png

 

 

If someone could please help or guide me how to load items to edit which has the same COURSE name.

 

I've tried to follow April Dunnam's amazing app but fail on the edit screen (https://youtu.be/A8SiNTnQw0Q). 

 

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @Mo_Islam ,

Do you mean that the "Save" formula could not work for updating existing records?

 

Please consider modify your formula as below:

ForAll(
    RenameColumns(galleryClassAdd_1.AllItems, "ID", "ID1"),  // modify formula here
    If(
       IsBlank(ID1),
       Patch(
            'Timetable Test',
            Defaults('Timetable Test'),
           {
             Title: toptxtCourse_1.Text,
            Location: toptxtLocation_1.Text,
            'Start Time': If(Checkbox1_1.Value=false, StartDatePicker_1.SelectedDate + Time(Value(ddStartHour_1.Selected.Value), Value(ddStartMinutes_1.Selected.Value),0), StartDatePicker_1.SelectedDate + Time(23,59,59)),
            'End Time': If(Checkbox1_1.Value=false,EndDatePicker_1.SelectedDate + Time(Value(ddEndHour_1.Selected.Value), Value(ddEndMinutes_1.Selected.Value),0), EndDatePicker_1.SelectedDate + Time(23,59,59)),
            ParticipantsPicker: TeacherSelector_1.Selected,
            Category: CategorySelector_1.Selected,
            LessonNo: If(Not(CategorySelector_1.Selected.Value="Weekend"), txtLessonNo.Text),
            Teacher: First(Split(TeacherSelector_1.Selected.Email,"@")).Result
           }
       ),
       Patch(
             'Timetable Test',
              LookUp('Timetable Test', ID = ID1), // Modify formula here. Find the specific record you want to update
            {
              Title: toptxtCourse_1.Text,
              Location: toptxtLocation_1.Text,
              'Start Time': If(Checkbox1_1.Value=false, StartDatePicker_1.SelectedDate + Time(Value(ddStartHour_1.Selected.Value), Value(ddStartMinutes_1.Selected.Value),0), StartDatePicker_1.SelectedDate + Time(23,59,59)),
              'End Time': If(Checkbox1_1.Value=false, EndDatePicker_1.SelectedDate + Time(Value(ddEndHour_1.Selected.Value), Value(ddEndMinutes_1.Selected.Value),0), EndDatePicker_1.SelectedDate + Time(23,59,59)),
              ParticipantsPicker: TeacherSelector_1.Selected,
              Category: CategorySelector_1.Selected,
              LessonNo: If(Not(CategorySelector_1.Selected.Value="Weekend"), txtLessonNo.Text),
              Teacher: First(Split(TeacherSelector_1.Selected.Email,"@")).Result
            }
        )
    )
);
Refresh('Timetable Test');
Set(successMessage,"Event edited successfully");Navigate(SuccessScreen)

 

Please try the above formula, then check if the issue is solved.

 

Regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

8 REPLIES 8
GarethPrisk
Super User II
Super User II

Nicely written code, and the app is looking great!

This is basically a conflict of data types issue, in that the collection is being established (or was established) as X, and then you're trying to insert Y into it. The error is basically indicating there's a mismatched column, or multiple, and you can't do the second collect (from the DB) into your app-created collection.

You have a few options:

  1. Revise your collect from the DB, to map columns to your app-created collection
  2. Create the collection for your app, from the DB, first, then do the app-created collections into it
  3. Examine the source DB columns and ensure your app-created collection mirrors the data types and expected column structure

 

For example, using #2:

Establish the collection in your app's OnStart (probably as a new collection name)

 

ClearCollect(
    colSpoTable, // Your collection name
    Filter(
        SpoList, // Your SPO table name
        1 = 2 // To return 0 records, but get table structure
    )
);​

 

Then update your second collect to now try to insert records into that new collection. You will see the errors immediately for any mismatched columns. Columns which are not in the source, such as my 'whatever' example below will be appended.

 

Collect(
    colSpoTable,
    {
        whatever: "whatever",
        ID: 1
    }
);

 

Fix any mismatches, and you should be good to go. This can help resolve issues any time you're working with collections that are a mixture of source and app-only data.

 

In your last screenshot, there also appears to be a discrepancy between how you're handling the date fields. In the Patch, it's a literal addition, and in the Collect it's a DateAdd (and missing a unit parameter). Doubt this is a big issue, but might be causing the fields to result in different data types (numeric vs. date, for example). I recommend using With or local variables, to help contextualize and streamline these types of operations. It will allow you to explicitly re-use exact pieces of code, makes it easier to make an update to help all usage of the code, and is easier to read. Example:

With(
    {
        varDate: DateAdd(
            DatePicker1.SelectedDate,
            Dropdown1.Selected.Value,
            Days
        ),
        varRecordID: 1
    },
    Patch(
        colSpoTable,
        {
            ID: varRecordID,
            Created: varDate
        }
    );
    Collect(
        colSpoTable,
        {
            ID: varRecordID + 1,
            Created: varDate
        }
    )
);

Please let me know if these suggestions help to unblock you. I'll be happy to assist further.

v-xida-msft
Community Support
Community Support

Hi @Mo_Islam ,

Regarding the error that you mentioned, it seems to tell that there is something wrong with your Collect formula.

 

From the formula that you provided, the 

selectedTimeEntries

collection stores records from your 'Timetable Test' List data source, actually, I think it is not necessary to add a Collect function under your Patch(selectedTimeEntries, ThisItem, {...}) formula to add a new entry into the selectedTimeEntries collection. The selectedTimeEntries collection is used to store the related events under the same COURSE (title) name, you should not add new entry to it.

Please consider remove the Collect formula under your Patch(selectedTimeEntries, ThisItem, {...}) formula, and then check if the issue is solved.

 

If you still want to add a new entry into the selectedTimeEntries collection, please try modify your formula inside the "Add" icon as below:

Select(Parent);
Patch(
      selectedTimeEntries,
      ThisItem,
      {
        ....
      }
);
Collect(
        selectedTimeEntries,
        Defaults('Timetable Test')    // Modify formula here
)

 

In addition, also please try to modify your Collect formula as below:

Select(Parent);
Patch(
      selectedTimeEntries,
      ThisItem,
      {
        ....
      }
);
Collect(
        selectedTimeEntries,
        {
           LessonNo: Max(selectedTimeEntries, LessonNo) + 1,
           'Start Time': DateAdd(StartDatePicker.SelectedDate,7,Days) + Time(Value(ddStartHour.Selected.Value), Value(ddStartMinutes.Selected.Value),0),
           'End Time':  DateAdd(EndDatePicker.SelectedDate,7,Days) + Time(Value(ddEndHour.Selected.Value), Value(ddEndMinutes.Selected.Value),0)
        }
)

 

Please try above solution, check if the issue is solved.

 

Regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Dear Both ( @v-xida-msft  & @GarethPrisk )

Thank you very much for you input. I have learned a lot. I went back and looked through my codes and found errors which I corrected. So far, I am able to do the following:

  1. From the VIEW COURSE screen, by clicking on the arrow, it correctly populates all the related events for the same course name on the EDIT SCREEN
    Mo_Islam_0-1598128334033.png
  2. The only things though that I can edit which saves correctly are the labels on the top: Teacher, Event, Category and Location.
  3. The + icon now is able to populate extra rows correctly as I wanted, i.e. +7 days

 

Select( Parent );
Patch(
    selectedTimeEntries,
    ThisItem,
    {
        LessonNo: Value(txtLessonNo.Text),
        EventDate: StartDatePicker_1.SelectedDate+Time(Value(ddStartHour_1.Selected.Value),Value(ddStartMinutes_1.Selected.Value),0),
        EndDate: EndDatePicker_1.SelectedDate+Time(Value(ddEndHour_1.Selected.Value),Value(ddEndMinutes_1.Selected.Value),0)
    }
);
Collect(
    selectedTimeEntries,
    {
        LessonNo: Text(txtLessonNo + 1),
        EventDate: DateAdd(StartDatePicker_1.SelectedDate,7,Days) + Time(Value(ddStartHour_1.Selected.Value), Value(ddStartMinutes_1.Selected.Value),0),
        EndDate: DateAdd(EndDatePicker_1.SelectedDate,7,Days) + Time(Value(ddEndHour_1.Selected.Value), Value(ddEndMinutes_1.Selected.Value),0)
    }
)​


Mo_Islam_1-1598128650616.png

 

  • When I click SAVE, it does save the new rows. 👏
    PROBLEM
  • If I make any changes to the existing rows date/time, it either does not make the change or it does only for the START TIME whilst making the END TIME blank.
  • Also, if I delete a row, this does not make the change to the SharePoint list.

Here is the SAVE code:

 

ForAll(
    galleryClassAdd_1.AllItems,
    If(IsBlank(ID),
    Patch(
        'Timetable Test',
        Defaults('Timetable Test'),
        {
            Title: toptxtCourse_1.Text,
            Location: toptxtLocation_1.Text,
            'Start Time': If(Checkbox1_1.Value=false, StartDatePicker_1.SelectedDate + Time(Value(ddStartHour_1.Selected.Value), Value(ddStartMinutes_1.Selected.Value),0), StartDatePicker_1.SelectedDate + Time(23,59,59)),
            'End Time': If(Checkbox1_1.Value=false,EndDatePicker_1.SelectedDate + Time(Value(ddEndHour_1.Selected.Value), Value(ddEndMinutes_1.Selected.Value),0), EndDatePicker_1.SelectedDate + Time(23,59,59)),
            ParticipantsPicker: TeacherSelector_1.Selected,
            Category: CategorySelector_1.Selected,
            LessonNo: If(Not(CategorySelector_1.Selected.Value="Weekend"), txtLessonNo.Text),
            Teacher: First(Split(TeacherSelector_1.Selected.Email,"@")).Result
        }
    ),
    Patch(
        'Timetable Test',
        {ID:ID},
        {
            Title: toptxtCourse_1.Text,
            Location: toptxtLocation_1.Text,
            'Start Time': If(Checkbox1_1.Value=false, StartDatePicker_1.SelectedDate + Time(Value(ddStartHour_1.Selected.Value), Value(ddStartMinutes_1.Selected.Value),0), StartDatePicker_1.SelectedDate + Time(23,59,59)),
            'End Time': If(Checkbox1_1.Value=false, EndDatePicker_1.SelectedDate + Time(Value(ddEndHour_1.Selected.Value), Value(ddEndMinutes_1.Selected.Value),0), EndDatePicker_1.SelectedDate + Time(23,59,59)),
            ParticipantsPicker: TeacherSelector_1.Selected,
            Category: CategorySelector_1.Selected,
            LessonNo: If(Not(CategorySelector_1.Selected.Value="Weekend"), txtLessonNo.Text),
            Teacher: First(Split(TeacherSelector_1.Selected.Email,"@")).Result
        }
    )
));
Refresh('Timetable Test');
Set(successMessage,"Event edited successfully");Navigate(SuccessScreen)
            

 

In a nutshell, I've only got part of it working but the actual Editing of the events does not work.

I'm almost there and would be grateful if you can suggest anything for me to try or if you can see any errors.

 

Thank you,

Mo

Hi @Mo_Islam ,

Do you mean that the "Save" formula could not work for updating existing records?

 

Please consider modify your formula as below:

ForAll(
    RenameColumns(galleryClassAdd_1.AllItems, "ID", "ID1"),  // modify formula here
    If(
       IsBlank(ID1),
       Patch(
            'Timetable Test',
            Defaults('Timetable Test'),
           {
             Title: toptxtCourse_1.Text,
            Location: toptxtLocation_1.Text,
            'Start Time': If(Checkbox1_1.Value=false, StartDatePicker_1.SelectedDate + Time(Value(ddStartHour_1.Selected.Value), Value(ddStartMinutes_1.Selected.Value),0), StartDatePicker_1.SelectedDate + Time(23,59,59)),
            'End Time': If(Checkbox1_1.Value=false,EndDatePicker_1.SelectedDate + Time(Value(ddEndHour_1.Selected.Value), Value(ddEndMinutes_1.Selected.Value),0), EndDatePicker_1.SelectedDate + Time(23,59,59)),
            ParticipantsPicker: TeacherSelector_1.Selected,
            Category: CategorySelector_1.Selected,
            LessonNo: If(Not(CategorySelector_1.Selected.Value="Weekend"), txtLessonNo.Text),
            Teacher: First(Split(TeacherSelector_1.Selected.Email,"@")).Result
           }
       ),
       Patch(
             'Timetable Test',
              LookUp('Timetable Test', ID = ID1), // Modify formula here. Find the specific record you want to update
            {
              Title: toptxtCourse_1.Text,
              Location: toptxtLocation_1.Text,
              'Start Time': If(Checkbox1_1.Value=false, StartDatePicker_1.SelectedDate + Time(Value(ddStartHour_1.Selected.Value), Value(ddStartMinutes_1.Selected.Value),0), StartDatePicker_1.SelectedDate + Time(23,59,59)),
              'End Time': If(Checkbox1_1.Value=false, EndDatePicker_1.SelectedDate + Time(Value(ddEndHour_1.Selected.Value), Value(ddEndMinutes_1.Selected.Value),0), EndDatePicker_1.SelectedDate + Time(23,59,59)),
              ParticipantsPicker: TeacherSelector_1.Selected,
              Category: CategorySelector_1.Selected,
              LessonNo: If(Not(CategorySelector_1.Selected.Value="Weekend"), txtLessonNo.Text),
              Teacher: First(Split(TeacherSelector_1.Selected.Email,"@")).Result
            }
        )
    )
);
Refresh('Timetable Test');
Set(successMessage,"Event edited successfully");Navigate(SuccessScreen)

 

Please try the above formula, then check if the issue is solved.

 

Regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Thank you so much @v-xida-msft 

This again allows to Add new records only and does not allow to edit/delete existing records.

I'm stuck 😥. No idea why it's not functioning like it should be.

Hi @Mo_Islam ,

Regarding the needs that you mentioned, I think the formula I provided above could achieve your needs. Please also consider try the following formula:

ForAll(
       galleryClassAdd_1.AllItems As LoopRecord,  // modify formula here
       Patch(
            'Timetable Test',
            If(                    // modify formula here
               IsBlank(LoopRecord.ID),
               Defaults('Timetable Test'),
               LookUp('Timetable Test', ID = LoopRecord.ID)
            ),
            {
             Title: toptxtCourse_1.Text,
             Location: toptxtLocation_1.Text,
             'Start Time': If(Checkbox1_1.Value=false, StartDatePicker_1.SelectedDate + Time(Value(ddStartHour_1.Selected.Value), Value(ddStartMinutes_1.Selected.Value),0), StartDatePicker_1.SelectedDate + Time(23,59,59)),
            'End Time': If(Checkbox1_1.Value=false,EndDatePicker_1.SelectedDate + Time(Value(ddEndHour_1.Selected.Value), Value(ddEndMinutes_1.Selected.Value),0), EndDatePicker_1.SelectedDate + Time(23,59,59)),
            ParticipantsPicker: TeacherSelector_1.Selected,
            Category: CategorySelector_1.Selected,
            LessonNo: If(Not(CategorySelector_1.Selected.Value="Weekend"), txtLessonNo.Text),
            Teacher: First(Split(TeacherSelector_1.Selected.Email,"@")).Result
           }
      )
);
Refresh('Timetable Test');
Set(successMessage,"Event edited successfully");Navigate(SuccessScreen)

 

Please try above solution, then check if the issue is solved.

 

Regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-xida-msft  This one also doesn't work.

 

May be if I explain how I get to the edit screen, it will help to understand better:

  • I add records to the SP List with the same 'Title' which represents the Course Name.
    • This allows all sessions under this course name to be filtered

Mo_Islam_0-1598265810832.png

  • The galViewCourses in the powerapp screen has this code for Items:

 

Distinct(
    SortByColumns(
        Filter(
            'Rabat Timetable Test',
            If(Dropdown1.Selected.Value="All",StartsWith(Course,tbSeacrhInput.Text), 
            Category.Value = Dropdown1.Selected.Value && StartsWith(Course,tbSeacrhInput.Text))
        ),
        "EventDate",
        Descending
    ),
    Course
)

 

  • The onSelect for an item has this code which carries over to the EDIT screen which you have seen before:

 

Select(Parent);
Set(
    selectedEvent,
    ThisItem
);
ClearCollect(
    selectedTimeEntries,
    Filter(
        'Rabat Timetable Test',
        Course = selectedEvent.Result
    )
)

 

Am I doing something wrong here? 

And yes I agree, your previous code should work but again it only allows me to add new records but not edit.

I'm happy to try work only on making modify to work, and then combine later with adding new records as well. I'm very grateful for your support.

When you use the remove code only for the collection (trash icon), it does not not update the SharePoint list by removing the record. So the workaround is to remove the record instantly when you click the trash icon as follows:

 

Select(Parent);
Remove(selectedTimeEntries,ThisItem);
Remove('Timetable Test',ThisItem)

 

This removes from the collection (selectedTimeEntries) and from the SP list instantly.

Thank you @v-xida-msft . 

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (858)