Salesforce 邮件服务

功能 根据邮件回复内容进行审批或者字段更新等

新建Email Service (配置允许的邮件地址等,新建Email Addresses) 并且编写后台Apex 

global class ******EmailService implements Messaging.InboundEmailHandler{

	global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
														   Messaging.InboundEnvelope envelope) {

		//Finance Flag and SOC Flag
		Boolean Finance = False;
		Boolean SOC = False;
		Boolean GM = False;
		Boolean Technical = False;
        Boolean FinanceVP = False;

		Boolean isReply = true; // REPLY SUCCESS EMAIL OR NOT
		final String[] lstReviewResult = new String[]{'Approved', 'Rejected'};
		Integer intReviewRes = -1; // -1=Error ; 0=Approved ; 1=Rejected ;

		Messaging.InboundEmailResult EmailResult = new Messaging.InboundEmailresult();
		String userEmail = email.fromAddress;
		String emailBody =
				( email.plainTextBody != null ? email.plainTextBody : email.htmlBody.replaceAll('<[^>]*>', '\n'));

		if (email.subject.contains('[#RVID:') && String.IsNotBlank(emailBody)) {
			// get Opportunity Id
			String OpportunityId = email.subject.substring(
							email.subject.indexOf('[#RVID:') + 7,
					email.subject.indexOf('#]')
			);

			Opportunity opp = [
					SELECT id,
							*************
					FROM Opportunity
					WHERE id =: OpportunityId
			];


			List<User> userList = [select id, Email, Name, LastName, FirstName, Profile.Id from User where Email =: userEmail and IsActive=true];
			Profile FinanceProfile = [select id, name from Profile where name='Finance'];
			if (userList[0].Profile.Id == FinanceProfile.Id) {
				Finance = TRUE;
			}
         
            Profile FinanceVPProfile = [select id, name from Profile where name='Finance VP'];
			if (userList[0].Profile.Id == FinanceVPProfile.Id) {
				FinanceVP = TRUE;
			}

		
			User SOCUser = [select id, Email, Name, LastName, FirstName, BMO_specialist__r.Email from User where id =:opp.Owner.BMO_specialist__c];
			if (userList[0].Id == SOCUser.Id) {
				SOC = TRUE;
			}

			User GMUser = [select id, Email from User where id =:opp.Owner.Contract_Approver__c];
			if (userList[0].Id == GMUser.Id) {
				GM = TRUE;
			}

			Profile technicalProfile = [select id, name from Profile where name='Technical'];
			if (userList[0].Profile.Id == technicalProfile.Id) {
				Technical = TRUE;
			}

			User user = userList[0];
			if (user != null) {
				// get Email Content and clear empty line
				List<String> lines = emailBody.split('\n');
				for (integer i = 0; i < lines.size() ; i++) {
					if (String.IsBlank(lines[i]))
						lines.remove(i);
				}
				String reviewResult = lines[0].trim();
				String reviewComments = lines[1];

				if (reviewResult.equalsIgnoreCase('APPROVE') || reviewResult.equalsIgnoreCase('APPROVED') ||
						reviewResult.equalsIgnoreCase('YES') || reviewResult.equalsIgnoreCase('Approved') ||
						reviewResult.equalsIgnoreCase('Approve') || reviewResult.equalsIgnoreCase('Yes')) {
					intReviewRes = 0;
				} else if (reviewResult.equalsIgnoreCase('REJECT') || reviewResult.equalsIgnoreCase('REJECTED') ||
						reviewResult.equalsIgnoreCase('NO') || reviewResult.equalsIgnoreCase('Reject') ||
						reviewResult.equalsIgnoreCase('Rejected') || reviewResult.equalsIgnoreCase('No')) {
					intReviewRes = 1;
				}

				// update opportunity value
				if (intReviewRes >= 0 && Finance && opp.Finance_Approval_Status__c == 'Pending') {
					opp.Finance_Approval_Status__c = lstReviewResult[intReviewRes];
					opp.Finance_Dept_Comments__c = reviewComments;
				} else if (intReviewRes >= 0 && SOC && opp.SOC_Dept__c == 'Pending') {
					opp.SOC_Dept__c = lstReviewResult[intReviewRes];
					opp.SOC_Dept_Comments__c = reviewComments;
				} else if (intReviewRes >= 0 && GM && opp.GM_approval_Status__c == 'Pending') {
					opp.GM_approval_Status__c = lstReviewResult[intReviewRes];
					opp.GM_approval_Comments__c = reviewComments;
				} else if (intReviewRes >= 0 && Technical && opp.Technical_Dept_Status__c == 'Pending') {
					opp.Technical_Dept_Status__c = lstReviewResult[intReviewRes];
					opp.Technical_Dept_Comments__c = reviewComments;
				} else if (opp.Legal_Dept__c ==  'Pending'){
					opp.Legal_Dept__c = lstReviewResult[intReviewRes];
					opp.Legal_Dept_Comments__c = reviewComments;				
                }else if(intReviewRes >= 0 && opp.VP_Approval_Status__c == 'Pending' && FinanceVP){
                    opp.VP_Approval_Status__c = lstReviewResult[intReviewRes];
					opp.VP_Approval_comments__c = reviewComments;
                }

			}

			// reyply the procss result to reviewer
			String repSubject = '[noreply]you have seccess to review.';
			String repBody = 'Dear ' + user.FirstName + ' ' + user.LastName + ':\n' +
					'You have successfully to review the Opportunity[' + OpportunityId + ']\n' +
					'Thank you!';

			//update opp
			Database.SaveResult dbRes = null;
			if (user != null && intReviewRes != -1 && String.IsNotBlank(OpportunityId)) {
				dbRes = Database.update(opp);
			}

			if (user != null && isReply && dbRes != null && dbRes.isSuccess()) {
				if (user.id != '005900000015hNj') {
					CommonUtils.SendBasicMail(new String[]{userEmail}, repSubject, repBody, null);
				}
			} else {
				//String failureReason = '';
				repSubject = '[noreply]you have failed to review.';
				repBody = 'Dear ' + user.FirstName + ' ' + user.LastName + ':\n' +
						'You have failed to review the Opportunity[' + OpportunityId + ']\n' +
					// 'Reasons for the failure :'+ failureReason +' \n'+
						'Thank you!';
				CommonUtils.SendBasicMail(new String[]{userEmail}, repSubject, repBody, null);
			}
		}



		return EmailResult;
	}

}

将邮件服务中的Email Address 存储为label ,新建对应的邮件模板 在邮件模板中使用该label

<messaging:emailTemplate replyTo="{!$Label.EmailServiceAdress}"  subject="Destination Region is {!relatedTo.Region__c},Please review this {!relatedTo.RecordType.Name} [#RVID:{!relatedTo.ID}#]" recipientType="User" relatedToType="Opportunity">
<messaging:htmlEmailBody >
<html>

 <script language="javascript">   

   var stringObj="{!relatedTo.Account.business_License__c}";   

   var newstr=stringObj.replace("&amp;","&");   

  

   </script>  



 <head>
  <p>Dear {!recipient.FirstName}</p>

<p>you have a {!relatedTo.RecordType.Name} approval request.</p>

<p>Direct email reply approve rule:</p>
<p>Email first line: if agree reply (APPROVE, APPROVED, YES), else reply (REJECT, REJECTED, NO)</p>
<p>Email second line: add approve(or reject) Comments.</p>
<p>直接回复邮件的方法:</p>
<p>请在第一行里面输入:如果通过请输入(APPROVE, APPROVED, YES),拒绝请输入(REJECT, REJECTED, NO)</p>
<p>请在第二行里面输入您的(包括通过/拒绝)的理由.</p>
<p>------------</p>
<p>Or to approve or reject this item, click this link</p>
<p>{!Left($Api.Enterprise_Server_URL_190,(find('/services',$Api.Enterprise_Server_URL_190))) + relatedTo.id}</p>

 </head>
  <body style="margin:10px auto;font-size:10px">
--------------------English Version------------------------
        <div >
<table border="0" width="100%"  style="border-collapse: collapse;margin:8px auto;font-size:15px" > 
****************************

</table>


<table border="1" width="100%"  style="border-collapse: collapse;margin:8px auto;font-size:15px" >

<apex:repeat value="{!relatedTo.Payment__r}" var="pay">
****************
</apex:repeat>

</table>         

</body>
<foot>
Thank you

</foot>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值