You need to sign in to do that
Don't have an account?
Dynamic SOQL not working with Database.query
Hi
i need to delete a particular location selected by user...i got some thing wrong in my code can anyone help out please....
Custom Controller
public with sharing class Loca {
private String rID{get;set;}
Location__c[] ld=new List<Location__c>();
List<Location__c> lc=new List<Location__c>();
public PageReference doDelete(){
try{
String s='SELECT ID FROM Location__c WHERE ID='+rID;
String.escapeSingleQuotes(s);
ld=Database.Query(s);
Database.delete(ld);
PageReference pr=new PageReference('/apex/success');
return pr;
}catch(Exception e){
ApexPages.Message m=new ApexPages.Message(ApexPages.severity.ERROR,'Deletion was not successful');
ApexPages.addMessage(m);
PageReference pr=new PageReference('/apex/unsuccess');
return pr;
}
PageReference pr=new PageReference('/apex/success');
return pr;
}
public List<Location__c> getLocations(){
String s='SELECT NAME,Country__c,Phone__c,City__c,State_Province__c FROM Location__c';
lc=Database.Query(s);
return lc;
}
}
VisualForce Page:
<apex:page controller="Loca">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!Locations}" var="l">
<apex:column headerValue="Action">
<apex:commandLink action="{!doDelete}" value="Delete">
<apex:param value="l.id" assignTo="{!rID}"/>
</apex:commandLink>
</apex:column>
<apex:column headerValue="Name" value="{!l.name}"/>
<apex:column headerValue="Phone" value="{!l.Phone__c}"/>
<apex:column headerValue="Country" value="{!l.Country__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Thanks in advance
Sravan Alaparthi.
here goes the solution...
Page:
<apex:page controller="DeleteTestingNew" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!records}" var="r">
<apex:column headerValue="Action">
<apex:commandlink value="Delete" action="{!doDelete}">
<apex:param name="rId" value="{!r.Id}" assignTo="{!rId}"/>
</apex:commandlink>
</apex:column>
<apex:column headerValue="Name">
{!r.name}
</apex:column>
<apex:column headerValue="city">
{!r.City__c}
</apex:column>
<apex:column headerValue="phone">
{!r.Phone__c}
</apex:column>
<apex:column headerValue="country">
{!r.Country__c}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public with sharing class DeleteTestingNew {
public String rId{get;set;}
Location__c delDlt = new Location__c();
public PageReference doDelete() {
delDlt=[select Id from Location__c where id =:rId];
delete delDlt;
pagereference ref=new pagereference('/apex/newdelete');
ref.setredirect(true);
return ref;
}
List<Location__c> lstdlt = new List<Location__c>();
public List<Location__c> getRecords() {
lstdlt =[select id,name,city__c,country__c,phone__c from Location__c];
return lstdlt;
}
}
All Answers
Is the problem that the record isn't deleted? In this section:
you are passing the string literal 'l.id' as the parameter value - you need to use merge syntax:
Hello Bob,
Thanks for the quick reply...
Even after using the merge field no result...
Actually it is not even rising any Exception also user is being redirected to the successful deletion page with the location record still there...
Sravan Alaparthi
Salesforce Developer/Consultant/Configurator
You should always go for escape sequence when you are having primitive data type as a criteria in dynamic SOQL.
Print the query before you execute it and it should in exact resemblance to how we execute it normally. You can check the printed query using force.com explorer or developer console.
In your case
SELECT ID FROM Location__c WHERE ID = 34dfgdfgdfgdfg, but the id value will always be enclosed with single quotes.
In order to achieve you need to use escape sequence just like below
String s='SELECT ID FROM Location__c WHERE ID='+ '\'' + rID + '\'';
--yvk
Hello YVK431,
Thanks for the tip of using developer console...
Actually when i execute this code
Loca l=new Loca();
ID i='a00G0000009EeRN';
l.setRID(i);
l.doDelete();
and displayin g the query using system,debug
i am getting the query as xxxxxxxxxxxWHERE ID=null
i tried several other ways to set rID=any location record id but all in vain any help will be greatly appreciated...
Sravan alaparthi
Salesforce Developer/adminstrator
I think you will need a rerender attribute on your delete button - otherwise I've found the parameter doesn't get passed back to the controller.
Hello all,
Thanks for the replies i implemented it other way and got it.....
here goes the solution...
Page:
<apex:page controller="DeleteTestingNew" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!records}" var="r">
<apex:column headerValue="Action">
<apex:commandlink value="Delete" action="{!doDelete}">
<apex:param name="rId" value="{!r.Id}" assignTo="{!rId}"/>
</apex:commandlink>
</apex:column>
<apex:column headerValue="Name">
{!r.name}
</apex:column>
<apex:column headerValue="city">
{!r.City__c}
</apex:column>
<apex:column headerValue="phone">
{!r.Phone__c}
</apex:column>
<apex:column headerValue="country">
{!r.Country__c}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public with sharing class DeleteTestingNew {
public String rId{get;set;}
Location__c delDlt = new Location__c();
public PageReference doDelete() {
delDlt=[select Id from Location__c where id =:rId];
delete delDlt;
pagereference ref=new pagereference('/apex/newdelete');
ref.setredirect(true);
return ref;
}
List<Location__c> lstdlt = new List<Location__c>();
public List<Location__c> getRecords() {
lstdlt =[select id,name,city__c,country__c,phone__c from Location__c];
return lstdlt;
}
}