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