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 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 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





   



 
  

Dear {!recipient.FirstName}

you have a {!relatedTo.RecordType.Name} approval request.

Direct email reply approve rule:

Email first line: if agree reply (APPROVE, APPROVED, YES), else reply (REJECT, REJECTED, NO)

Email second line: add approve(or reject) Comments.

直接回复邮件的方法:

请在第一行里面输入:如果通过请输入(APPROVE, APPROVED, YES),拒绝请输入(REJECT, REJECTED, NO)

请在第二行里面输入您的(包括通过/拒绝)的理由.

------------

Or to approve or reject this item, click this link

{!Left($Api.Enterprise_Server_URL_190,(find('/services',$Api.Enterprise_Server_URL_190))) + relatedTo.id}

--------------------English Version------------------------
****************************
****************
Thank you

 

 

你可能感兴趣的:(salesforce)