标签:ide exception tor class tar ima 记录 跟踪 payment
asdfa
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService orgSvc = serviceFactory.CreateOrganizationService(context.UserId);
1 using Microsoft.Xrm.Sdk; 2 using Microsoft.Xrm.Sdk.Query; 3 using System; 4 using System.ServiceModel; 5 using System.Text; 6 7 namespace DemoPlugins 8 { 9 public class YaoOrderItemPostUpdate : IPlugin 10 { 11 public void Execute(IServiceProvider serviceProvider) 12 { 13 14 //获取日志服务 15 ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); 16 //写一些日志,方便跟踪 17 tracingService.Trace($"Enter PreWorkOrderCreate on {DateTime.UtcNow.ToString()}"); 18 IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 19 if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) 20 { 21 //插件针对的当前实体记录,对于Pre Create来讲,该对象包括了所有设置的字段值,若字段没有设置值,在该对象中会不存在 22 Entity currentEntity = (Entity)context.InputParameters["Target"]; 23 //获取组织服务 24 IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 25 IOrganizationService orgSvc = serviceFactory.CreateOrganizationService(context.UserId); 26 27 if (currentEntity.Id != null) 28 { 29 30 //单价总价 31 decimal sumAmount = 0; 32 //获取明细信息 33 QueryExpression queryExpressionItem = new QueryExpression("new_yaoorderitem"); 34 queryExpressionItem.ColumnSet.AllColumns = true; 35 queryExpressionItem.Criteria.AddCondition("new_yaoorderitemid", ConditionOperator.Equal, currentEntity.Id); 36 queryExpressionItem.NoLock = true; 37 //queryExpressionItem.TopCount = 1; 38 var item = orgSvc.RetrieveMultiple(queryExpressionItem); 39 //查询主表Id 40 Guid orderid = new Guid(); 41 if (item != null && item.Entities != null && item.Entities.Count > 0) 42 { 43 orderid = item.Entities[0].GetAttributeValue<EntityReference>("new_yaoorderid").Id; 44 int b = 100; 45 } 46 47 //所有明细 48 QueryExpression queryExpressionItem2 = new QueryExpression("new_yaoorderitem"); 49 queryExpressionItem2.ColumnSet.AllColumns = true; 50 queryExpressionItem2.Criteria.AddCondition("new_yaoorderid", ConditionOperator.Equal, orderid); 51 queryExpressionItem2.NoLock = true; 52 //queryExpressionItem.TopCount = 1; 53 var items = orgSvc.RetrieveMultiple(queryExpressionItem2); 54 55 if (items != null && items.Entities != null && items.Entities.Count > 0) 56 { 57 foreach (var e in items.Entities) 58 { 59 if (e.Attributes.Contains("new_amount")) 60 { 61 sumAmount += (decimal)e.Attributes["new_amount"]; 62 } 63 } 64 } 65 66 //获取当前主表信息 67 //var new_yaoorderid = ((Microsoft.Xrm.Sdk.EntityReference)currentEntity["new_yaoorderid"]).Id; 68 69 //获取主表信息 70 QueryExpression qe = new QueryExpression("new_yaoorder"); 71 //qe.ColumnSet.AllColumns = true; 72 qe.ColumnSet = new ColumnSet("new_yaoorderid", "new_sumamount", "new_discountamount", "new_paymentamount"); 73 qe.Criteria.AddCondition("new_yaoorderid", ConditionOperator.Equal, orderid); 74 qe.NoLock = true; 75 qe.TopCount = 1; 76 var ec = orgSvc.RetrieveMultiple(qe); 77 //单价金额 78 if (ec.Entities[0].Attributes.Contains("new_sumamount")) 79 { 80 ec.Entities[0]["new_sumamount"] = sumAmount; 81 } 82 //优惠金额 83 decimal new_discountamount = 0; 84 if (ec.Entities[0].Attributes.Contains("new_discountamount")) 85 { 86 new_discountamount = decimal.Parse(ec.Entities[0]["new_discountamount"].ToString()); 87 } 88 //应付金额 = 单价金额 - 优惠金额 89 if (ec.Entities[0].Attributes.Contains("new_paymentamount")) 90 { 91 ec.Entities[0]["new_paymentamount"] = 0; 92 if (ec.Entities[0].Attributes.Contains("new_sumamount")) 93 { 94 ec.Entities[0]["new_paymentamount"] = decimal.Parse(ec.Entities[0]["new_sumamount"].ToString()) - new_discountamount; 95 } 96 } 97 98 try 99 { 100 orgSvc.Update(ec[0]); 101 } 102 catch (Exception ex) 103 { 104 105 throw new InvalidPluginExecutionException(ex.ToString()); 106 } 107 } 108 109 110 } 111 112 113 } 114 } 115 }
标签:ide exception tor class tar ima 记录 跟踪 payment
原文地址:https://www.cnblogs.com/yaohongtao/p/12809781.html