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 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)); }
/***** * 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); }
private static Entity GetFullPostText(IOrganizationService org, EntityReference entityRef, Entity post, ref Entity postComment) { // Retrieve the full wall for this record to expand out any standard posts var wallPage = 1; while (true) { var wall = (RetrieveRecordWallResponse)org.Execute(new RetrieveRecordWallRequest { Entity = entityRef, CommentsPerPost = 10, PageSize = 10, PageNumber = wallPage, Source = post.GetAttributeValue <OptionSetValue>("source") }); var foundPost = false; foreach (var wallPost in wall.EntityCollection.Entities) { if (wallPost.Id == post.Id) { if (post == postComment) { postComment = wallPost; } post = wallPost; foundPost = true; break; } } if (foundPost || !wall.EntityCollection.MoreRecords) { break; } wallPage++; } if (postComment == post) { postComment = post; } else { EntityCollection comments; if (post.RelatedEntities.TryGetValue(new Relationship("Post_Comments"), out comments)) { foreach (var wallComment in comments.Entities) { if (wallComment.Id == postComment.Id) { postComment = wallComment; break; } } } } return(post); }