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
Mike Tol 1Mike Tol 1 

Delete contact from table by id

Hi!
How can I delete definite contact from table by id?

Apex:
  @AuraEnabled
    public static void deleteContact(Id contactId) {        
        List<Contact> deleteContact = 
            [SELECT Id
            FROM Contact 
            WHERE Id = :contactId 
        ];
        delete deleteContact;        
   }

Js:
    handleDelete() {
        console.log("launch");        
        deleteContact()
            .then(result => {
                console.log(result, 'end')
                this.contacts = result;
            })
            .catch(error => {
                console.log(error, "err")
                this.error = error;                
            });
    } 
Best Answer chosen by Mike Tol 1
Naveen KNNaveen KN
Your code looks fine. But when the contact is associated with any other records you cannot perform the delete operation. Are you getting any errors with this code?

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Mike,

Refer the below link have similar kind of solution.
https://salesforcescool.blogspot.com/2021/11/delete-multiple-records-in-datatable-in.html

If this helps, Please mark it as best answer.

Thanks!!
Mike Tol 1Mike Tol 1
Thank you Ankaiah!  But I can't write code in the Apex method, not in Js
 
Naveen KNNaveen KN
Your code looks fine. But when the contact is associated with any other records you cannot perform the delete operation. Are you getting any errors with this code?
This was selected as the best answer
Mike Tol 1Mike Tol 1
Thank you Naveen KN! I was able to do it.
@AuraEnabled
    public static void deleteContact(Id recordId) {   
        List<Contact> contacts = 
        [SELECT Id FROM Contact WHERE Id = :recordId];
        delete contacts;        
   }

handleDelete() {    
        deleteContact({recordId: this.recordId})
            .then(result => {
                this.contacts = result;
            })
            .catch(error => {
                this.error = error;                
            });
    }