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
Akanksha sharma 15Akanksha sharma 15 

When i clicked on record on delete commandlink record is not deleted

 

my scenario is when i will click on new opportunity button a new opportunity page should open where i fill the details and click on save then it will redirect to the list page of opportunity where records save with edit and delete link when click on edit link edit page should open i have done till that but when i click on delete link it should deleted i am not abe to do for delete when i am deleting record a new bank page open
 

    <apex:page StandardController="Opportunity" Extensions="secondpage" >
        <apex:form >
        <apex:pageBlock >
    
        <apex:pageBlockButtons location="Top" >
         <apex:commandButton value="Save" action="{!saverec}"/> <br/><br/>
         
         <apex:commandLink action="{!editOpp}" value="Edit"/>
      <apex:param value="{!opp.id}" assignTo="{!selectedAccId}" name="selectedAccId"/>
      <apex:commandLink action="{!deleteOpp}" value="Delete" />
     <apex:param value="{!opp.id}" assignTo="{!selectedAccId}" name="selectedAccId"/>
        </apex:pageBlockButtons>
        <apex:pageblockSection >
           <apex:inputfield value="{!opp.Name}" />
            <apex:inputfield value="{!opp.StageName}"/>
             <apex:inputfield value="{!opp.AccountId}"/>
             <apex:inputfield value="{!opp.Amount}"/>
            <apex:inputfield value="{!opp.closeDate}"/>
       </apex:pageblockSection>
                </apex:pageBlock>   
        </apex:form>
    </apex:page>

----

    public class secondpage {
    
       
        Public Opportunity opp{get;set;}
        Public String op1;
         public id accid{set;get;}
       public String selectedAccId {get;set;}
          public String selectedoppId{get;set;}
        public secondpage(ApexPages.StandardController stdController) {
            
       
            Opp= (Opportunity)stdController.getRecord();
               
            
        }
       
       
        public PageReference saverec() {
           
            insert opp;
           
            pagereference pr = new pagereference('https://c.ap6.visual.force.com/apex/Opp2');
           
            return pr;
        }
    
    
        public Pagereference editOpp()
     {
         
           PageReference ref = new PageReference('/'+selectedAccId+'/e?retURL=%2F'+selectedAccId);
            return ref;
     }
     
     public Pagereference deleteOpp()
     {
            return null;
    
            }
     }

----

    <apex:page Controller="mycontroller1">
    <apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection >
    <apex:pageBlockTable value="{!Opportunitylst}" var="op">
    <apex:column value="{!op.Name}"/>
    <apex:column value="{!op.StageName}"/>
    <apex:column value="{!op.Amount}" />
    <apex:column value="{!op.accountId}"/>
    <apex:column value="{!op.CloseDate}"/>
    <apex:column headerValue="Action" >
      <apex:commandLink value="Edit /" action="{!editOpp}">
        <apex:param value="{!op.id}" assignTo="{!selectedAccId}" name="selectedAccId"/>
        </apex:commandLink>
        
        <apex:commandLink action="{!deleteOpp}" value="Delete"/>
     <apex:param value="{!op.id}" assignTo="{!selectedAccId}" name="selectedAccId"/>
        
        
    </apex:column>                        
    </apex:pageBlockTable>
    </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
    </apex:page>

----
                        
    public class mycontroller1 {
    
        public List<Opportunity> Opp{get;set;}
        Public List<Opportunity> Opportunitylst{get;set;}
        public String selectedAccId {get;set;}
    
    
      public PageReference deleteOpp() {
              
opportunitylst=[select Name,AccountId,Amount,CloseDate,StageName from Opportunity where id=:selectedAccId];      
delete  opportunitylst;     
  return null;
              
              }
    
                 
                 
                 
        public mycontroller1(){
            Opportunitylst=[select Name,AccountId,Amount,CloseDate,StageName from Opportunity order by createdDate DESC limit 10 ];
         }
        public Pagereference editOpp()
     {
         p
           PageReference ref = new PageReference('/'+selectedAccId+'/e?retURL=%2F'+selectedAccId);
            return ref;
     }
    }

 
Raj VakatiRaj Vakati
Update you're my controller page as shown below 
<apex:column headerValue="Action" >
                        <apex:commandLink value="Edit /" action="{!editOpp}">
                            <apex:param value="{!op.id}" assignTo="{!selectedAccId}" name="selectedAccId"/>
                        </apex:commandLink>
                        
                        <apex:commandLink action="{!deleteOpp}" value="Delete">
                            <apex:param value="{!op.id}" assignTo="{!selectedAccId}" name="selectedAccId"/>
                        </apex:commandLink>  
                            
                        </apex:column>
Controller delete method
public PageReference deleteOpp() {
        
        Opportunity temp=[select Name,AccountId,Amount,CloseDate,StageName from Opportunity where id=:selectedAccId];      
        delete  temp;     
        return null;
        
    }

 
Akanksha sharma 15Akanksha sharma 15
i did but i am getting this error

List has no rows for assignment to SObject
Error is in expression '{!deleteOpp}' in page opp2: Class.mycontroller1.deleteOpp: line 10, column 1
An unexpected error has occurred. Your development organization has been notified.
Sandeep YadavSandeep Yadav
Hi Akansha 
Update your delete button code with this--
<apex:commandLink action="{!deleteOpp}" value="Delete">
    <apex:param value="{!op.id}" assignTo="{!selectedAccId}" name="selectedAccId"/>
</apex:commandLink>

its working in my org.
I think you should do this using Wrapper Class Concept.
Akanksha sharma 15Akanksha sharma 15
ya i update but it again giving error 
List has no rows for assignment to SObject
Sandeep YadavSandeep Yadav
this error comes when your query doesn't return anything.
but your query seems to correct.
let's check the query result in debug log
Shubham saini 14Shubham saini 14

Hello Akanksha,
 

Update deleteOpp method in controller

public PageReference deleteOpp() {
              
opportunitylst=[select Name,AccountId,Amount,CloseDate,StageName from Opportunity where id=:selectedAccId];      
delete  opportunitylst;     
PageReference retURL = new PageReference('/apex/VFPName');
retURL.setRedirect(true);
return retURL;

}


Hope, it will help you.

Thank You