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
trikkutrikku 

How To delete the Account Related contacts Through "DELETECONTACT" Button on Account page

Hai,

 

I added a button "DELETECONTACT" to Account page. if i click on "DELETECONTACT" Button then i want to  delete the conatcts assosiated with that account.using Ajax..Is there any help

 

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

Code on custom button of the account object

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}

{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js")}

 

 

 

sforce.connection.sessionId="{!$Api.Session_ID}";

var confirmation1=confirm('do you want to delete contact');

if(confirmation1)

{

var returnstring=sforce.apex.execute("deletecontact","deletecon",{evntid:"{! Account.Id }"});

window.location.reload();

}

 

------------ Apex Controller ------------------

global class deletecontact

{

webservice static void deletecon(string accountid1)

{

    list<contact> con=[select id from contact where accountid=:accountid1];

    if(con.size()>0)

    {

    delete con;

    }

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

Code on custom button of the account object

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}

{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js")}

 

 

 

sforce.connection.sessionId="{!$Api.Session_ID}";

var confirmation1=confirm('do you want to delete contact');

if(confirmation1)

{

var returnstring=sforce.apex.execute("deletecontact","deletecon",{evntid:"{! Account.Id }"});

window.location.reload();

}

 

------------ Apex Controller ------------------

global class deletecontact

{

webservice static void deletecon(string accountid1)

{

    list<contact> con=[select id from contact where accountid=:accountid1];

    if(con.size()>0)

    {

    delete con;

    }

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
trikkutrikku

Thank u Ankit for your Valuble Time..

 

with little changes it works nicely

trikkutrikku

Hai Navthar,this works fine..

but I want this "DELETECONTACT" button works without class..can we write the query with in the button funcionalty without class..

 

 

Thanks in Advance..