private void GetInterestedUsers(Entity post, Entity comment, Entity entity, IOrganizationService org, HashSet <EntityReference> userIds, Dictionary <Guid, Link> entityRelationships) { var processedEntities = new HashSet <Guid>(); // Get any users mentioned in the post var userRegex = new Regex(@"@\[(?<etc>[0-9]+),(?<id>[a-z0-9-]+),", RegexOptions.IgnoreCase); foreach (Match user in userRegex.Matches(comment.GetAttributeValue <string>("text"))) { FollowMention(Int32.Parse(user.Groups["etc"].Value), new Guid(user.Groups["id"].Value), org, processedEntities, new Link { From = comment.ToEntityReference(), Description = "Mentions" }, userIds, entityRelationships); } // Get the user who posted the comment var createdBy = comment.GetAttributeValue <EntityReference>("createdby"); if (userIds.Add(createdBy)) { entityRelationships.Add(createdBy.Id, new Link { From = comment.ToEntityReference(), Description = "Posted By" }); } // Get the user who posted the original post createdBy = post.GetAttributeValue <EntityReference>("createdby"); if (userIds.Add(createdBy)) { entityRelationships.Add(createdBy.Id, new Link { From = post.ToEntityReference(), Description = "Posted By" }); } // Add any user who has also replied to this same post var replyQry = new QueryByAttribute("postcomment"); replyQry.AddAttributeValue("postid", post.Id); replyQry.ColumnSet = new ColumnSet("createdby"); foreach (var reply in org.RetrieveMultiple(replyQry).Entities) { createdBy = reply.GetAttributeValue <EntityReference>("createdby"); if (userIds.Add(createdBy)) { entityRelationships.Add(createdBy.Id, new Link { From = entity.ToEntityReference(), Description = "Also Commented On By" }); } } // Get anyone otherwise interested in the record being posted on GetInterestedUsers(entity, org, processedEntities, userIds, entityRelationships); }
public static Entity Merge(this Entity sourceEntity, Entity preImage) { var newEntity = sourceEntity?.ToEntityReference().ToEntity() ?? preImage; if (sourceEntity != null) { foreach (var fieldName in sourceEntity.Attributes.Keys) { newEntity[fieldName] = sourceEntity[fieldName]; } } if (preImage == null) { return(newEntity); } foreach (var fieldName in preImage.Attributes.Keys) { if (!newEntity.Contains(fieldName)) { newEntity[fieldName] = preImage[fieldName]; } } return(newEntity); }
/***** * Currently unable to get this function working. * Soemthing about the WinQuoteRequest and CloseQuoteRequest does not fuction as expected * error: "cannot close the entity because it is not in the correct state" always occurs * ******************/ private static void ConvertQuoteToOrder(Microsoft.Xrm.Sdk.Client.OrganizationServiceContext context, Microsoft.Xrm.Sdk.Entity myQuote) { // Activate the quote SetStateRequest activateQuote = new SetStateRequest() { EntityMoniker = myQuote.ToEntityReference(), State = new OptionSetValue(1), Status = new OptionSetValue(2) }; context.Execute(activateQuote); //Console.WriteLine("Quote activated."); Guid quoteId = myQuote.GetAttributeValue <Guid>("quoteid"); var quoteClose = new Entity("quoteclose"); quoteClose.Attributes["quoteid"] = myQuote.ToEntityReference(); quoteClose.Attributes["subject"] = "Won The Quote"; WinQuoteRequest winQuoteRequest = new WinQuoteRequest() { QuoteClose = quoteClose, Status = new OptionSetValue(-1) //2? -1?? }; var winQuoteResponse = (WinQuoteResponse)context.Execute(winQuoteRequest); ColumnSet salesOrderColumns = new ColumnSet("salesorderid", "totalamount"); var convertOrderRequest = new ConvertQuoteToSalesOrderRequest() { QuoteId = quoteId, ColumnSet = salesOrderColumns }; var convertOrderResponse = (ConvertQuoteToSalesOrderResponse)context.Execute(convertOrderRequest); }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function)] HttpRequestMessage req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); var content = req.Content; string jsonContent = await content.ReadAsStringAsync(); //log.Info($"Received D365 Event with payload: {jsonContent}"); Microsoft.Xrm.Sdk.RemoteExecutionContext remoteExecutionContext = DeserializeJsonString <Microsoft.Xrm.Sdk.RemoteExecutionContext>(jsonContent); //read Plugin Message Name string messageName = remoteExecutionContext.MessageName; log.Info($"Message Name : {messageName}"); //read execution depth of plugin Int32 depth = remoteExecutionContext.Depth; log.Info($"Depth : {depth}"); //read BusinessUnitId Guid businessUnitid = remoteExecutionContext.BusinessUnitId; log.Info($"Business Unit ID : {businessUnitid.ToString()}"); //read Target Entity Microsoft.Xrm.Sdk.Entity targetEntity = (Microsoft.Xrm.Sdk.Entity)remoteExecutionContext.InputParameters["Target"]; log.Info($"Target Entity Logical Name : {targetEntity.LogicalName} - ID : {targetEntity.Id.ToString()}"); //read attribute from Target Entity string dataPayload = targetEntity.GetAttributeValue <string>("fkh_eventdata"); log.Info($"Data PayLoad : {dataPayload}"); if (!string.IsNullOrEmpty(dataPayload)) { log.Info($"Sending Event to EventGrid Topic..."); callTopicAsync(log, dataPayload, targetEntity.ToEntityReference()).GetAwaiter().GetResult(); log.Info($"Event successfully sent to EventGrid Topic..."); } return(jsonContent == null ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body") : req.CreateResponse(HttpStatusCode.OK, "Hello " + jsonContent)); }
/// <summary> /// Delete method override. Takes Entity as input parameter /// </summary> /// <param name="entity">Entity to delete</param> public static void Delete(this IOrganizationService service, Entity entity) { CheckParam.CheckForNull(entity, nameof(entity)); service.Delete(entity.ToEntityReference(true)); }
private void GetInterestedUsers(Entity entity, IOrganizationService org, HashSet <Guid> processedEntities, HashSet <EntityReference> userIds, Dictionary <Guid, Link> entityRelationships) { if (!processedEntities.Add(entity.Id)) { return; } // Add any user explicitly linked to from the parent record // Include owner, exclude createdby and modifiedby as they can set to workflow owners who aren't interested // in specific records foreach (var attribute in entity.Attributes) { if (attribute.Key == "createdby" || attribute.Key == "modifiedby") { continue; } var userRef = attribute.Value as EntityReference; if (userRef != null && (userRef.LogicalName == "systemuser" || userRef.LogicalName == "team")) { if (userIds.Add(userRef)) { entityRelationships.Add(userRef.Id, new Link { From = entity.ToEntityReference(), Description = GetEntityMetadata(org, entity.LogicalName).Attributes.Single(a => a.LogicalName == attribute.Key).DisplayName.UserLocalizedLabel.Label }); } } } // Add any user who follows the parent record. var entityFollowsQry = new QueryByAttribute("postfollow"); entityFollowsQry.AddAttributeValue("regardingobjectid", entity.Id); entityFollowsQry.ColumnSet = new ColumnSet("ownerid"); foreach (var entityFollow in org.RetrieveMultiple(entityFollowsQry).Entities) { var userRef = entityFollow.GetAttributeValue <EntityReference>("ownerid"); if (userIds.Add(userRef)) { entityRelationships.Add(userRef.Id, new Link { From = entity.ToEntityReference(), Description = "Followed By" }); } } // Recurse into any accounts linked to this record var accountIds = new HashSet <Guid>(); foreach (var attribute in entity.Attributes) { var accountRef = attribute.Value as EntityReference; if (accountRef != null && accountRef.LogicalName == "account" && !processedEntities.Contains(accountRef.Id)) { accountIds.Add(accountRef.Id); if (!entityRelationships.ContainsKey(accountRef.Id)) { entityRelationships.Add(accountRef.Id, new Link { From = entity.ToEntityReference(), Description = GetEntityMetadata(org, entity.LogicalName).Attributes.Single(a => a.LogicalName == attribute.Key).DisplayName.UserLocalizedLabel.Label }); } } } foreach (var accountId in accountIds) { var account = org.Retrieve("account", accountId, new ColumnSet("ownerid")); // Recurse into the account GetInterestedUsers(account, org, processedEntities, userIds, entityRelationships); } }