+ Start a Discussion

Opportunity is not deleted




      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;



The Opportunity does not have a standard field that is a Master-Detail lookup to a Contact.  This is the only way that Opportunities could be deleted automatically when a Contact is deleted.  In fact, the Opportunity object does not lookup to the Contact object at all.  It does look up to an Account, and all Contacts will also lookup to an Account.


Does your org have a custom field that associates an Opportunity to a specific Contact?  If so, you could query for all Opportunities whose Contact lookup field equals the ID of the Contact(s) you are about to delete, and delete those Opportunities.  Let's assume that the lookup field is called, 'Contact__c'.  The following code should work:


webservice static DelClass Method2( String cfName, String clName )
    DelClass res2 = new DelClass();
    res2.errorMessage = 'Contact does not exist!';

    List<Contact> lstContact = [SELECT Id FROM Contact WHERE FirstName = :cfName and LastName = :clName];
    if ( lstContact.isEmpty() ) return res2;

    delete [SELECT Id FROM Opportunity WHERE Contact__c IN :lstContact]
    delete lstContact;
    return null;


If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them.  Thanks!