locked
Run workflow against RequiredAttendee Field on Appointment record RRS feed

  • Question

  • Hi, 

    We currently run a workflow on the appointment record that updates the related contact's last appointment date with the date of a new appointment so we can see when we last met them.  This works against the regarding field on the appointment record.  However, the required attendees field is a better one to update against, its a party list that allows us to select multiple contacts which is very common.  I am struggling to figure out how I can relate appointments to the Required Attendee field so when an appointment is filed out all of the contacts under Required Attendees are updated appropriately.  


    • Edited by pslager Monday, September 15, 2014 2:21 PM
    Friday, September 12, 2014 5:53 PM

All replies

  • hi,

    we had a similar requirement - to update Last contacted date of contact entity based on appointment and phone call.

    when it was just regarding fields, the work flow was sufficient.

    when we had to change it to party list, we scrapped the workflow and converted it to a plugin

    regards,

    Jithesh

    Saturday, September 13, 2014 1:16 AM
  • Thanks for the reply, sounds like we tried to do the same thing.  It almost seems impossible but I am not an expert.  Did you custom create a plugin?  
    Monday, September 15, 2014 2:16 PM
  • Hi Mate,

    Sorry for the delayed reply. The code was in 3 files and took some time to extract it. There may be extra variables or strange scope :) please ignore those apparent mistkes ( they are in for a reason). I have tried to extract all methods, let me know if any missing. This is a plugin code and will be required to be subscribing to the setstatemessage of appointments and phonecalls - just appointment for your case. Will appreciate it if you can post the working code that you created based on this code for the benefit of others.

    Cheers. Code courtesy - fellow developer Shivaraja Venketachellappa.

    public class ActivityPlugin : IPlugin
    {
        private IOrganizationService _OrganizationService;
    
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context =
                (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            _OrganizationService = service;
            //Update Entity's Last Contact date based on completed appointment
            if (context.MessageName.Equals("SetStateDynamicEntity"))
            {
                if (context.InputParameters.Contains("State"))
                {
    
                    string stateValue = ((OptionSetValue)context.InputParameters["State"]).Value.ToString();
                    EntityReference moniker = (EntityReference)context.InputParameters["EntityMoniker"];
    
                    if (stateValue.Equals("1") && moniker.LogicalName == "appointment") //Complete status
                    {
                        UpdateEntityLastContactDate_Appointment(moniker.Id);
                    }
                    else if (stateValue.Equals("1") && moniker.LogicalName == "phonecall") //Complete status
                    {
                        UpdateEntityLastContactDate_PhoneCall(moniker.Id);
                    }
                }
            }
        }
        public void UpdateEntityLastContactDate_Appointment(Guid AppointmentId)
        {
            DataCollection<Entity> activityparties = null;
    
            Entity Appointment = RetrieveAppointment(AppointmentId);
            activityparties = ((EntityCollection)(Appointment["requiredattendees"])).Entities;
            EntityReference regardingId = (EntityReference)Appointment["regardingobjectid"];
            DateTime actualEndDate = (DateTime)Appointment["scheduledend"];
    
            //Update the last contact date for regarding entity
            if (Appointment.GetAttributeValue<EntityReference>("regardingobjectid").LogicalName == "contact")
                UpdateLastContactDate(regardingId.Id, actualEndDate);
    
            //update the last contact date for all parties under To list
            if (activityparties != null)
            {
                if (activityparties.Count > 0)
                {
                    foreach (Entity party in activityparties)
                    {
                        EntityReference partyrecord = RetrieveActivityParty(party.Id);
                        //Check if party is a contact
                        if (partyrecord.LogicalName == "contact")
                        {
                            UpdateLastContactDate(partyrecord.Id, actualEndDate);
                        }
                    }
                }
            }
        }
    
        public void UpdateLastContactDate(Guid entityId, DateTime Date)
        {
            DateTime CurrentLastContactDate;
            Entity contact = RetrieveEntity(entityId);
            Entity updatedEntity = new Entity("contact");
    
            updatedEntity["contactid"] = entityId;
    
            if (contact.Contains("rio_lastcontactdate"))
            {
                CurrentLastContactDate = (DateTime)contact["rio_lastcontactdate"];
                if (CurrentLastContactDate < Date)
                {
                    updatedEntity["rio_lastcontactdate"] = Date;
                }
            }
            else
            {
                updatedEntity["rio_lastcontactdate"] = Date;
            }
    
            _OrganizationService.Update(updatedEntity);
    
        }
    
        public Entity RetrieveAppointment(Guid appointmentId)
        {
            if (appointmentId == Guid.Empty)
            {
                throw new ArgumentException("entityId");
            }
    
            string entityName = "appointment";
            ColumnSet appointmentColumns = new ColumnSet();
            appointmentColumns.AddColumns(new string[]
                                                 {
                                                    "scheduledend", "regardingobjectid", "actualend", "requiredattendees"
                                                 });
    
            return _OrganizationService.Retrieve(entityName, appointmentId, appointmentColumns);
    
        }
    
        public Entity RetrievePhonecall(Guid phonecallId)
        {
            if (phonecallId == Guid.Empty)
            {
                throw new ArgumentException("phonecallId");
            }
    
            string entityName = "phonecall";
            ColumnSet phonecallColumns = new ColumnSet();
            phonecallColumns.AddColumns(new string[]
                                                 {
                                                    "createdon", "from", "to"
                                                 });
    
            return _OrganizationService.Retrieve(entityName, phonecallId, phonecallColumns);
    
        }
        public void UpdateEntityLastContactDate_PhoneCall(Guid PhoneCallId)
        {
            DataCollection<Entity> fromparties = null;
            DataCollection<Entity> toparties = null;
            DateTime createdonDate;
    
            Entity Phonecall = RetrievePhonecall(PhoneCallId);
            createdonDate = (DateTime)Phonecall["createdon"];
            fromparties = ((EntityCollection)Phonecall["from"]).Entities;
            toparties = ((EntityCollection)Phonecall["to"]).Entities;
    
    
            //update the last contact date for all parties under 'from' list
            if (fromparties != null)
            {
                if (fromparties.Count > 0)
                {
                    foreach (Entity party in fromparties)
                    {
                        EntityReference partyrecord = RetrieveActivityParty(party.Id);
                        //Check if party is a contact
                        if (partyrecord.LogicalName == "contact")
                        {
                            UpdateLastContactDate(partyrecord.Id, createdonDate);
                        }
                    }
                }
            }
            //update the last contact date for all parties under 'To' list
            if (toparties != null)
            {
                if (toparties.Count > 0)
                {
                    foreach (Entity party in toparties)
                    {
                        EntityReference partyrecord = RetrieveActivityParty(party.Id);
                        //Check if party is a contact
                        if (partyrecord.LogicalName == "contact")
                        {
                            UpdateLastContactDate(partyrecord.Id, createdonDate);
                        }
                    }
                }
            }
    
        }
    }

    Tuesday, September 16, 2014 1:56 PM
  • missing function added below
    public EntityReference RetrieveActivityParty(Guid activityPartyId)
            {
                if (activityPartyId == Guid.Empty)
                {
                    throw new ArgumentException("activityPartyId");
                }
    
                OrganizationServiceContext orgContext = new OrganizationServiceContext(_OrganizationService);
    
                Entity activityParty = (from p in orgContext.CreateQuery("activityparty")
                                        where p.GetAttributeValue<Guid>("activitypartyid") == activityPartyId
                                        select p).FirstOrDefault();
    
    
                return (EntityReference)activityParty["partyid"];
    
            }

    Tuesday, September 16, 2014 1:59 PM