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
sravan alaparthisravan alaparthi 

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.

 

Best Answer chosen by Admin (Salesforce Developers) 
sravan alaparthisravan 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

bob_buzzardbob_buzzard

Is the problem that the record isn't deleted?  In this section:

 

<apex:commandLink action="{!doDelete}" value="Delete">
<apex:param value="l.id" assignTo="{!rID}"/>
</apex:commandLink>

 

you are passing the string literal 'l.id' as the parameter value - you need to use merge syntax:

 

<apex:commandLink action="{!doDelete}" value="Delete">
<apex:param value="{!l.id}" assignTo="{!rID}"/>
</apex:commandLink>

 

 

sravan alaparthisravan alaparthi

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

yvk431yvk431

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

 

 

sravan alaparthisravan alaparthi

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

bob_buzzardbob_buzzard

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.

sravan alaparthisravan alaparthi

Hello all,

 

Thanks for the replies i implemented it other way and got it.....

 

 

 

sravan alaparthisravan 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;
}

}

 

 

This was selected as the best answer