Guest User

Untitled

a guest
Nov 20th, 2017
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
  2. Messaging.InboundEnvelope env) {
  3.  
  4. // Create an InboundEmailResult object for returning the result of the
  5. // Apex Email Service
  6. Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
  7.  
  8. //use default assignment rule
  9. Database.DMLOptions dmo = new Database.DMLOptions();
  10. dmo.assignmentRuleHeader.useDefaultRule = true;
  11.  
  12. try {
  13. //select lead
  14. Lead k = this.get_recent_lead(email.fromAddress);
  15.  
  16. //select contact
  17. Contact c = this.get_contact(email.fromAddress);
  18.  
  19. //brand new mail?
  20. //Boolean first_touch = (k != null) & (c != null);
  21.  
  22. //not a contact, not a lead? create a new one!
  23. //if (k != null && c != null) {
  24.  
  25. //create new lead
  26. k = new Lead(
  27. firstname = this.getContactFirstname(email.fromName),
  28. lastname = this.getContactLastname(email.fromName),
  29. company = this.getContactCompany(email.fromAddress),
  30. email = email.fromAddress,
  31. //email_origin__c = env.fromAddress,
  32. leadsource = 'bant.io',
  33. Description = email.plainTextBody
  34. //lead_source_detail__c = 'joinus@wework.com'
  35. );
  36.  
  37. //use lead assignment rules
  38. k.setOptions(dmo);
  39.  
  40. //dml
  41. insert k;
  42.  
  43. //re-query
  44. k = [ SELECT id, ownerId, isConverted, convertedContactId, convertedAccountId
  45. FROM Lead
  46. WHERE id = :k.id
  47. LIMIT 1
  48. ];
  49. // }
  50.  
  51. //collect what
  52. Id whatId = null;
  53.  
  54.  
  55.  
  56. }
  57. catch(Exception pEx) {
  58. result.success = false; //default value
  59. system.debug(pEx.getMessage());
  60. throw new Email2LeadException(pEx.getMessage());
  61. }
  62.  
  63. return result;
  64. }
  65.  
  66.  
  67. private Lead get_recent_lead(String pEmail) {
  68. Lead[] Ks = [ SELECT id, email, isConverted, convertedContactId, convertedContact.ownerId, ownerId, convertedAccountId
  69. FROM Lead
  70. WHERE email = :pEmail
  71. ORDER BY createddate DESC
  72. LIMIT 1
  73. ];
  74.  
  75. return Ks.size() > 0 ? Ks[0] : null;
  76. }
  77.  
  78.  
  79. private Contact get_contact(String pEmail) {
  80. Contact[] Ks = [ SELECT id, AccountId, ownerId
  81. FROM Contact
  82. WHERE email = :pEmail
  83. ORDER BY createddate DESC
  84. LIMIT 1
  85. ];
  86.  
  87. return Ks.size() > 0 ? Ks[0] : null;
  88. }
  89.  
  90.  
  91.  
  92. private String getContactFirstname(String pFrom) {
  93. String result;
  94. try {
  95. result = pFrom.substring(0,pFrom.indexOf(' '));
  96. }
  97. catch(Exception pEx) {
  98. result = ''; //default value
  99. }
  100.  
  101. return result;
  102. }
  103.  
  104.  
  105. private String getContactLastname(String pFrom) {
  106. String result;
  107. try {
  108. result = pFrom.indexOf(' ') > 0 ?
  109. pFrom.substring(pFrom.indexOf(' ')) :
  110. pFrom;
  111. }
  112. catch(Exception pEx) {
  113. result = ''; //default value
  114. }
  115.  
  116. return result;
  117. }
  118.  
  119.  
  120. private String getContactCompany(String pEmail) {
  121. String result;
  122. try {
  123. result = pEmail.indexOf('@') > 0 ?
  124. pEmail.substring(pEmail.indexOf('@') + 1) :
  125. pEmail;
  126. }
  127. catch(Exception pEx) {
  128. result = ''; //default value
  129. }
  130.  
  131. return result;
  132. }
  133.  
  134.  
  135. public class Email2LeadException extends Exception{}
Add Comment
Please, Sign In to add comment