1

I am working with webservice when i perform delete operation on contact at that time only contact is deleted the opportunity which belongs particular contact is not deleted may i know what i have to do.

My code is may i know where i have to do modifications

webservice static DelClass Method2(String cfName, String clName){
     DelClass res2 = new DelClass();
     res2.errorMessage = 'fnamelame  not exist!';
     List<Contact> lstContact = [Select Id, FirstName, LastName 
                                 From Contact 
                                 Where FirstName = :cfName 
                                 And LastName = :clName];
       if(lstContact.isEmpty()) return res2;
       delete lstContact;
       return null;
}
4
  • Opportunities attach to the Account, not to the Contact. Do you use Opportunity Contact Roles or have a custom field linking Contacts to your Opportunities?
    – Mike Chale
    Dec 9, 2013 at 15:36
  • 1
    @Mike Chale I used Opportunity Contact Roles while insert the contact and opportunity.
    – Sathya
    Dec 9, 2013 at 15:37
  • please add that additional information to your question.
    – Mike Chale
    Dec 9, 2013 at 16:16
  • If this is real code, you really need to work on your method names, 'Method2' is not an adequately descriptive name for an externally published service. Dec 27, 2013 at 11:33

1 Answer 1

4

Salesforce does not cascade delete Opportunities when a Contact in an Opportunity Contact Role is deleted. It just removes the Contact Role entry. To delete Opportunities related to the contact, you'd have to query the Opportunities with Opportunity Contact Roles to get the list of all Opportunities that need to be deleted.

delete [SELECT Id FROM Opportunity WHERE Id IN (SELECT OpportunityId FROM OpportunityContactRole WHERE ContactId IN :lstContact)];
2
  • 3
    delete [SELECT Id FROM Opportunity WHERE Id IN (SELECT OpportunityId FROM OpportunityContactRole WHERE ContactId IN :lstContact)]; ;)
    – eyescream
    Dec 9, 2013 at 16:10
  • Nice. I always get there eventually, but have to do it a few times. Dec 9, 2013 at 16:15

You must log in to answer this question.

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