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
ramya1ramya1 

Argument 1 cannot be null during the Dynamic Search

Hi..

 

Am trying to build a dynamic query for search Scenario...i used the following code..

 

public void searchAppointment()
{

string query = 'select id,name,branch__c,doctor__c,Appt_Reason__c,Appt_Status__c,Appt_Date__c from appointment__c WHERE ';

 

appointmentBranch = userinputData.branch__c;
if(appointmentBranch != null){
query = query+' branch__c includes (\''+appointmentBranch +'\')';
}

appointmentReason = userinputData.Appt_Reason__c;
if(appointmentReason != null){
query = query+'AND Appt_Reason__c includes (\''+appointmentReason +'\')';
}

 

runQuery();

}

 

public void runQuery() {
system.debug('..QUERY5..'+QUERY);
try{
apptmnt = Database.query(query); 
}catch(exception e){}
system.debug('Appointmenttttttt'+apptmnt);
}

 

 

Am getting values in the Query within the searchAppointment method...

am getting issue in the runQuery method...values are not getting passed into Query here ..

Getting error as : 

System.NullPointerException: Argument 1 cannot be null

 

 

Thanks in Advance..

Devendra@SFDCDevendra@SFDC

 

Hi,

 

Put System.debug statements for your query. There is a chance that where parameter getting null value.

 

Thanks,

Devendra

vishal@forcevishal@force

Hello,

 

Yes, Devendra is correct. 

 

You are adding a WHERE in your query but what if both your conditions are false? Your query will end at where, which is incorrect.

 

Try forming the query in a way where it works fine for all the 3 cases:

1. Both conditions are true

2. One is true and other is false

3. Both are false.

 

Try this:

 

String apptBranchCondition;
String apptReasonCondition;
string query = 'select id,name,branch__c,doctor__c,Appt_Reason__c,Appt_Status__c,Appt_Date__c from appointment__c';
if(userinputData.branch__c != null){
apptBranchCondition = ' branch__c includes (\''+appointmentBranch +'\')';
}

if(userinputData.Appt_Reason__c != null){
apptReasonCondition = ' Appt_Reason__c includes (\''+appointmentReason +'\')';
}

// Now add WHERE Conditions
if(apptBranchCondition != NULL || apptReasonCondition != NULL){
query = query + ' WHERE';

if(apptBranchCondition != NULL && apptReasonCondition != NULL){
query = query + apptBranchCondition + ' AND' + apptReasonCondition;
}
else if(apptBranchCondition != NULL && apptReasonCondition == NULL){
query = query + apptBranchCondition;
}
else if(apptBranchCondition == NULL || apptReasonCondition != NULL){
query = query + apptReasonCondition;
}
}

SunilKumar.ax1648SunilKumar.ax1648

Hi,

 

this block of code you put in constructor or call this method in constructor
try

 

string query = 'select id,name,branch__c,doctor__c,Appt_Reason__c,Appt_Status__c,Appt_Date__c from appointment__c;

 

appointmentBranch = userinputData.branch__c;
if(appointmentBranch != null){
query = query+' branch__c includes (\''+appointmentBranch +'\')';
}

appointmentReason = userinputData.Appt_Reason__c;
if(appointmentReason != null){
query = query+'AND Appt_Reason__c includes (\''+appointmentReason +'\')';
}

 

runQuery();

}

ramya1ramya1

Thanks for your reply

 

I am getting error at 

 

Date appointmentDate = Date.valueof(userinputData.Appt_Date__c);
system.debug('..AppDate..'+userinputData.Appt_Date__c);
if(appointmentDate != null){
query = query+' AND Appt_Date__c = '+appointmentDate +'';
}

 

I am getting error as 

no viable alternative at character '<EOF>'

 

Thiyagarajan SelvarajThiyagarajan Selvaraj

Try this,

 

query += ' AND Appt_Date__c =: appointmentDate';