public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value != default(object)) { Dynamics365State state = (Dynamics365State)value; return(string.Format("{0} ({1})", state.Name, state.Code)); } return(base.ConvertTo(context, culture, value, destinationType)); }
public List <Dynamics365State> GetStates() { List <Dynamics365State> states = new List <Dynamics365State>(); try { states.AddRange(Dynamics365State.GetStates(Entity, Connection)); } catch { } return(states); }
public List <Field> GetDynamics365SetStateDataDestinationFields() { List <Field> fields = new List <Field>(); try { List <Dynamics365State> states = Dynamics365State.GetStates(Entity, Connection); if (states?.Count > 0) { // add state and status field from destination entity Dynamics365State state = states[0]; Dynamics365Status status = state.Statuses[0]; fields.AddRange(Entity.GetFields(Connection).Where(field => ((Dynamics365Field)field).LogicalName == state.LogicalName || ((Dynamics365Field)field).LogicalName == status.LogicalName)); } if (Entity.LogicalName == CASE_ENTITY_NAME) { Dynamics365Entity incidentResolution = Dynamics365Entity.Create(CASE_RESOLUTION_ENTITY_NAME, Connection); fields.AddRange(incidentResolution.GetFields(Connection).Where(field => ((Dynamics365Field)field).LogicalName == "subject" || ((Dynamics365Field)field).LogicalName == "description")); } else if (Entity.LogicalName == QUOTE_ENTITY_NAME) { Dynamics365Entity quoteClose = Dynamics365Entity.Create(CASE_RESOLUTION_ENTITY_NAME, Connection); fields.AddRange(quoteClose.GetFields(Connection).Where(field => ((Dynamics365Field)field).LogicalName == "subject" || ((Dynamics365Field)field).LogicalName == "description")); } else if (Entity.LogicalName == OPPORTUNITY_ENTITY_NAME) { Dynamics365Entity opportunityClose = Dynamics365Entity.Create(CASE_RESOLUTION_ENTITY_NAME, Connection); fields.AddRange(opportunityClose.GetFields(Connection).Where(field => ((Dynamics365Field)field).LogicalName == "subject" || ((Dynamics365Field)field).LogicalName == "description" || ((Dynamics365Field)field).LogicalName == "actualend" || ((Dynamics365Field)field).LogicalName == "actualrevenue")); } // todo - check this is correct. need to be able to support multiple values for 'first non-null value' logic //foreach (FieldValue value in values) //{ // // prevent fields from being selected more than once // fields.RemoveAll(field => field == value.DestinationField); //} } catch { } return(fields); }
public static List <Dynamics365State> GetStates(Dynamics365Entity entity, Dynamics365Connection connection) { ConnectionCache cache = new ConnectionCache(connection); string cacheKey = string.Format("GetStates:{0}", entity.LogicalName); List <Dynamics365State> states = (List <Dynamics365State>)cache[cacheKey]; if (states == null) { states = new List <Dynamics365State>(); RetrieveEntityRequest request = new RetrieveEntityRequest() { LogicalName = entity.LogicalName, EntityFilters = EntityFilters.Attributes, RetrieveAsIfPublished = false }; using (OrganizationServiceProxy proxy = connection.OrganizationServiceProxy) { RetrieveEntityResponse response = (RetrieveEntityResponse)proxy.Execute(request); StateAttributeMetadata stateMetadata = (StateAttributeMetadata)response.EntityMetadata.Attributes.FirstOrDefault(findField => findField is StateAttributeMetadata); if (stateMetadata != null) { foreach (StateOptionMetadata stateOption in stateMetadata.OptionSet.Options) { Dynamics365State state = new Dynamics365State() { Code = (int)stateOption.Value, Name = stateOption.Label.UserLocalizedLabel.Label, LogicalName = stateMetadata.LogicalName }; StatusAttributeMetadata statusMetadata = (StatusAttributeMetadata)response.EntityMetadata.Attributes.FirstOrDefault(findField => findField is StatusAttributeMetadata); if (statusMetadata != null) { foreach (StatusOptionMetadata statusOption in statusMetadata.OptionSet.Options) { if (statusOption.State == state.Code) { Dynamics365Status status = new Dynamics365Status() { Code = (int)statusOption.Value, Name = statusOption.Label.UserLocalizedLabel.Label, LogicalName = statusMetadata.LogicalName }; state.Statuses.Add(status); } } //state.Statuses.Sort((status1, status2) => status1.Name.CompareTo(status2.Name)); } states.Add(state); } } } //states.Sort((state1, state2) => state1.Name.CompareTo(state2.Name)); cache[cacheKey] = states; } return(states); }
protected override string GetRequestDescription(OrganizationRequest request) { Guid id = Guid.Empty; string state = mode == SetStateMode.Fixed ? State.Name : "<State>"; string status = mode == SetStateMode.Fixed ? Status.Name : "<Status>"; if (request is UpdateRequest updateRequest) { id = updateRequest.Target.Id; } else if (request is SetStateRequest setStateRequest) { id = setStateRequest.EntityMoniker.Id; } else if (request is CloseIncidentRequest closeIncidentRequest) { id = ((EntityReference)closeIncidentRequest.IncidentResolution[CASE_RESOLUTION_INCIDENT_ID]).Id; state = "Resolved"; EntityMetadata metadata = Dynamics365Entity.Create(CASE_ENTITY_NAME, Connection).GetEntityMetadata(Connection); StatusAttributeMetadata statusMetadata = ((StatusAttributeMetadata)metadata.Attributes.First(field => field.LogicalName == STATUSCODE_NAME)); status = statusMetadata.OptionSet.Options.First(option => option.Value == closeIncidentRequest.Status.Value).Label.UserLocalizedLabel.Label; } else if (request is ReviseQuoteRequest reviseQuoteRequest) { id = reviseQuoteRequest.QuoteId; state = "Draft"; status = "In Progress"; } else if (request is WinQuoteRequest winQuoteRequest) { id = ((EntityReference)winQuoteRequest.QuoteClose[QUOTE_CLOSE_QUOTE_ID]).Id; state = "Won"; status = "Won"; } else if (request is CloseQuoteRequest closeQuoteRequest) { id = ((EntityReference)closeQuoteRequest.QuoteClose[QUOTE_CLOSE_QUOTE_ID]).Id; state = "Closed"; EntityMetadata metadata = Dynamics365Entity.Create(QUOTE_ENTITY_NAME, Connection).GetEntityMetadata(Connection); StatusAttributeMetadata statusMetadata = ((StatusAttributeMetadata)metadata.Attributes.First(field => field.LogicalName == STATUSCODE_NAME)); status = statusMetadata.OptionSet.Options.First(option => option.Value == closeQuoteRequest.Status.Value).Label.UserLocalizedLabel.Label; } else if (request is WinOpportunityRequest winOpportunityRequest) { id = ((EntityReference)winOpportunityRequest.OpportunityClose[OPPORTUNITY_CLOSE_OPPORTUNITY_ID]).Id; state = "Won"; EntityMetadata metadata = Dynamics365Entity.Create(OPPORTUNITY_ENTITY_NAME, Connection).GetEntityMetadata(Connection); StatusAttributeMetadata statusMetadata = ((StatusAttributeMetadata)metadata.Attributes.First(field => field.LogicalName == STATUSCODE_NAME)); status = statusMetadata.OptionSet.Options.First(option => option.Value == winOpportunityRequest.Status.Value).Label.UserLocalizedLabel.Label; } else if (request is LoseOpportunityRequest loseOpportunityRequest) { id = ((EntityReference)loseOpportunityRequest.OpportunityClose[OPPORTUNITY_CLOSE_OPPORTUNITY_ID]).Id; state = "Lost"; EntityMetadata metadata = Dynamics365Entity.Create(OPPORTUNITY_ENTITY_NAME, Connection).GetEntityMetadata(Connection); StatusAttributeMetadata statusMetadata = ((StatusAttributeMetadata)metadata.Attributes.First(field => field.LogicalName == STATUSCODE_NAME)); status = statusMetadata.OptionSet.Options.First(option => option.Value == loseOpportunityRequest.Status.Value).Label.UserLocalizedLabel.Label; } return(string.Format(Properties.Resources.Dynamics365SetStateOperationRequestDescription, Entity.DisplayName, id, state, status)); }