Categories: Custom Pages
If any of my blogs or videos have ever helped you, I’d love to ask a favour.
I’m taking on a challenge: walking 50 KM in under 10 hours in honour of two incredible women who are both bravely facing chemotherapy right now.
🩷 I’m fundraising for Cancer Research UK 🩷
If you can help, every donation, big or small, helps bring us closer to a cure
*** NOTE: ALL INFORMATION IS ACCURATE AT DATE OF PUBLISHING ***

Activities are a pretty common way to understand and manage records in Dataverse/D365. Users can add tasks, appointments, phone calls, and custom activities via the timeline on a record. Account Managers and Business Development Managers can review timelines to see what has been happening with that record before calling them, and Customer Service Reps can link activities to cases to show their support work. What if someone is using one of your custom pages and updates some fields? It might be a requirement to also see that update on the timeline in the form of a task. In this post we will see how to add activities to a record timeline by creating and completing a task from a custom page.

Edit your custom page first by adding in the activity table for the activity type you want to create. In my example I will add in the Tasks table. Then find the Users table and add that too. We need to be able to link the Task to the User who pressed the button and interacted with the Custom page.

Click to view in detail

In your OnStart logic at the App level, add in the following expression. We can use this simple expression to get the user who opened the custom page and set them in a variable called CurrentUser.

Set(CurrentUser, User());
Click to view in detail

Now we can go ahead and update the Patch statement that is on the OnSelect of our button that submits the custom page. You can add something like this near the bottom of the Patch but above the Back(); assuming you have one. You want to make sure the Task is created before the custom page gets closed. So first we are creating a new variable called varNewTask. This is so that if we want to mark that Task as closed, we can easily get that record in another statement. We then Patch through to the Tasks table and use Defaults when we create a new record. The fields you want to fill out are up to you, although you will need to set any that are required on that entity. One thing to note, you will not be able to create and mark the activity as complete in one step, but more on that in a bit.

  • Subject – Put something logical that will show on the Task
  • Description – Again, put something logical. What we can also do is use the value that was added to the txtReason multi-line text box on the custom page by joining some text in quotes, then & and the value of that text box
  • Regarding – this is important and is what makes the task show up on the timeline for a record. The regarding should be set by using LookUp then identifying the Leads table, then looking for a Lead where the ID of the Lead equals the varSelectedRecord (that you should have set on your OnStart of the App) Lead ID
  • Start Date – you could set it as Now() which would be the current date and time
  • Owner – again we will use a Lookup to search the Users table, then match where the Users Name equals the current users email address which should find and match the user that opened the custom page
Set(
  varNewTask,
  Patch(
    Tasks,
    Defaults(Tasks),
    {
      Subject: "Lead Returned To Marketing",
      Description: "The Lead was returned to Marketing. The reason provided was: " & txtReason.Value,
      Regarding: LookUp(Leads, Lead = varSelectedRecord.Lead),
      'Start Date': Now(),
       Owner: LookUp(Users, 'User Name' = CurrentUser.Email)
      
    }
  )
);
Click to view in detail

When you test your custom page, you should be able to navigate to the Lead (or record) and find the Timeline and see that the task was created. It will show the user who used the custom page as the one who modified it too. Perfect! However, what if there is nothing for that person to complete, we don’t want to have all these tasks sitting as Overdue and clogging up Dashboards and Views.

Click to view in detail

This is where that varNewTask variable comes in. We can add another Patch statement directly below the first one that created the task, and this time we can set the Activity Status (not Status) as Completed and the Status Reason as Completed. We can also use Now() to set the Actual End date too.

Patch(
  Tasks,
  varNewTask,
  {
    'Activity Status': 'Activity Status (Tasks)'.Completed,
    'Status Reason': 'Status Reason (Tasks)'.Completed,
    'Actual End': Now()
  }
);

So the full part of the Patch that creates and closes the Task should look something like this. Again, we need to make sure that Back() comes right at the end.

Click to view in detail

Now the Task on the timeline should show completed by the user who opened the custom page with all of the details that were provided from the text box they filled out.

Click to view in detail


Check out the latest post:
Add Information Buttons To Provide Additional Details For Custom Page Users


If any of my blogs or videos have ever helped you, I’d love to ask a favour.
I’m taking on a challenge: walking 50 KM in under 10 hours in honour of two incredible women who are both bravely facing chemotherapy right now.
🩷 I’m fundraising for Cancer Research UK 🩷
If you can help, every donation, big or small, helps bring us closer to a cure
This is just 1 of 580 articles. You can browse through all of them by going to the main blog page, or navigate through different categories to find more content you are interested in. You can also subscribe and get new blog posts emailed to you directly.


Leave a Reply

Your email address will not be published. Required fields are marked *