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學習筆記 系列匯總 -- 持續更新中


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM