1.引用类库程序集
using Microsoft.Xrm.Sdk.Messages;
2.创建实例
ExecuteTransactionRequest requestToSaveRecords = new ExecuteTransactionRequest()
{
Requests = new OrganizationRequestCollection(),
ReturnResponses = true,
};
3.查询方法
IOrganizationService orgService = OrgServiceUtil.Client;
//单条查询
var oldEntity = orgService.Retrieve("实体名", 实体ID, new ColumnSet(true));
//多条查询
QueryExpression boxdetailquery = new QueryExpression("实体名")
{
NoLock = true,
ColumnSet = new ColumnSet(true),
Criteria =
{
Conditions=
{
new ConditionExpression("foton_box",ConditionOperator.Equal,item),
new ConditionExpression("statecode",ConditionOperator.Equal,0)
}
}
};
var boxs = orgService.RetrieveMultiple(boxdetailquery);
4.创建新增记录
//实体记录所有字段复制实现
Entity newEntity = oldEntity.Copy();
Guid new_id = Guid.NewGuid();
//重置字段
newEntity.Id = new_id;
newEntity["detailid"] = new_id;//Id
//两个选项字段的赋值
newEntity["isrepeat"] = true;
//选项集字段的赋值
newEntity["shipvia"] = new OptionSetValue(shipvia);
//浮点数字段赋值,为0需转换一下double类型,否则会报错
newEntity["amount"] = Convert.ToDouble(0);
//查找类型字段赋值为空
newEntity["command"] = null;
//查找类型字段赋值
newEntity["command1"] = new EntityReference("实体名",userId);
//状态类型字段赋值
newEntity["state"] = new OptionSetValue(0);
requestToSaveRecords.Requests.Add(new CreateRequest() { Target = newEntity });
5.更新实体记录
var entity = new Entity("实体名", Id);
entity["shipvia"] = new OptionSetValue(shipvia);
entity["status"] = new OptionSetValue(136020013);
entity["shippingboxs"] = "";
entity["command"] = null;
requestToSaveRecords.Requests.Add(new UpdateRequest() { Target = entity });
6.删除实体记录
var entityReference = new EntityReference("实体名", new Guid(item.itemid));
requestToSaveRecords.Requests.Add(new DeleteRequest() { Target = entityReference });
7.提交执行,当其中有执行失败的,不会提交。
orgService.Execute(requestToSaveRecords);
标签:回滚,requestToSaveRecords,newEntity,Dynamic,实体,改查,entity,new,true From: https://www.cnblogs.com/jiangyuhu/p/17705159.html