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学习笔记 系列汇总 -- 持续更新中



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   疯吻IT  阅读(2624)  评论(3编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2014-02-15 SharePoint 2013 APP 开发示例 (三)使用远程的web资源
点击右上角即可分享
微信分享提示