+ Start a Discussion
Amer SkroboAmer Skrobo 

How to delete a single record from list

Hi I want to add some kind <apex:commandLink> to delete single record from a list. When I clik on "delete" it should be removed from a list or database. Do you have any ideas . I've tried something like this but it is not working. Or I don't know how to proper set it.

<apex:commandLink value="Delete" action="{! deleteUser}" >
                <apex:param name="var" value="{!st.id}" assignTo="{!var}"/>
            </apex:commandLink>

public void deleteUser(){
        
        tudent__c student = Database.query( 'SELECT Id FROM tudent__c WHERE Id=\''+newStudent.id+'\'' );
        Database.delete(student);
          
    }
 
Best Answer chosen by Amer Skrobo
Raj VakatiRaj Vakati
public String rId{get;set;}
    DataLoadTest__c delDlt = new DataLoadTest__c();
    public PageReference doDelete() {
        delDlt=[select Id from DataLoadTest__c where id =:rId];
        delete delDlt;
        //re-directing to a new page, please create this page "newdelete"
        pagereference ref=new pagereference('/apex/newdelete');
        ref.setredirect(true);
        return ref;
    }
 
<apex:commandlink value="Delete" action="{!doDelete}">
                          <apex:param name="rId" value="{!r.Id}" 
                           assignTo="{!rId}"/>
                      </apex:commandlink>

 

All Answers

Raj VakatiRaj Vakati
I think you are delete the record correcly but not rendering the page after delete ... 

Please check the database after deleteing .. 

And reredner the page again after deleteing 
Amer SkroboAmer Skrobo
When I press delete I get this result
The biggest problem here I think is how to forward ID from VisualForce page to Apex class ??
User-added image
Raj VakatiRaj Vakati
Refer this link 

https://salesforceradical.blogspot.com/p/deleting-particular-record-in-displayed.html
Raj VakatiRaj Vakati
public String rId{get;set;}
    DataLoadTest__c delDlt = new DataLoadTest__c();
    public PageReference doDelete() {
        delDlt=[select Id from DataLoadTest__c where id =:rId];
        delete delDlt;
        //re-directing to a new page, please create this page "newdelete"
        pagereference ref=new pagereference('/apex/newdelete');
        ref.setredirect(true);
        return ref;
    }
 
<apex:commandlink value="Delete" action="{!doDelete}">
                          <apex:param name="rId" value="{!r.Id}" 
                           assignTo="{!rId}"/>
                      </apex:commandlink>

 
This was selected as the best answer
Amer SkroboAmer Skrobo
Thank you so much you realy helped me