Hello,
Read my previous post on Approval Process via Chatter Feed for better Understanding, where the importance of speeding up the Approval Process and the method of Approving records via Chatter Feed.
We all know that Email Service is an automated process that uses Apex class to do some process based on the email contents.
This post demonstrates another easy and speedy way of Providing Approvals (Approving/Rejecting) by sending mails and processing the Approval Process using the Apex Email Services. Based on the mail contents and Apex class logic, certain actions can be made which is the Approval Process in this post.
Let us assume that we have a Approval Process sending email alert to the Approver asking to Approve the record whenever a record is submitted for Approval, additionally the mail contains information about the record which is submitted for Approval and the Service Email Address to Reply To.
Once the Approver received this mail, he/she can send their Approval/Rejection/Recall just by sending a reply to the Service Email Address.
The Apex Class associated with this Service Address will then analyses the mail contents like Subject,Body and based on it Approves/Rejects/Recalls the Approval Process.
Create an Apex Class to process the inbound mail as below which operates on Contract Object,
This trigger takes the Contract Auto Number from the mail subject line and queries for the Contract record. Based on the tag in the email body <Approved> or <Rejected> or <Recalled>, the Approval Process was Approved or Rejected or Recalled.
To create email services, click
Checkout this link What is the Apex Email Service? , to know more about creating and defining a Email Service.
Screenshot of the mail received and the reply to the Service Address :
Screenshot of the Chatter in SFDC UI :
Read my previous post on Approval Process via Chatter Feed for better Understanding, where the importance of speeding up the Approval Process and the method of Approving records via Chatter Feed.
We all know that Email Service is an automated process that uses Apex class to do some process based on the email contents.
This post demonstrates another easy and speedy way of Providing Approvals (Approving/Rejecting) by sending mails and processing the Approval Process using the Apex Email Services. Based on the mail contents and Apex class logic, certain actions can be made which is the Approval Process in this post.
Let us assume that we have a Approval Process sending email alert to the Approver asking to Approve the record whenever a record is submitted for Approval, additionally the mail contains information about the record which is submitted for Approval and the Service Email Address to Reply To.
Once the Approver received this mail, he/she can send their Approval/Rejection/Recall just by sending a reply to the Service Email Address.
The Apex Class associated with this Service Address will then analyses the mail contents like Subject,Body and based on it Approves/Rejects/Recalls the Approval Process.
Create an Apex Class to process the inbound mail as below which operates on Contract Object,
global class ContractApproval implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope envelope) { Messaging.InboundEmailResult emailresult = new Messaging.InboundEmailResult(); system.debug(email.plainTextBody); if(email.subject.containsIgnoreCase('Contract Approval')) { String str = email.subject.normalizeSpace(); String ContractNumber = str.substring(str.indexOf('-')+1,str.length()); System.debug('ContractNumber :'+ContractNumber); Contract con = [Select Id,ContractNumber From Contract WHERE ContractNumber=:ContractNumber]; try { List<ProcessInstance> pi = [SELECT Id FROM ProcessInstance WHERE TargetObjectId=:con.Id AND Status='Pending']; if(pi.size()==0) return emailresult; List<ProcessInstanceWorkitem> piw = [SELECT Id,ActorId,ProcessInstanceId FROM ProcessInstanceWorkitem WHERE Actor.Email=:email.fromAddress and ProcessInstanceId=:pi[0].Id]; if(piw.size()==0) return emailresult; Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest(); if(email.plainTextBody.containsIgnoreCase('<approved>')) { req.setComments('Request Approved by Email Approval..!!'); req.setAction('Approve'); } else if(email.plainTextBody.containsIgnoreCase('<rejected>')) { req.setComments('Request Rejected by Email Approval..!!'); req.setAction('Reject'); } else if(email.plainTextBody.containsIgnoreCase('<recalled>')) { req.setComments('Request Recalled by Email Approval..!!'); req.setAction('Removed'); } req.setWorkitemId(piw[0].Id); Approval.ProcessResult result = Approval.process(req); FeedItem a = new FeedItem(ParentId=con.Id,Type='TextPost'); if(result.isSuccess()) { a.Body='Email received from Approver and the process was '+result.getInstanceStatus(); } else if(!result.isSuccess()) { a.Body='Email received from Approver but some error occured, please contact sys admin'; } a.Body+=email.plainTextBody; Insert a; } catch(Exception e) { system.debug(e); } } return emailresult; } }
To create email services, click
Checkout this link What is the Apex Email Service? , to know more about creating and defining a Email Service.
Screenshot of the mail received and the reply to the Service Address :
Screenshot of the Chatter in SFDC UI :
good text thank you sharing
ReplyDeletehello Sir i got an error when reply to the Email below the error , please let you tell me how can i fixed it ?
ReplyDeleteThe attached message was sent to the Email Service address but could not be processed because the following error occurred:
554 System.QueryException: List has no rows for assignment to SObject
Class.MyReq.TravelRequest.handleInboundEmail: line 12, column 1
Faisal,
ReplyDeleteThis is because the List SOQL WHERE condition is not met, and hence showing up this error.
When the incoming mail is received, i believe you are using SOQL statement with WHERE condition and assigning it to a List. But the incoming mail didnt satisfy the where condition and hence failed further processing
Let me know, if you need any further help on this.
Thanks,
Bharathi
This comment has been removed by the author.
ReplyDeletethank you for reply , but im not assign where clause into any list , simply i want to update or change my custom filed of custom object , here is the code if(email.subject.containsIgnoreCase('leave Approval'))
ReplyDelete{
String str = email.subject.normalizeSpace();
MyReq__leave__c con = [Select Id From MyReq__leave__c ];
try{ ...
so what i do to update status filed of this object MyReq__leave__c ?
Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
ReplyDeleteSalesforce Training
Your article on Apex Email Services is really informative. It assist me to understand whole concept with ease. Thanks for sharing.
ReplyDeleteReally a fantastic post, easy and quite helpful... Thanks for this wonderful blog...
ReplyDeleteKeep Posting....!!!
Nice post. Oracle is a relational database management system produced by oracle corporation. Nowadays most of the multinational companies used this oracle database for storing and managing their data's and programs. So learning Oracle Training in Chennai is one of the best idea to make a bright career.
ReplyDeleteCloud computing is storing and accessing the large data sets over the internet instead of your PC computer. So that you can manage the data and program anywhere through the internet.
ReplyDeleteRegards..
Cloud Training in Chennai
Can we please appreciate the knowledge of the fellow writer, and take our advertising of training to someplace else.
ReplyDeleteI believe such comments did stop him for posting further.
Nice Article! Mostly I have gathered knowledge from the blogger, because its provides more information over the books & here I can get more experienced skills from the professional, thanks for taking your to discussing this topic.
ReplyDeleteRegards,
Informatica training in chennai|Best Informatica Training In Chennai
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
ReplyDeleteRegards,
sas training in Chennai|sas training institutes in Chennai|sas training center in Chennai
Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
ReplyDeleteHadoop training in bangalore
Really awesome blog.
ReplyDeleteBig Data Hadoop Training In Chennai | Big Data Hadoop Training In anna nagar | Big Data Hadoop Training In omr | Big Data Hadoop Training In porur | Big Data Hadoop Training In tambaram | Big Data Hadoop Training In velachery
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
ReplyDeletejava training in chennai
java training in tambaram
aws training in chennai
aws training in tambaram
python training in chennai
python training in tambaram
selenium training in chennai
selenium training in tambaram
I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge.
ReplyDeletesap training in chennai
sap training in porur
azure training in chennai
azure training in porur
cyber security course in chennai
cyber security course in porur
ethical hacking course in chennai
ethical hacking course in porur
Thanks for sharing such informative post. Salesforce is a cloud based CRM product that allows users to create dynamic application and service over the cloud technology.
ReplyDeletehardware and networking training in chennai
hardware and networking training in omr
xamarin training in chennai
xamarin training in omr
ios training in chennai
ios training in omr
iot training in chennai
iot training in omr