+ Start a Discussion
Vandana RattanVandana Rattan 

Query to Fetch OpportunityLineItems

I am writing a scheduler to run a batch process monthly on OpportunityLineItems and update the ServiceDate to Current Date. The query that I am usin to fetch OpportunityLineItems for Opportunities whose CloseDate > Today's date gives error.

Query:-
'SELECT servicedate, opportunityid from OpportunityLineItem where opportunityid IN \'(SELECT id from opportunity where closeDate >\')' + System.today().format();

Error:
00:00:22.058 (58548104)|EXCEPTION_THROWN|[11]|System.QueryException: expecting a colon, found '(SELECT id from opportunity where closeDate >'
00:00:22.058 (58619737)|SYSTEM_METHOD_EXIT|[11]|Database.getQueryLocator(String)
00:00:22.058 (58676089)|FATAL_ERROR|System.QueryException: expecting a colon, found '(SELECT id from opportunity where closeDate >'

How should I modify the query?

Thanks in Advance!
Best Answer chosen by Vandana Rattan
AshlekhAshlekh
Hi,

Use below code 
Date s = system.today();
string query = 'select id,servicedate, opportunityid from opportunitylineitem where opportunityid in (select id from opportunity where closeDate >:S)';

If it helps you than plese mark it as a solution 

All Answers

AshlekhAshlekh
Hi,

Use below code 
Date s = system.today();
string query = 'select id,servicedate, opportunityid from opportunitylineitem where opportunityid in (select id from opportunity where closeDate >:S)';

If it helps you than plese mark it as a solution 
This was selected as the best answer
kevin lamkevin lam
'SELECT servicedate, opportunityid from OpportunityLineItem where opportunityid IN (SELECT id from opportunity where closeDate > TODAY)'; 
Vandana RattanVandana Rattan
Thanks Ashlekh. Your answer solved my query.