+ Start a Discussion
NervosaNervosa 

Deleting an item by ID on a VF page

Greetings to everyone!

 

On my VF i have a commandlink and it looks like this:

        <apex:commandLink value="Delete" action="{!del}">
            <apex:param name="idtodel" value={!pitem.merchandise.id}"/>
        </apex:commandLink>

 In my Apex controller i want to implement deletion like this:

    public PageReference del() {
    Id IdToDel=System.currentPageReference().getParameters().get('id');
    Item__c ItemToDel = [SELECT all
                         FROM Item__c
                         WHERE id = IdToDel];
    PageReference curPage = ApexPages.currentPage(); 
    curPage.getParameters().put('success','true');
        curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
        curPage.setRedirect(true);
        return curPage; 
            
    }

and i get an error "unexpected token: 'IdToDel' at line 7 column 40"

Best Answer chosen by Admin (Salesforce Developers) 
BharathimohanBharathimohan

Hi Nervosa,

 

Use colon (:) before your varaible,

 

Item__c ItemToDel = [SELECT all
                         FROM Item__c
                         WHERE id = :IdToDel];

 

use colone (:)IdToDel

 

Please mark this post as solved, if it helps you

 

Regards,

Bharathi
Salesforce For All

 

 

All Answers

NervosaNervosa

My VF page:

<apex:page standardStylesheets="false" showHeader="false" sidebar="false" Controller="FullFunctionality">
<apex:stylesheet value="{!URLFOR($Resource.Styles, 'styles.css')}"/>
<apex:form style="align:left">
<apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even" align="center">

    <apex:column style="width:100">
        <apex:commandLink value="Delete" action="{!del}">
            <apex:param name="idtodel" value="{!pitem.merchandise.id}"/>
        </apex:commandLink>
    </apex:column>
    <apex:column headerValue="Item name" style="text-align:left">
        <apex:outputText value="{!pitem.merchandise.name}"/>
    </apex:column>
    <apex:column headerValue="Price" >
        <apex:outputText value="{!pitem.merchandise.Item_Price__c}"/>
    </apex:column>
    <apex:column headerValue="Date of adding" >
        <apex:outputText value="{0,date,MM/dd/yyyy}">
            <apex:param value="{!pitem.merchandise.createddate}"/>
        </apex:outputText>
    </apex:column>    
</apex:dataTable>
<!-- </apex:form>

<apex:form >
<apex:messages /> -->
<apex:pageBlock > 
    <apex:pageBlockSection columns="1" >
        <apex:inputText value="{!NewItemName}" label="Name"/>            
            <apex:selectList id="types" size="1" required="true" label="Type" >
                <apex:selectOptions value="{!types}"/>
            </apex:selectList>
            <apex:inputText value="{!NewItemPrice}" label="Price"/>
            <apex:inputText value="{!NewItemQuantity}" label="Quantity"/>
            <apex:inputText value="{!NewItemReleaseDate}"  label="Release Date"/>
        <apex:commandButton value="Add item" action="{!add}" />
    </apex:pageBlockSection>
</apex:pageBlock>        
</apex:form>
</apex:page>

 My Apex controller:

public class FullFunctionality {

    public PageReference del() {
        Id IdToDel = System.currentPageReference().getParameters().get('id');
        Item__c ItemToDel = [SELECT all
                             FROM Item__c
                             WHERE id = IdToDel];
        PageReference curPage = ApexPages.currentPage(); 
        curPage.getParameters().put('success','true');
        curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
        curPage.setRedirect(true);
        return curPage; 
            
    }

    public String NewItemName {get; set;}
    public Integer NewItemQuantity { get; set; }
    public Integer NewItemPrice { get; set; }
    public String NewItemType { get; set; }
    public Date NewItemReleaseDate { get; set; }

    public List<SelectOption> getTypes(){
        List<SelectOption> types = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Item__c.Item_Type__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry f : ple)
           {
              types.add(new SelectOption(f.getLabel(), f.getValue()));
           }       
        return types;
    }
    
public PageReference add(){

Item__c NewItem = new Item__c(            
            Name = NewItemName,
            Item_Price__c = NewItemPrice,
            Items_Available__c = NewItemQuantity,
            Item_Type__c = NewItemType,
            Release_Date__c = NewItemReleaseDate);
            insert NewItem; 
            PageReference curPage = ApexPages.currentPage(); 
            curPage.getParameters().put('success','true');
            curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
            curPage.setRedirect(true);
            return curPage;
}
    
    DisplayMerchandise[] products;

    public class DisplayMerchandise {
        public Item__c merchandise { get; set; }
        public DisplayMerchandise(Item__c item) {
            this.merchandise = item;
        }
    }

    public DisplayMerchandise[] getProducts() {
        if (products == null) {
            products = new DisplayMerchandise[]{};
            for (Item__c item : 
                [SELECT id, name, item_price__c, createddate, item_type__c 
                 FROM Item__c ]) {
               products.add(new DisplayMerchandise(item));
             }
         }
     return products;      
    }
}

 

 

BharathimohanBharathimohan

Hi Nervosa,

 

Use colon (:) before your varaible,

 

Item__c ItemToDel = [SELECT all
                         FROM Item__c
                         WHERE id = :IdToDel];

 

use colone (:)IdToDel

 

Please mark this post as solved, if it helps you

 

Regards,

Bharathi
Salesforce For All

 

 

This was selected as the best answer
harsha__charsha__c

Hi Nervosa,

 

I'm just adding one more point to Bharathi

 

Id IdToDel = System.currentPageReference().getParameters().get('id');

 This line should be changed to the followed as , You taken the param name in the page as " idtodel " 

Id IdToDel = System.currentPageReference().getParameters().get('idtodel');

 

 

 

 

 

NervosaNervosa

THANK YOU VERY VERY MUCH =)

 

But there is one more little question i can't find answer to. How can i select ALL fields in a query? In SQL it's simply a ' * '. And how can i do it here?

harsha__charsha__c

Hi Nervosa,

 

In SOQL, we cant use ' * ' to query all fields like SQL

 

1. Specify all the fields manually in the query

 

2. use fieldset concept and add all fields to fieldset

 

The usage of fieldsets for querying all the fields goes as followed..!

 

String query = 'SELECT ';
for(Schema.FieldSetMember f : SObjectType.Merchandise__c.FieldSets.Dimensions.getFields()) 
{
    query += f.getFieldPath() + ', ';
}
query += 'Id, Name FROM Merchandise__c LIMIT 1';
List<Merchandise__c> lst = new List<Merchandise__c>(Database.query(query));

 

NervosaNervosa

Thanks for answers.

Not to create another thread i ask here again.

 

Now my controller looks this way:

public class FullFunctionality {

    public String NewItemName {get; set;}
    public Integer NewItemQuantity { get; set; }
    public Integer NewItemPrice { get; set; }
    public String NewItemType { get; set; }
    public Date NewItemReleaseDate { get; set; }
    public String IdToDel { get; set; }
    

    public PageReference del() {

        //String IdToDel = System.currentPageReference().getParameters().get('idtodel');
        Item__c ItemToDel = [SELECT id
                             FROM Item__c
                             WHERE id = :IdToDel];
        delete ItemToDel;                     
        PageReference curPage = ApexPages.currentPage(); 
        curPage.getParameters().put('success','true');
        curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
        curPage.setRedirect(true);
        return curPage;
            
    }

    public List<SelectOption> getTypes(){
        List<SelectOption> types = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Item__c.Item_Type__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry f : ple)
           {
              types.add(new SelectOption(f.getLabel(), f.getValue()));
           }       
        return types;
    }
    
public PageReference add(){

Item__c NewItem = new Item__c(            
            Name = NewItemName,
            Item_Price__c = NewItemPrice,
            Items_Available__c = NewItemQuantity,
            Item_Type__c = NewItemType,
            Release_Date__c = NewItemReleaseDate);
            insert NewItem; 
            PageReference curPage = ApexPages.currentPage(); 
            curPage.getParameters().put('success','true');
            curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
            curPage.setRedirect(true);
            return curPage;
}
    
    DisplayMerchandise[] products;

    public class DisplayMerchandise {
        public Item__c merchandise { get; set; }
        public DisplayMerchandise(Item__c item) {
            this.merchandise = item;
        }
    }

    public DisplayMerchandise[] getProducts() {
        if (products == null) {
            products = new DisplayMerchandise[]{};
            for (Item__c item : 
                [SELECT id, name, item_price__c, createddate, item_type__c 
                 FROM Item__c ]) {
               products.add(new DisplayMerchandise(item));
             }
         }
     return products;      
    }
}

 And my VF page:

<apex:page standardStylesheets="false" showHeader="false" sidebar="false" Controller="FullFunctionality">
<apex:stylesheet value="{!URLFOR($Resource.Styles, 'styles.css')}"/>
<apex:form style="align:left">
<apex:pageBlock >
<apex:pageBlockSection columns="2">
<apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even" align="center">

    <apex:column style="width:100">
        <apex:commandButton value="Delete" action="{!del}">
            <apex:param name="idtodel" value="{!pitem.merchandise.id}" assignTo="{!IdToDel}"/>
        </apex:commandButton>
    </apex:column>
    <apex:column headerValue="Item name" style="text-align:left" >
        <apex:outputText value="{!pitem.merchandise.name}"/>
    </apex:column>
    <apex:column headerValue="Price" >
        <apex:outputText value="{!pitem.merchandise.Item_Price__c}"/>
    </apex:column>
    <apex:column headerValue="Date of adding" >
        <apex:outputText value="{0,date,MM/dd/yyyy}">
            <apex:param value="{!pitem.merchandise.createddate}"/>
        </apex:outputText>
    </apex:column>    
</apex:dataTable>
<!--</apex:pageBlock>
<apex:pageBlock > -->
    <apex:pageBlockSection columns="1"  >
        <apex:inputText value="{!NewItemName}" label="Name"/>            
            <apex:selectList id="types" size="1" required="true" label="Type" >
                <apex:selectOptions value="{!types}"/>
            </apex:selectList>
            <apex:inputText value="{!NewItemPrice}" label="Price"/>
            <apex:inputText value="{!NewItemQuantity}" label="Quantity"/>
            <apex:inputText value="{!NewItemReleaseDate}"  label="Release Date"/>
        <apex:commandButton value="Add item" action="{!add}" />
    </apex:pageBlockSection>
</apex:pageBlockSection>
</apex:pageBlock>        
</apex:form>
</apex:page>

 But when i click any of commandButtons to perfom deletion i get an error: "System.QueryException: List has no rows for assignment to SObject

Error is in expression '{!del}' in component <apex:page> in page fullfunctionalityinventory

"

harsha__charsha__c

Did you check whether " IdToDel " value is populated with value being sent from the page..?

NervosaNervosa

Finally i've found the solution. The only thing i needed to do is to put CommandLink instead of CommandButton.

Now my VF page is:

<apex:page standardStylesheets="false" showHeader="false" sidebar="false" Controller="FullFunctionality">
<apex:stylesheet value="{!URLFOR($Resource.Styles, 'styles.css')}"/>
<apex:form style="align:left">
<apex:pageBlock >
<apex:pageBlockSection columns="2">
<apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even" align="center">

    <apex:column style="width:100">
        <apex:commandLink value="Delete" action="{!del}">
            <apex:param name="idtodel" value="{!pitem.merchandise.id}" assignTo="{!IdToDel}"/>
        </apex:commandLink>
    </apex:column>
    <apex:column headerValue="Item name" style="text-align:left" >
        <apex:outputText value="{!pitem.merchandise.name}"/>
    </apex:column>
    <apex:column headerValue="Price" >
        <apex:outputText value="{!pitem.merchandise.Item_Price__c}"/>
    </apex:column>
    <apex:column headerValue="Date of adding" >
        <apex:outputText value="{0,date,MM/dd/yyyy}">
            <apex:param value="{!pitem.merchandise.createddate}"/>
        </apex:outputText>
    </apex:column>    
</apex:dataTable>
<!--</apex:pageBlock>
<apex:pageBlock > -->
    <apex:pageBlockSection columns="1"  >
        <apex:inputText value="{!NewItemName}" label="Name"/>            
            <apex:selectList id="types" size="1" required="true" label="Type" >
                <apex:selectOptions value="{!types}"/>
            </apex:selectList>
            <apex:inputText value="{!NewItemPrice}" label="Price"/>
            <apex:inputText value="{!NewItemQuantity}" label="Quantity"/>
            <apex:inputText value="{!NewItemReleaseDate}"  label="Release Date"/>
        <apex:commandButton value="Add item" action="{!add}" />
    </apex:pageBlockSection>
</apex:pageBlockSection>
</apex:pageBlock>        
</apex:form>
</apex:page>

 And my Apex controller:

public class FullFunctionality {

    public String NewItemName {get; set;}
    public Integer NewItemQuantity { get; set; }
    public Integer NewItemPrice { get; set; }
    public String NewItemType { get; set; }
    public Date NewItemReleaseDate { get; set; }
    public String IdToDel { get; set; }
    
    public PageReference del() {

        Item__c ItemToDel = [SELECT id
                             FROM Item__c
                             WHERE id = :IdToDel];
        delete ItemToDel;                     
        PageReference curPage = ApexPages.currentPage(); 
        curPage.getParameters().put('success','true');
        curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
        curPage.setRedirect(true);
        return curPage;
            
    }

    public List<SelectOption> getTypes(){
        List<SelectOption> types = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Item__c.Item_Type__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry f : ple)
           {
              types.add(new SelectOption(f.getLabel(), f.getValue()));
           }       
        return types;
    }
    
public PageReference add(){

Item__c NewItem = new Item__c(            
            Name = NewItemName,
            Item_Price__c = NewItemPrice,
            Items_Available__c = NewItemQuantity,
            Item_Type__c = NewItemType,
            Release_Date__c = NewItemReleaseDate);
            insert NewItem; 
            PageReference curPage = ApexPages.currentPage(); 
            curPage.getParameters().put('success','true');
            curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
            curPage.setRedirect(true);
            return curPage;
}
    
    DisplayMerchandise[] products;

    public class DisplayMerchandise {
        public Item__c merchandise { get; set; }
        public DisplayMerchandise(Item__c item) {
            this.merchandise = item;
        }
    }

    public DisplayMerchandise[] getProducts() {
        if (products == null) {
            products = new DisplayMerchandise[]{};
            for (Item__c item : 
                [SELECT id, name, item_price__c, createddate, item_type__c 
                 FROM Item__c ]) {
               products.add(new DisplayMerchandise(item));
             }
         }
     return products;      
    }
}