Guest User

Untitled

a guest
Jan 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. global class ConvertLeadAction {
  2. @InvocableMethod(label='Convert Leads')
  3. global static List<ConvertLeadActionResult> convertLeads(List<ConvertLeadActionRequest> requests) {
  4. List<ConvertLeadActionResult> results = new List<ConvertLeadActionResult>();
  5. for (ConvertLeadActionRequest request : requests) {
  6. results.add(convertLead(request));
  7. }
  8. return results;
  9. }
  10.  
  11. public static ConvertLeadActionResult convertLead(ConvertLeadActionRequest request) {
  12. Database.LeadConvert lc = new Database.LeadConvert();
  13. lc.setLeadId(request.leadId);
  14. lc.setConvertedStatus(request.convertedStatus);
  15.  
  16. if (request.accountId != null) {
  17. lc.setAccountId(request.accountId);
  18. }
  19.  
  20. if (request.contactId != null) {
  21. lc.setContactId(request.contactId);
  22. }
  23.  
  24. if (request.overWriteLeadSource != null && request.overWriteLeadSource) {
  25. lc.setOverwriteLeadSource(request.overWriteLeadSource);
  26. }
  27.  
  28. if (request.createOpportunity != null && !request.createOpportunity) {
  29. lc.setDoNotCreateOpportunity(!request.createOpportunity);
  30. }
  31.  
  32. if (request.opportunityName != null) {
  33. lc.setOpportunityName(request.opportunityName);
  34. }
  35.  
  36. if (request.ownerId != null) {
  37. lc.setOwnerId(request.ownerId);
  38. }
  39.  
  40. if (request.sendEmailToOwner != null && request.sendEmailToOwner) {
  41. lc.setSendNotificationEmail(request.sendEmailToOwner);
  42. }
  43.  
  44. Database.LeadConvertResult lcr = Database.convertLead(lc, true);
  45. if (lcr.isSuccess()) {
  46. ConvertLeadActionResult result = new ConvertLeadActionResult();
  47. result.accountId = lcr.getAccountId();
  48. result.contactId = lcr.getContactId();
  49. result.opportunityId = lcr.getOpportunityId();
  50. return result;
  51. } else {
  52. throw new ConvertLeadActionException(lcr.getErrors()[0].getMessage());
  53. }
  54. }
  55.  
  56. global class ConvertLeadActionRequest {
  57. @InvocableVariable(required=true)
  58. global ID leadId;
  59.  
  60. @InvocableVariable(required=true)
  61. global String convertedStatus;
  62.  
  63. @InvocableVariable
  64. global ID accountId;
  65.  
  66. @InvocableVariable
  67. global ID contactId;
  68.  
  69. @InvocableVariable
  70. global Boolean overWriteLeadSource;
  71.  
  72. @InvocableVariable
  73. global Boolean createOpportunity;
  74.  
  75. @InvocableVariable
  76. global String opportunityName;
  77.  
  78. @InvocableVariable
  79. global ID ownerId;
  80.  
  81. @InvocableVariable
  82. global Boolean sendEmailToOwner;
  83. }
  84.  
  85. global class ConvertLeadActionResult {
  86. @InvocableVariable
  87. global ID accountId;
  88.  
  89. @InvocableVariable
  90. global ID contactId;
  91.  
  92. @InvocableVariable
  93. global ID opportunityId;
  94. }
  95.  
  96. class ConvertLeadActionException extends Exception {}
  97. }
Add Comment
Please, Sign In to add comment