当前位置: 代码迷 >> .NET相关 >> Dynamic CRM 2013学习笔记(三十四)自定义审批流五
  详细解决方案

Dynamic CRM 2013学习笔记(三十四)自定义审批流五

热度:341   发布时间:2016-04-24 02:56:28.0
Dynamic CRM 2013学习笔记(三十四)自定义审批流5

审批过程中,经常要求自动发邮件:审批中要通知下一个审批人进行审批;审批完通知申请人已审批完;被拒绝后,要通知已批准的人和申请人。下面详细介绍如何实现一个自动发邮件的插件:

 

1. 根据审批状态来确定要通知哪个人或哪个角色

  • 状态为2 - 审批中时,查找下一个审批人
/// <summary>
/// 下一个审批人
/// </summary>
/// <returns></returns>
private List<Guid> GetNextStepPerson()
{
//收件人
List<Guid> mailToList = new List<Guid>();
string fetchQuery = @"
<fetch mapping='logical' distinct='false'>
<entity name='crm_approve_activity'>
<attribute name='ownerid' />
<order attribute='createdon' descending='true' />
<filter type='and'>
<condition attribute='regardingobjectid' operator='eq' uitype='' value='{0}' />
<condition attribute='crm_approve_state' operator='eq' value='2' />
</filter>
</entity>
</fetch>"
;

FetchExpression fetchExp = new FetchExpression(string.Format(fetchQuery, entity.Id));
EntityCollection result = service.RetrieveMultiple(fetchExp);
if (result.Entities.Count == 0) return null;
foreach (Entity activity in result.Entities)
{
Guid ownerid = activity.GetAttributeValue<EntityReference>("ownerid").Id;
if (!mailToList.Contains(ownerid)) mailToList.Add(ownerid);
}
return mailToList;
}

 

  • 状态为3 - 审批通过时,查找申请人
/// <summary>
/// 获得提交人
/// </summary>
/// <returns></returns>
private List<Guid> GetSubmitPerson()
{
//收件人
List<Guid> mailToList = new List<Guid>();
string fetchQuery = @"
<fetch count='1' mapping='logical'>
<entity name='crm_approve_activity'>
<attribute name='ownerid' />
<order attribute='createdon' />
<filter type='and'>
<condition attribute='regardingobjectid' operator='eq' uitype='' value='{0}' />
<condition attribute='crm_approve_state' operator='eq' value='1' />
</filter>
</entity>
</fetch>"
;

FetchExpression fetchExp = new FetchExpression(string.Format(fetchQuery, entity.Id));
EntityCollection result = service.RetrieveMultiple(fetchExp);
if (result.Entities.Count == 0) return null;
foreach (Entity activity in result.Entities)
{
Guid ownerid = activity.GetAttributeValue<EntityReference>("ownerid").Id;
if (!mailToList.Contains(ownerid)) mailToList.Add(ownerid);
}
return mailToList;
}

 

 

  • 状态为4 - 审批拒绝时,查找审批过的人,以及申请人
/// <summary>
/// reject 后获得审批过的和单据创始人
/// </summary>
/// <returns></returns>
private List<Guid> GetApprovedPerson()
{
//收件人
List<Guid> mailToList = new List<Guid>();
string fetchQuery = @"
<fetch mapping='logical' distinct='false'>
<entity name='crm_approve_activity'>
<attribute name='ownerid' />
<order attribute='createdon' descending='true' />
<filter type='and'>
<condition attribute='regardingobjectid' operator='eq' uitype='' value='{0}' />
<condition attribute='crm_approve_state' operator='eq' value='1' />
</filter>
</entity>
</fetch>"
;

FetchExpression fetchExp = new FetchExpression(string.Format(fetchQuery, entity.Id));
EntityCollection result = service.RetrieveMultiple(fetchExp);
if (result.Entities.Count == 0) return null;
foreach (Entity activity in result.Entities)
{
Guid ownerid = activity.GetAttributeValue<EntityReference>("ownerid").Id;
if (!mailToList.Contains(ownerid)) mailToList.Add(ownerid);
}
return mailToList;
}

 

2. 定义邮件模板

Entity curEntity = service.Retrieve(entity.LogicalName, entity.Id,
new ColumnSet(m_Parameter, "ownerid"));

string subjectTxt = string.Empty;
string formNumber = curEntity.GetAttributeValue<string>(m_Parameter);
if (formNumber == null) formNumber = "";
string fName = entityDisplayName + "(" + formNumber + ")";

StringBuilder body = new StringBuilder();
body.AppendLine("<p>Dear {0},</p>");//收件人的 first name
string url = GetCRMServiceUrl() + "main.aspx?etn=" + entity.LogicalName
+ "&pagetype=entityrecord&id=%7B" + entity.Id + "%7D";
if (approvalStatus == 4)
{
subjectTxt = fName + " was rejected";
body.AppendLine(string.Format("For more information, please click on <a href='{0}' target='_blank'>{1}</a></p>", url, fName));
body.AppendLine("<p>Best Regards</p>");
body.AppendLine("<p>CRM</p>");
// txt = "Reject";
}
else if (approvalStatus == 3)
{
subjectTxt = fName + " has been completed";
body.AppendLine(string.Format("<p>{0} has been completed. For more information, please click on <a href='{1}' target='_blank'>{2}</a></p>", fName, url, fName));
body.AppendLine("<p>Best Regards</p>");
body.AppendLine("<p>CRM</p>");
// txt = "Completed";
}
else if (approvalStatus == 2)
{
subjectTxt = "Please review and approve " + fName;
body.AppendLine(string.Format("<p> Please review and approve the {0} on <a href='{1}' target='_blank'>{2}</a></p>", fName, url, fName));
body.AppendLine("<p>Thank you very much!</p>");
body.AppendLine("<p>Best Regards</p>");
body.AppendLine("<p>CRM</p>");
}

 

3. 创建邮件实体

List<ActivityParty> fromList = new List<ActivityParty>();
fromList.Add(new ActivityParty()
{
PartyId = new EntityReference(SystemUser.EntityLogicalName, context.UserId),
ParticipationTypeMask = new OptionSetValue(8)
});

foreach (Guid to in mailToList)
{
// to
List<ActivityParty> toList = new List<ActivityParty>();
toList.Add(new ActivityParty()
{
PartyId = new EntityReference(SystemUser.EntityLogicalName, to),
ParticipationTypeMask = new OptionSetValue(8)
});

Email email = new Email();
email.From = fromList;
email.To = toList;
//email.Cc = cclist;
email.RegardingObjectId = new EntityReference(curEntity.LogicalName, curEntity.Id);
email.ActualDurationMinutes = 30;
email.IsWorkflowCreated = false;
email.Subject = subjectTxt;
SystemUser user = new SystemUser();
user.Attributes = service.Retrieve(SystemUser.EntityLogicalName, to,
new ColumnSet("firstname")).Attributes;
email.Description = string.Format(body.ToString(), user.FirstName);
email.Id = service.Create(email);
SendMail(service, email.Id);
}

 

4. 发送邮件

SendEmailRequest sendEmailreq = new SendEmailRequest();
sendEmailreq.EmailId = mailId;
sendEmailreq.TrackingToken = "";
sendEmailreq.IssueSend = true;
SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);

 

 

5. 注册插件

image

 

6. 错误处理

有一次系统重置后,发邮件的插件报了一个错:Cannot open Sql Encryption Symmetric Key because Symmetric Key password does not exist in Config DB

解决方法:

依次打开Settings->Data management –> Data Encryption

image

image

然后在上面红框里填上任意一个key即可

 

大功告成!

 

Dynamic CRM 2013学习笔记 系列汇总 -- 持续更新中

1楼小小天狼星
学习了,同样在看Dynamic CRM相关的东西。多多请教。
  相关解决方案