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
raj123raj123 

Date in the query string is not populating correctly , instead string is populated

Hi Folks

 

I have an issue

 

date d = system.today().addDays(-2);
String strQuery='Select Id,Collection_Center_Code__c,RMA_Number__c,Case.Contact.Name,Contact.Email,SC_Received_Num_Boxes_WDE__c,SCReceivedPalleteID_WDE__c,SCReceivedTracking_WDE__c, SCReceivedTime_WDE__c,SCShipToWDDate_WDE__c,(Select Id,Received_Date__c From Order_Status__r where received_date__c > :d and received_date__c = null ) From Case t Where Collection_Center_Code__c =\''+Code+'\'';

 

the date variable is coming as d instead of value when i check in debug logs 

 

can some one help me with this 

thanks,

 

Best Answer chosen by Admin (Salesforce Developers) 
Anup JadhavAnup Jadhav

You might have to convert the the value in d to string, and it will work because the query string expects a string, not a Date object.

 

Regards,

Anup

All Answers

Anup JadhavAnup Jadhav

Hey raj123,

 

I am not clear as to what the issue is.

 

Can you elaborate the following?

 

> the date variable is coming as d instead of value when i check in debug logs

 

Do you mean that the value in variable d is empty?

 

Also, your SOQL is incorrect I believe. How do you expect this query to return anything when you are checking if the received_date__c is both greater than the value in d, and also null? These two conditions are mutually exclusive.

 

Regards,

Anup

raj123raj123

hi thank for the quick reply 

hi i am comparing the received_date__c(date type ) with d  which is a date i am preparing to pass to the query, but the date value is not showing insted d is showing up 

 

the whole thing is in the querystring as i am passing the query string to 

Database.query(strQuery)

debug logs

 

Select Id,Collection_Center_Code__c,RMA_Number__c,Case.Contact.Name,Contact.Email,SC_Received_Num_Boxes_WDE__c,SCReceivedPalleteID_WDE__c,SCReceivedTracking_WDE__c, SCReceivedTime_WDE__c,SCShipToWDDate_WDE__c,(Select Id,Received_Date__c From  Order_Status__r where received_date__c >: d and received_date__c = null ) From Case t Where Collection_Center_Code__c ='FRA'
Navatar_DbSupNavatar_DbSup

Hi,


I have tested the same and its working fine at my end. Try the below as reference:


date d = system.today().addDays(-2);
system.debug('@@@@@@@@@@@@@@@@@@' +d);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

raj123raj123

but if i am using the date variable d in the query string , the date vaule is not ppulating at runtime

bob_buzzardbob_buzzard

Your debug looks like you have a space between the ':' character and the 'd' - is that the case for the code?

raj123raj123

i removed the space b/n : and d , still noo success

Anup JadhavAnup Jadhav

Ah I see!

 

Then modify your query string as follows and it should work:

 

String strQuery='Select Id,Collection_Center_Code__c,RMA_Number__c,Case.Co

ntact.Name,Contact.Email,SC_Received_Num_Boxes_WDE__c,SCReceivedPalleteID_WDE__c,SCReceivedTracking_WDE__c, SCReceivedTime_WDE__c,SCShipToWDDate_WDE__c,(Select Id,Received_Date__c From Order_Status__r where received_date__c > '+d+' and received_date__c = null ) From Case t Where Collection_Center_Code__c =\''+Code+'\'';

 

hope this helps!

 

- Anup

raj123raj123

ohh, its erroring out

Anup JadhavAnup Jadhav

You might have to convert the the value in d to string, and it will work because the query string expects a string, not a Date object.

 

Regards,

Anup

This was selected as the best answer
sfdcfoxsfdcfox

Your query was wrong this whole time, I think. You have "received_date__c > :d and received_date__c = null" in your filter conditions, which will never be satisified. The debug logs won't show the substituted date value in the bound SOQL statement, but it will work correctly regardless. Try using your original query, using "or" instead of "and", if that is your intent. A field can not possibly ever be null *and* return a true value for any inequality operator, since null is always a false result in an inequality operator.

 

EDIT: If you're using Database.Query(String), you have to format the date field yourself. Instead, just use the standard SOQL syntax ( [SELECT ... FROM ... WHERE .. ] ), so that salesforce.com can bind it correctly for you. Alternatively, you can bind directly to the variable using:

 

String query = 'SELECT ... FROM ... WHERE ... Received_date__c > :d ...'

 

Thanks to the magic of the Database.query runtime engine, it will try and resolve bound variables in a string just as it would were it a static (normal) query.