Here am using Salesforce provided email services, where we can perform Dml processes on the basis of mails coming to the salesforce Org.
Salesforce generates its own email on which if user sends email, it is received by the associated Salesforce org.
To perform this whole action, need to create a EmailHandler class.
Here is the emailHandler class "UpdateAccountFromEmail"
/**
* Description : Update Account's phone using Inbound Messages.
*
* Created By : Abhi Tripathi
*
* Version : V1.0
*
* Revision Log : 10/01/2013 Created
**/
global class UpdateAccountFromEmail implements Messaging.InboundEmailHandler {
//Method to recieve email
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
//Instance of InboundEmailResult
Messaging.Inboundemailresult result = new Messaging.Inboundemailresult();
try{
//Strings
String phone = '';
String name = '';
//Text body
String accountTextBody = email.plainTextBody;
//Loop
for(String newStr : accountTextBody.split('\r\n')) {
//Get name values
name = newStr.substringAfter('Name:');
name = name.substringBefore('Phone:');
//Get phone value
phone = newStr.substringAfter('Phone:');
//Remove spaces
name = name.trim();
phone = phone.trim();
//List
List<string> phoneNumber = new List<string>();
//Loop through the String
for(String phn : phone.split('\n') ) {
phoneNumber.add(phn);
}
//Assigning value
phone = phoneNumber[0];
}
//Check fo name value
if(name != null || name != '') {
//Query on account
List<account> account = [Select Id From Account Where Name =: name];
System.debug('##### value of account' + account);
//Check for list size
if(account.size() != 0) {
//Assigning value to phone field
account[0].Phone = phone;
update account;
} else {
//If name is not found then create a new Account
Account acc = new Account();
acc.Name = name;
acc.Phone = phone;
insert acc;
}
}
//Set success to true
result.success = true;
}
//Excetion
catch(Exception e){
result.success=false;
result.Message='Unable to update your "Phone Number", Please try again.';
}
return result;
}
}
Now we need to create our "Email Service"
Go to
Setup --> Develop --> Email Service
do as in below, in Apex Class field populate the name of the emailHandler class as marked, left Accept Email From blank, Click Save.
Now we can see the salesforce have generated an email id to which User can send email to Salesforce, as marked below
Now compose a mail, any of the email service as we had left the "Accept Email From" field blank, that will allow all the email services. Compose your email like this
After sending the above mail your Account will have a new record named "Test", or if you have already an Account named Test then it will update its phone field.
Awesome....gud goin dude... :) :)
ReplyDelete