You can use Power Automate FLOW or a plugin to achieve this functionality. You will need to set Activity party object in the form.
Here is sample code, please test first at your end. You will need to register plugin using Plugin registration code and register step on create of appointment entity / table.
using System;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
public class SetAppointmentFromField : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Check if the target entity is Appointment and if it's being created or updated
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity targetEntity &&
targetEntity.LogicalName == "appointment")
{
// Obtain the organization service
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
// Get the current user ID
Guid currentUserId = context.InitiatingUserId;
// Query queues where the user is a member
QueryExpression query = new QueryExpression("queue")
{
ColumnSet = new ColumnSet("queueid", "name"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("primaryuserid", ConditionOperator.Equal, currentUserId)
}
}
};
EntityCollection queues = service.RetrieveMultiple(query);
if (queues.Entities.Any())
{
// Use the first queue found (or apply your own logic to determine which queue to use)
Entity queue = queues.Entities.First();
EntityReference queueReference = new EntityReference("queue", queue.Id);
// Update the "From" field of the appointment
if (targetEntity.Attributes.Contains("from"))
{
targetEntity["from"] = new EntityCollection(new Entity[]
{
new Entity("activityparty")
{
["partyid"] = queueReference
}
});
}
else
{
targetEntity.Attributes.Add("from", new EntityCollection(new Entity[]
{
new Entity("activityparty")
{
["partyid"] = queueReference
}
}));
}
}
else
{
throw new InvalidPluginExecutionException("The current user is not associated with any queue.");
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException($"An error occurred in the SetAppointmentFromField plugin: {ex.Message}", ex);
}
}
}
}