0

I have two S2S integrated orgs which are sharing Lead's records succefully. I have implemented the same using Apex and Salesforce to Salesforce Connections. My requirement is if a record is deleted from Source org, that record (already shared through S2S integration) should automatically delete from target org using Email Services. I have created a trigger and class in Source org which is sending the mail after record deletion. However the record is not being deleted from Target org. I have written an Email Handler class and called the same in Email Service at Target org, however I think there is some issue with the Email Handler class with respect to mapping. Kindly suggest.

Source Org's Trigger-

trigger EmailAfterDelete on Lead(after delete) { 

      HelperLeadTriggerNew.sendEmail(trigger.old); // Calling Helper class

}

Source Org's Helper Class-

public with sharing class HelperLeadTriggerNew {
public static List <Lead> sendEmail(List<Lead> lds){


List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for (Lead ld : lds) {    
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    email.setToAddresses(new String[] {'[email protected]'});
    email.setSubject('Deleted Lead Alert Lead: ' + ld.Name + '');
    email.setPlainTextBody('This message is to alert you that the Lead named ' + ld.Name + ' has been deleted.');
    emails.add(email);
}
Messaging.sendEmail(emails);

return lds;
 }
}

Till now everything is working fine.

Target Org's Email Handler Class

global class myHandler implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
      Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
      return result;
  }
 public static List <Lead> sendEmail(List<Lead> lds){

    List<Lead> ld = new List<Lead>();
for(Lead l : lds)
{
    Lead led = new Lead();
    led.Name = alName;
    ld.add(led);
}


delete ld;    
return null;    
  }
}

Debug Logs at Target Org-

23:36:18.049 (49930877)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000001QqZA|emailToLead.handleInboundEmail
23:36:18.051 (51494513)|METHOD_ENTRY|[1]|01p28000001QqZA|emailToLead.emailToLead()
23:36:18.051 (51506131)|METHOD_EXIT|[1]|emailToLead
23:36:18.052 (52620646)|SYSTEM_METHOD_ENTRY|[5]|System.debug(ANY)
23:36:18.052 (52657955)|USER_DEBUG|[5]|DEBUG|abc Deleted Lead Alert Lead: Test 54
23:36:18.052 (52668252)|SYSTEM_METHOD_EXIT|[5]|System.debug(ANY)
23:36:18.053 (53002823)|SOQL_EXECUTE_BEGIN|[6]|Aggregations:0|SELECT id FROM Lead WHERE Name = :tmpVar1 LIMIT 1
23:36:18.062 (62675940)|SOQL_EXECUTE_END|[6]|Rows:0
23:36:18.062 (62970324)|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject

Class.emailToLead.handleInboundEmail: line 6, column 1
23:36:18.062 (62987385)|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject
2
  • I can see you have a method named sendEmail(List<Lead> lds) on the inbound email handler. But where is that method getting called? Also how do you identintify which leads are to be deleted on the inbound email handler? are you using some regex to do it?
    – Ranga
    Commented May 25, 2015 at 9:08
  • Thanks for your reply.... I need to identify and delete the leads which have the same "Name" in both orgs. I know there are lots of issues in the Ema handler class. Could you please help me with a sample code for the same so that I can try it in my org. Commented May 25, 2015 at 9:18

1 Answer 1

0

In email subject you can give lead name as well. And in handleInboundEmail() method you can retrieve lead name from subject:

global class myHandler implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {

    String leadName = email.subject;
    Lead leadToDelete = [Select id from Lead where Name IN :leadName];
    Delete leadToDelete;

    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    return result;
    }
}

If you don't want to give lead name in subject, then you can extract lead name from body using string methods and then do the same as above.

Hope this may help.

19
  • Thanks alot for your help.... I tried your code however, when checked in debug logs, the Subject is picking up the Lead Name but it is not deleting the same. It shows Name as ' SELECT Name FROM Account WHERE Name = :tmpVar1 ' Commented May 25, 2015 at 11:42
  • Yes you got right, there is a small syntax issue. This must work: Lead leadToDelete = [Select id from Lead where Name = :leadName];
    – Nebula
    Commented May 25, 2015 at 11:45
  • I already made that correction but its giving the same error and record is not getting deleted. Please help. Commented May 26, 2015 at 6:06
  • Can you please give the code & error which you are getting.
    – Nebula
    Commented May 26, 2015 at 6:11
  • Heres the code- "global class emailToLead implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { String LeadName = email.subject; Lead ldToDelete = [Select id from Lead where Name = :LeadName]; System.debug ('abc' + ldToDelete); Delete ldToDelete; System.debug ('xyz' + ldToDelete); Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); return result; } }" and the debug log says "SELECT id FROM Lead WHERE Name = :tmpVar1" Commented May 26, 2015 at 6:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .