编写后台处理逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using RekTec.Crm.Common.Helper;
using RekTec.Crm.DataManagement;
using System.Data;

namespace RekTec.Crm.Workflow.Import
{
//继承ImportBase,类要为public,否则编译不会出错但是注册程序集会出错
public class ImportInsuranceprice : ImportBase
{
//传入的参数是从上传的Excel中提出的数据,
protected override void Import(DataTable dt, out IList<string> errors)
{
errors = new List<string>();
if (dt == null || dt.Rows.Count == 0)
{
return;
}
var successcount = 0;
var errorcount = 0;

for (var i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
try
{
#region 校验输入数据
//数据类型为数字的要用Cast.ConToDecimal,判断为空要用IsNullOrWhiteSpace
if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["物料名称"])))
{
throw new Exception("物料名称不能为空!");
}
var new_product_id = Cast.ConToString(dr["物料名称"]);

if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["物料编码"])))
{
throw new Exception("物料编码不能为空!");
}
var new_materialcode = Cast.ConToString(dr["物料编码"]);

if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["付款类型"])))
{
throw new Exception("付款类型不能为空");
}
var new_paymenttype = Cast.ConToString(dr["付款类型"]);

if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["首付比例(%)"])))
{
throw new Exception("首付比例(%)不能为空!");
}
var new_downpaymentsrat = Cast.ConToDecimal(dr["首付比例(%)"]);

if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["基准价格"])))
{
throw new Exception("基准价格不能为空!");
}
var new_standardprice = Cast.ConToDecimal(dr["基准价格"]);

if (string.IsNullOrWhiteSpace(Cast.ConToString(dr["所属组织"])))
{
throw new Exception("所属组织不能为空!");
}
var new_organisation_id = Cast.ConToString(dr["所属组织"]);
#endregion
#region 查询对应的数据是否存在
//查询使用QueryExpression的话只能用字段名,使用Broker.ExecuteDataTable查询的话可以按视图进行查询.
var query = new QueryExpression("new_vehiclebenchmark");
query.ColumnSet = new ColumnSet(new string[] { "new_standardprice" });
// query.Criteria.AddCondition("new_product_idName", ConditionOperator.Equal, new_product_id);
query.Criteria.AddCondition("new_materialcode", ConditionOperator.Equal, new_materialcode);
query.Criteria.AddCondition("new_downpaymentsrat", ConditionOperator.Equal, new_downpaymentsrat);
var result = OrganizationServiceAdmin.RetrieveMultiple(query);
#endregion
if (result != null && result.Entities != null && result.Entities.Count > 0)
{
throw new Exception("已存在重复数据,不允许导入");
}
#region 如果不存在 则新建
var createEntity = new Entity("new_vehiclebenchmark");
//物料名称
createEntity["new_product_id"] = GetEntityReference("product", "name", new_product_id, i, "物料名称");
//物料编码
createEntity["new_materialcode"] = new_materialcode;
//付款类型
var new_paymenttypes = GetOptions(OrganizationServiceAdmin, "new_vehiclebenchmark", "new_paymenttype");
createEntity["new_paymenttype"] = new OptionSetValue(new_paymenttypes[new_paymenttype]);
//首付比例
createEntity["new_downpaymentsrat"] = new_downpaymentsrat;
//基准价格
createEntity["new_standardprice"] = new_standardprice;
//所属组织
createEntity["new_organisation_id"] = GetEntityReference("businessunit", "name", new_organisation_id, i, "所属组织");
OrganizationService.Create(createEntity);
#endregion


successcount++;

}
catch (Exception ex)
{
errors.Add($"第{i + 1}行导入失败,原因:" + ex.Message + "\n");
errorcount++;
continue;
}
}

}

/// <summary>
/// 获取lookup字段的值
/// </summary>
/// <param name="entityname">要查询的实体名</param>
/// <param name="field"></param>
/// <param name="value"></param>
/// <returns></returns>
public EntityReference GetEntityReference(string entityname, string field, string value, int i, string text)
{
QueryExpression qe = new QueryExpression(entityname);
qe.ColumnSet.AddColumn(entityname + "id");
qe.Criteria.AddCondition(field, ConditionOperator.Equal, value);
if (entityname.ToLower() != "businessunit" && entityname.ToLower() != "systemuser")
{
qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
}
EntityCollection ec = OrganizationService.RetrieveMultiple(qe);
if (ec == null || ec.Entities.Count < 1)
throw new Exception($"第{i + 1}行创建失败,原因:本行[" + text + "]字段未能查询到对应的信息!\n");

return new EntityReference(entityname, (Guid)ec.Entities[0].Attributes[entityname + "id"]);
}
// <summary>
/// 获取选项集
/// </summary>
/// <param name="organizationService">服务</param>
/// <param name="entityName">实体信息</param>
/// <param name="attributeName">字段信息</param>
/// <returns></returns>
private Dictionary<string, int> GetOptions(IOrganizationService organizationService, string entityName, string attributeName)
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
QueryExpression queryExpression = new QueryExpression
{
EntityName = "stringmap"
};
queryExpression.ColumnSet.AddColumns(new string[]
{
"value",
"attributevalue"
});
queryExpression.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, new object[]
{
entityName
});
queryExpression.Criteria.AddCondition("attributename", ConditionOperator.Equal, new object[]
{
attributeName
});

queryExpression.AddOrder("attributevalue", OrderType.Ascending);
EntityCollection entityCollection = organizationService.RetrieveMultiple(queryExpression);
if (entityCollection != null && entityCollection.Entities != null && entityCollection.Entities.Count > 0)
{
foreach (Entity expr_10D in entityCollection.Entities)
{
int attributeValue = expr_10D.GetAttributeValue<int>("attributevalue");
string attributeValue2 = expr_10D.GetAttributeValue<string>("value");
dictionary.Add(attributeValue2, attributeValue);
}
}
return dictionary;
}
}
}

导入按钮

Homepage指的是视图页面

1
2
3
4
5
6
//按钮id
new_vehiclebenchmark.HomepageGrid.new_vehiclebenchmark.MainTab.Actions.ImportLine
//16×16../img/image
$webresource:new_/imgs/ribbon/new_import_16.png
//32×32../img/image
$webresource:new_/imgs/ribbon/new_import.png

image-20210713115644860

1
2
3
4
5
//函数名
rtcrm.showImportWindow
//引用文件
$webresource:new_/js/rtcrm.min.js
//value的值为实体的名

image-20210713115835382

创建流程

创建流程,流程名称为要为英文,创建之后就是唯一名称new_后边的不能更改,创建完之后显示的流程名称可以改.

image-20210719172122523

添加参数

输入参数

名称 类型 必须
ParentEntityId 字符串 可选
ParentEntityName 字符串 可选
ImportLogId 字符串 必需

输出参数

名称 类型 必须
Message 字符串 可选

image-20210719172456565

选择后台写好的逻辑流

image-20210719173227435

设置导入传参和分派值传参(分派值在这里是返回的信息message)

image-20210719174931259

注意这里的message的值选择

这里将选择的后台逻辑文件的步骤名称命名为”导入

微信截图_20210721144654

则在为message选值时,查找里边选择的值也为”导入

微信截图_20210721144801

然后点击添加→确定

微信截图_20210721144817

错误提示

如果上边的分派值像选择步骤一样直接选择参数 ,如下

image-20210719175047057

则在导入的时候会出现错误:”未将对象引用设置到对象的实例

微信截图_20210721144454

创建数据导入

导入名称

实体名称:要导入数据的实体的名称,和创建按钮的时候的传值一样.

导入动作:选择上边创建的导入流程

模板:选择导入的数据模板,保存之后在注释中添加附件.

image-20210719193157597