Welcome guys to this blog.
This time we are going to discuss on the Email services in the salesforce. Salesforce provides the facility to create the new record using the Email service. We will see the example of creating case and case comment by using the Email service. To implement this functionality we need three thing as Apex Controller, Enabled Email Service, Apex Trigger on CaseComment object in any salesforce org. So let's start.
To create the email service we need a apex controller same as the the following.
**********************Controller************************************
global class emails implements Messaging.InboundEmailHandler{
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
string subject1 = email.subject;
string body = email.plainTextBody;
Attachment attachment = new Attachment();
string sender = email.fromAddress;
string st = body;
string cname = email.fromName;
string sub;
List<contact> contacts;
contacts = new List<contact>();
List<case> cases;
cases = new List<case>();
contacts =[select id, Email,Account.ID, Name from contact where Email =: sender limit 1];
if(subject1.contains('Re:')){
integer i = subject1.length();
sub = subject1.substring(4,i);
}else{
sub = subject1;
}
if(contacts.size()>0){
for(contact con : contacts){
if(con.id != null)
{
cases = [select id, CaseNumber from case];
for(case c : cases)
{
if(c.CaseNumber.contains(sub))
{
c.subject=subject1;
c.caseIdentifier__c = false;
c.Description=body;
c.Status='Working';
c.Origin='Email';
c.Severity__c='<-----None--->';
c.Reported_at__c=c.CreatedDate;
update c;
CaseComment cc = new CaseComment(ParentId=c.Id,CommentBody=st,IsPublished=true);
insert cc;
if(email.TextAttachments !=null){
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = c.Id;
insert attachment;
}
}
if(email.BinaryAttachments !=null){
for (Messaging.Inboundemail.BinaryAttachment ttAttachment : email.BinaryAttachments) {
attachment.Name = ttAttachment.fileName;
attachment.Body = ttAttachment.body;
attachment.ParentId = c.Id;
insert attachment;
}
}
return result;
}
}
if(con.Email == sender)
{
case cs = new case(contactID=con.ID,description=body,origin='email',Severity__c='None',status='new');
insert cs;
CaseComment cc1 = new CaseComment(ParentId=cs.Id,CommentBody=st.abbreviate(4000),IsPublished=true);
insert cc1;
}
}
}
}
else
{
contact c;
c = new contact();
c.LastName =cname;
c.subjects__C = 'java';
c.Email=sender;
insert c;
case cs1 = new case(contactID=c.ID,description=body,origin='email',status='new');
insert cs1;
}
return result;
}
}
********************************Controller************************************
After saving that controller we need to make the Email Service in the salesforce. To create the email service in the salesforce you have to move as setup -> App setUp -> develop -> Email service . Just click on the New Service . When you are going to create the email service you will see that there is an option to add the
Apex class. In that option we will add our above class. When you finished the process of creating the Email Service , you will get an address. Thar address should be added in gmail account as Forward Message which gmail account is being in the Salesforce org.
When you send email to this address a new Case and a new Contact will be created in the salesforce org. That contact will be associated that case.
This is the first step. In next step we will see that when we add a comment in Case comment of the salesforce, then an email will be sent to the contact who is associated with this case. That contact will receive an email . If that contact send any reply to that email , then new comment will be created in same case in the salesforce and the process is so on.
To implement the second step we need to write a trigger on the CaseComment object in salesforce.
***************************trigger*********************************************
trigger casetest on CaseComment (after insert) {
set<Id> caseId = new set<Id>();
set<Id> conId = new set<Id>();
set<Id> conId1 = new set<Id>();
list<CaseComment> lst = new list <CaseComment>();
Id contId;
string caseNo;
boolean flag;
string comment;
for(CaseComment cs : trigger.new){
caseId.add(cs.Id);
}
if(CaseId.size() > 0){
lst = [select id,commentbody,parentid from CaseComment where id in :caseId];
}
if(lst.size()>0){
for(CaseComment css: lst){
conId1.add(css.parentid);
comment = css.commentBody;
}
}
List<case> cse = new List<case>();
if(conId1.size() > 0){
List<case> cases = new List<case>();
cases = [select id, casenumber, contactId, caseIdentifier__c,description from case where id in : conId1];
if(cases.size() > 0){
for(case cs : cases){
system.debug('ajit'+ comment +'ajit1'+cs.description);
if(cs.description==null){
caseNo = cs.caseNumber;
contId = cs.contactId;
flag = true;
}
else{
if(comment.equals(cs.description)==false ){
caseNo = cs.caseNumber;
contId = cs.contactId;
flag = true;
}
}
}
}
}
if(flag == true){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject(caseNo);
mail.setTargetObjectId(contId);
mail.setreplyTo('my_email_services@2hpulp0pv93g3rbe2ng8dzw0ocs0wzyupuemgncw5cnhjoiffg.9-gjk7eaa.9.apex.salesforce.com');
mail.setSaveAsActivity(false);
mail.setHtmlBody(comment);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
So in above blog we saw that how to create a new case and casecomment in salesforce using Email service.
If you any doubt or issue then let me know.
This time we are going to discuss on the Email services in the salesforce. Salesforce provides the facility to create the new record using the Email service. We will see the example of creating case and case comment by using the Email service. To implement this functionality we need three thing as Apex Controller, Enabled Email Service, Apex Trigger on CaseComment object in any salesforce org. So let's start.
To create the email service we need a apex controller same as the the following.
**********************Controller************************************
global class emails implements Messaging.InboundEmailHandler{
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
string subject1 = email.subject;
string body = email.plainTextBody;
Attachment attachment = new Attachment();
string sender = email.fromAddress;
string st = body;
string cname = email.fromName;
string sub;
List<contact> contacts;
contacts = new List<contact>();
List<case> cases;
cases = new List<case>();
contacts =[select id, Email,Account.ID, Name from contact where Email =: sender limit 1];
if(subject1.contains('Re:')){
integer i = subject1.length();
sub = subject1.substring(4,i);
}else{
sub = subject1;
}
if(contacts.size()>0){
for(contact con : contacts){
if(con.id != null)
{
cases = [select id, CaseNumber from case];
for(case c : cases)
{
if(c.CaseNumber.contains(sub))
{
c.subject=subject1;
c.caseIdentifier__c = false;
c.Description=body;
c.Status='Working';
c.Origin='Email';
c.Severity__c='<-----None--->';
c.Reported_at__c=c.CreatedDate;
update c;
CaseComment cc = new CaseComment(ParentId=c.Id,CommentBody=st,IsPublished=true);
insert cc;
if(email.TextAttachments !=null){
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = c.Id;
insert attachment;
}
}
if(email.BinaryAttachments !=null){
for (Messaging.Inboundemail.BinaryAttachment ttAttachment : email.BinaryAttachments) {
attachment.Name = ttAttachment.fileName;
attachment.Body = ttAttachment.body;
attachment.ParentId = c.Id;
insert attachment;
}
}
return result;
}
}
if(con.Email == sender)
{
case cs = new case(contactID=con.ID,description=body,origin='email',Severity__c='None',status='new');
insert cs;
CaseComment cc1 = new CaseComment(ParentId=cs.Id,CommentBody=st.abbreviate(4000),IsPublished=true);
insert cc1;
}
}
}
}
else
{
contact c;
c = new contact();
c.LastName =cname;
c.subjects__C = 'java';
c.Email=sender;
insert c;
case cs1 = new case(contactID=c.ID,description=body,origin='email',status='new');
insert cs1;
}
return result;
}
}
********************************Controller************************************
After saving that controller we need to make the Email Service in the salesforce. To create the email service in the salesforce you have to move as setup -> App setUp -> develop -> Email service . Just click on the New Service . When you are going to create the email service you will see that there is an option to add the
Apex class. In that option we will add our above class. When you finished the process of creating the Email Service , you will get an address. Thar address should be added in gmail account as Forward Message which gmail account is being in the Salesforce org.
When you send email to this address a new Case and a new Contact will be created in the salesforce org. That contact will be associated that case.
This is the first step. In next step we will see that when we add a comment in Case comment of the salesforce, then an email will be sent to the contact who is associated with this case. That contact will receive an email . If that contact send any reply to that email , then new comment will be created in same case in the salesforce and the process is so on.
To implement the second step we need to write a trigger on the CaseComment object in salesforce.
***************************trigger*********************************************
trigger casetest on CaseComment (after insert) {
set<Id> caseId = new set<Id>();
set<Id> conId = new set<Id>();
set<Id> conId1 = new set<Id>();
list<CaseComment> lst = new list <CaseComment>();
Id contId;
string caseNo;
boolean flag;
string comment;
for(CaseComment cs : trigger.new){
caseId.add(cs.Id);
}
if(CaseId.size() > 0){
lst = [select id,commentbody,parentid from CaseComment where id in :caseId];
}
if(lst.size()>0){
for(CaseComment css: lst){
conId1.add(css.parentid);
comment = css.commentBody;
}
}
List<case> cse = new List<case>();
if(conId1.size() > 0){
List<case> cases = new List<case>();
cases = [select id, casenumber, contactId, caseIdentifier__c,description from case where id in : conId1];
if(cases.size() > 0){
for(case cs : cases){
system.debug('ajit'+ comment +'ajit1'+cs.description);
if(cs.description==null){
caseNo = cs.caseNumber;
contId = cs.contactId;
flag = true;
}
else{
if(comment.equals(cs.description)==false ){
caseNo = cs.caseNumber;
contId = cs.contactId;
flag = true;
}
}
}
}
}
if(flag == true){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject(caseNo);
mail.setTargetObjectId(contId);
mail.setreplyTo('my_email_services@2hpulp0pv93g3rbe2ng8dzw0ocs0wzyupuemgncw5cnhjoiffg.9-gjk7eaa.9.apex.salesforce.com');
mail.setSaveAsActivity(false);
mail.setHtmlBody(comment);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
So in above blog we saw that how to create a new case and casecomment in salesforce using Email service.
If you any doubt or issue then let me know.
No comments:
Post a Comment