function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lisa Haigy 37Lisa Haigy 37 

Delete failed. First exception on row 0 with id xxxxxxxxxxxxxxx; first error: ENTITY_IS_DELETED, entity is deleted: [] Error is in expression

I have a VFP that our reps use to record the outcome of any outbound calls they make on a contact.  There is a "call" button on the contact page that brings up the Call Visualforce page.  The Cancel button should close the VFP and return the user to the contact page and delete the call record. The error- Delete failed. First exception on row 0 with id xxxxxxxxxxxxxxxxxx; first error: ENTITY_IS_DELETED, entity is deleted: []  Error is in expression '{!callCancel}' in component <apex:commandButton> in page call: Class.CallExtension.callCancel: line 537, column 1
 
    public void callCancel() {
        //system.debug('****Cancelling***');
        Delete call;
 
Tommaso BolisTommaso Bolis
looks like you are trying to access a deleted entity
can you post the full code?
Lisa Haigy 37Lisa Haigy 37
Thanks for the response Tommaso.  I did remove some code that had to deal with campaigns so it may look a bit strange   
}
    public CallExtension(ApexPages.StandardController stdController) {
                //system.debug('###Controller###');
        Id conID = apexpages.currentpage().getparameters().get('conid');
        Id callID = apexpages.currentpage().getparameters().get('id');
        if (!Test.isRunningTest()) {
            stdController.addFields(Utility.fieldNames('Call__c'));
        }
        if (callID != null) {
            call = (Call__c)stdController.getRecord();   
            conID = call.Contact__c;
            keepOnCalling = call.Keep_Calling_on_this_Contact__c;
            callEdit = true;
        }
        else {
            con = [select id, firstname, fax, territory__c, otherstreet, othercity, otherstate, otherpostalcode, othercountry, mobilephone, mailingstreet, mailingcity, mailingstate, mailingpostalcode, mailingcountry, lastname, accountid, phone, email, role__c, program__c, recordTypeID, flow_url__c, program__r.In_Service__c, program__r.In_Service_Email_Text__c, program__r.talking_points__c, program__r.In_Services__c, Next_Attempt__c, program__r.Webinars__c, program__r.Call_Field_Set_Name__c, program__r.Flow_URL__c, program__r.Fulfillments__c, Name from Contact where id = : conid];
            call = new Call__c();
            call.Contact__c = conID;
            call.subject__c = con.program__r.In_Service__c;
            call.Description__c = con.program__r.In_Service_Email_Text__c;
            con.Next_Attempt__c = null;     
            callEdit = false;       
        }
        //system.debug('###ConID###:' + conID);
        Id currentUserID = UserInfo.getUserId();
        con = [select id, name, firstname, fax, territory__c, otherstreet, othercity, otherstate, otherpostalcode, othercountry, mobilephone, mailingstreet, mailingcity, mailingstate, mailingpostalcode, mailingcountry, lastname, accountid, phone, email, role__c, program__c, recordTypeID, flow_url__c, program__r.In_Service__c, program__r.In_Service_Email_Text__c, program__r.talking_points__c, program__r.In_Services__c, Next_Attempt__c, program__r.Webinars__c, program__r.Call_Field_Set_Name__c, program__r.Flow_URL__c, program__r.Fulfillments__c from Contact where id = : conid];
        talkingPoints = con.program__r.talking_points__c;
        //user u = [select id, Gotomeeting_Info__c from user where id =: currentUserID];
        //if (u.Gotomeeting_Info__c != null) call.Description__c += '\n \n' + u.Gotomeeting_Info__c;
        shipToFirstName = con.firstname;
        shipToLastName = con.lastname;
        shipToStreet = con.mailingstreet;
        shipToCity = con.mailingcity;
        shipToState = con.mailingstate;
        shipToZip = con.mailingpostalcode;
        shipTofax =  con.fax;
        newContact = new Contact();
        LoadKitOptions();
    }
 public List<SelectOption> getKOC() {
        List<SelectOption> options = new List<SelectOption>();
        if (call.Outcome__c == 'Target Reached' || call.Outcome__c == 'Schedule Call Back' || call.Outcome__c == null || call.Outcome__c == 'Left Voicemail' || call.Outcome__c == 'No Answer') {
            options.add(new SelectOption('','---Please Select---'));
            options.add(new SelectOption('Yes','Yes'));            
        }
        options.add(new SelectOption('No','No'));
        return options;
    }
    public void outcomeCheck() {
        system.debug('##Outcome##' + call.Outcome__c);
        showSave = true;
        if (call.Outcome__c == 'Not Interested' || call.Outcome__c == 'Do Not Call' || call.Outcome__c == 'Bad Number' || call.Outcome__c == 'Retired' || call.Outcome__c == 'Deceased') {
            call.Keep_Calling_on_this_Contact__c = 'No';
            system.debug('##SetToNo###');
        }
    }
    public PageReference dmlAction() {
        if (!callEdit) {
            insert call;
            system.debug('###call###' + call);
        }
        return null;
    }
    public List<wContact> getContacts() {
        if (contactList ==  null) {
            contactList = new List<wContact>();
            Boolean trueFalse;
            for (Contact c : [select id, name, firstname, lastname, email, role__c from Contact where accountid =: con.accountid order by lastname]) {
                if (con.id == c.id) trueFalse = true;
                contactList.add(new wContact(c,trueFalse,false));
                trueFalse = false;
            }            
        }
        return contactList;
    }
 public List<wCampaign> getCampaigns() {
    datetime rightNow = datetime.now(); 
        if (campaignList == null) {
            campaignList = new List<wCampaign>();
            for (Campaign c : [select id, name, Program__c, Speaker__c, Webinar_Date_Time__c,  StartDate, EndDate, Description from Campaign where program__c =: con.program__c and isactive = true and type = 'Webinar']) {
                campaignList.add(new wCampaign(c));
            }
        }
        return campaignList;
    }
    public List<wObjective> getObjectives() {
        Map<ID,Call_Contact_Objective__c> coCOOMap = new Map<ID,Call_Contact_Objective__c>();        
        if (callEdit) {
            for (Call_Contact_Objective__c cco : [select id, Contact_Objective__c, Status__c from Call_Contact_Objective__c where Call__c = : call.id]) {
                coCOOMap.put(cco.Contact_Objective__c,cco);
            }
        }
        if (objectiveList == null) {
            objectiveList = new List<wObjective>();
            for (Contact_Objective__c co : [select id, seq__c, Description__c, Objective__c, Objective_Status__c, Objective_Text__c from Contact_Objective__c where Contact__c =: con.id order by Objective_Text__c]) {
                if (coCOOMap.containsKey(co.id)) {
                    objectiveList.add(new wObjective(co,true,true,coCOOMap.get(co.id).id));
                }
                else {
                    objectiveList.add(new wObjective(co,true,false,null));
                }
            }
        }            
        return objectiveList;
    }
    public class wObjective {
        Public Boolean selected {get; set;}
        Public Boolean show {get; set;}
        public Contact_Objective__c obj {get; set;}
        Public String cco {get; set;}
        public wObjective (Contact_Objective__c co, Boolean bShow, Boolean isSelected, String ccoID) {
            selected = isSelected;
            show = bShow;
            obj = co;
            cco = ccoID;
        }
    }    
    public void newContact() {
        call.Create_Contact_Objectives__c = true;
        hideCallSave = true;
        showNewContact = true;
    }
    public void cancelNewContact() {
        showNewContact = false;
        hideCallSave = false;
    }
      public void newWebinarContact() {
        showNewWebinarContact = true;
    }
    public void cancelWebinarNewContact() {
        showNewWebinarContact = false;
    } 
    public void newWebinarContactSave() {
        showNewWebinarContact = false;
        newContact.accountid = con.accountid;
        newContact.Referred_By__c = con.id;
        newContact.program__c = con.program__c;
        newContact.mailingstreet = con.mailingstreet;
        newContact.mailingcity = con.mailingcity;
        newContact.mailingstate = con.mailingstate;
        newContact.mailingpostalcode = con.mailingpostalcode;
        newContact.mailingCountry = con.mailingCountry;
        newContact.otherstreet = con.otherstreet;
        newContact.othercity = con.othercity;
        newContact.otherstate = con.otherstate;
        newContact.otherpostalcode = con.otherpostalcode;
        newContact.otherCountry = con.otherCountry;
        newContact.Territory__c = con.Territory__c;
        if (newContactLastName == null || newContactLastName == '') {
            newContact.lastname = 'Unknown';
        }
        else {
            newContact.lastname = newContactLastName;
        }
        newContact.Create_Objectives__c = call.Create_Contact_Objectives__c;
        newContact.Referred_By__c = con.id;
        newContact.Contact_Origin__c = 'Other Contact';
        newContact.recordTypeID = con.recordTypeID;
        insert newContact;
        List<Contact_Objective__c> coList = new List<Contact_Objective__c>();
               if (call.Create_Contact_Objectives__c) {
                      for (Contact_Objective__c co : [select id, seq__c, Description__c, Objective__c, Objective_Status__c from Contact_Objective__c where Contact__c =: con.id]) {
                Contact_Objective__c coNew = new Contact_Objective__c();
                coNew = co.clone();
                coNew.Contact__c = newContact.id;
                coNew.Objective_Status__c = 'Not Started';
                coList.add(coNew);
            }
        }        
        */
        //system.debug('***newContact***' + newContact);
        contactList.add(new wContact(newContact,true,false));
        //system.debug('***contactList***' + contactList);
        newContact = new Contact();
        newContactLastName = null;
   }
    public void newISContact() {
        showNewISContact = true;
    }
    public void cancelISNewContact() {
        showNewISContact = false;
    }    
    public void newISContactSave() {
        showNewISContact = false;
        newContact.accountid = con.accountid;
        newContact.Referred_By__c = con.id;
        newContact.program__c = con.program__c;
        newContact.mailingstreet = con.mailingstreet;
        newContact.mailingcity = con.mailingcity;
        newContact.mailingstate = con.mailingstate;
        newContact.mailingpostalcode = con.mailingpostalcode;
        newContact.mailingCountry = con.mailingCountry;
        newContact.otherstreet = con.otherstreet;
        newContact.othercity = con.othercity;
        newContact.otherstate = con.otherstate;
        newContact.otherpostalcode = con.otherpostalcode;
        newContact.otherCountry = con.otherCountry;
        newContact.Territory__c = con.Territory__c;
        if (newContactLastName == null || newContactLastName == '') {
            newContact.lastname = 'Unknown';
        }
        else {
            newContact.lastname = newContactLastName;
        }
        newContact.recordTypeID = con.recordTypeID;
        newContact.Referred_By__c = con.id;
        newContact.Contact_Origin__c = 'Other Contact';
        newContact.recordTypeID = con.recordTypeID;        
        insert newContact;
        List<Contact_Objective__c> coList = new List<Contact_Objective__c>();
        if (call.Create_Contact_Objectives__c) {
 for (Contact_Objective__c co : [select id, seq__c, Description__c, Objective__c, Objective_Status__c from Contact_Objective__c where Contact__c =: con.id]) {
                Contact_Objective__c coNew = new Contact_Objective__c();
                coNew = co.clone();
                coNew.Contact__c = newContact.id;
                coNew.Objective_Status__c = 'Not Started';
                coList.add(coNew);
            }
        }        
        contactList.add(new wContact(newContact,true,false));
        newContact = new Contact();
        newContactLastName = null;
    }
    public void newContactSave() {
        system.debug('###In New Contact Save###');
        showNewContact = false;
        newContact.accountid = con.accountid;
        newContact.Referred_By__c = con.id;
        newContact.program__c = con.program__c;
        newContact.mailingstreet = con.mailingstreet;
        newContact.mailingcity = con.mailingcity;
        newContact.mailingstate = con.mailingstate;
        newContact.mailingpostalcode = con.mailingpostalcode;
        newContact.mailingCountry = con.mailingCountry;
        newContact.otherstreet = con.otherstreet;
        newContact.othercity = con.othercity;
        newContact.otherstate = con.otherstate;
        newContact.otherpostalcode = con.otherpostalcode;
        newContact.otherCountry = con.otherCountry;
        newContact.Territory__c = con.Territory__c;
        if (newContactLastName == null || newContactLastName == '') {
            newContact.lastname = 'Unknown';
        }
        else {
            newContact.lastname = newContactLastName;
        }
       //if (con.recordTypeID != null) newContact.recordTypeID = con.recordTypeID;
        newContact.Referred_By__c = con.id;
        newContact.Contact_Origin__c = 'Other Contact';
        newContact.recordTypeID = con.recordTypeID;        
        newContact.Program_Result__c = 'Not Started';
        insert newContact;
        List<Contact_Objective__c> coList = new List<Contact_Objective__c>();
        if (call.Create_Contact_Objectives__c) {
 for (Contact_Objective__c co : [select id, seq__c, Description__c, Objective__c, Objective_Status__c from Contact_Objective__c where Contact__c =: con.id]) {
                Contact_Objective__c coNew = new Contact_Objective__c();
                coNew = co.clone();
                coNew.Contact__c = newContact.id;
                coNew.Objective_Status__c = 'Not Started';
                coList.add(coNew);
            }
        }
        */
        call.spoke_to__c = newContact.id;
        insert coList;
        hideCallSave = false;
        newContactID = newContact.id;
        newContactName = newContact.Firstname + ' ' + newContact.LastName;
        newContactNewObjectives = newContact.Create_Objectives__c;
        system.debug('###NCID###' + newContactID);        
        newContact = new Contact();
        newContactLastName = null;
    }
        }
    public void checkOnSave() {
        system.debug('***CheckOnSave***');
        showSave = true;
   }
    public void newSave() {
  ccoCount = 0;
        List<Call_Contact_Objective__c> cooList = new List<Call_Contact_Objective__c>();
        for (wObjective wo : objectiveList) {
            if (wo.selected && wo.show) {
                if (wo.cco != null) {
                    Call_Contact_Objective__c coo = new Call_Contact_Objective__c(Id=wo.cco);
                    coo.Status__c = wo.obj.Objective_Status__c;
                    //coo.call__c = call.id;
                    //coo.Contact_Objective__c = wo.obj.id;
                    cooList.add(coo);
                    update wo.obj;
                }
                else {
                    Call_Contact_Objective__c coo = new Call_Contact_Objective__c();
                    coo.Status__c = wo.obj.Objective_Status__c;
                    coo.call__c = call.id;
                    coo.Contact_Objective__c = wo.obj.id;
                    cooList.add(coo);
                    update wo.obj;
                }
            }
        }

        if (cooList != null && cooList.size() > 0) {
            upsert cooList;
            ccoCount = cooList.size();            
        }
        system.debug('***Count***' + ccoCount);
        goodSave = true;
        call.Next_Attempt__c = con.Next_Attempt__c;
        call.Keep_Calling_on_this_Contact__c = keepOnCalling;
        //if (call.spoke_to__c == null) call.spoke_to__c = con.id;
        update call;
        con.Last_Activity_Date__c = date.today();
        con.Last_Call_Outcome__c = call.Outcome__c;
        if (call.Outcome__c == 'Target Reached') con.Last_Successful_Call_Date__c = date.today();
        update con;
    }
    public void callCancel() {
        //system.debug('****Cancelling***');
        Delete call;
    }    
   public List<Schema.FieldSetMember> getCallFields() {
        String fieldSetName = con.program__r.Call_Field_Set_Name__c;
        Map<String,Schema.FieldSet> schemaMap = Schema.SObjectType.Call__c.fieldSets.getMap();
        if (schemaMap.containsKey(fieldSetName)) {
            List<Schema.FieldSetMember> fields = Schema.SObjectType.Call__c.fieldSets.getMap().get(fieldSetName).getFields();
            return fields;            
        }
        else {
            return null;
        }
    }
    public void exclusionCheck() {
 ccoCount = 1;
    Set<ID> programObjectiveSet = new Set<ID>();
        Set<ID> excludedObjectiveSet = new Set<ID>();
        for (wObjective wo : objectiveList) {
            if (wo.selected == true) {
                programObjectiveSet.add(wo.obj.Objective__c);
            }
        }
        for (Objective_Exclusions__c oe : [select id, Program_Objective__c, Excluded_Program_Objective__c from Objective_Exclusions__c where Program_Objective__c in : programObjectiveSet]) {
            //system.debug(Utility.debugMarker('Loop'));
            excludedObjectiveSet.add(oe.Excluded_Program_Objective__c);
        }
        for (wObjective wo : objectiveList) {
            if (excludedObjectiveSet.contains(wo.obj.Objective__c)) {
                wo.show = false;
            }
            else {
                wo.show = true;
            }
        }