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
#DD#DD 

Getting malformed query error for the below query

Hi,
When i run this query i get Malformed query error;

qLoc = Database.getQueryLocator([Select Id, Name, Type, Status,(select Id from CampaignMembers where ContactId=:'+ objContact.Id +') from Campaign]);
 
Best Answer chosen by #DD
Amit Chaudhary 8Amit Chaudhary 8
Hi DD,

Please try below code.

String strQuery = ' Select Id, Name, Type, Status,(select Id from CampaignMembers where ContactId=\''+objContact.Id+'\' ) from Campaign' ;
qLoc = Database.getQueryLocator( strQuery );

Please let us know if this will help you

Thanks
Amit Chaudhary

All Answers

Himanshu ParasharHimanshu Parashar
qLoc = Database.getQueryLocator([Select Id, Name, Type, Status,(select Id from CampaignMembers where ContactId=:objContact.Id) from Campaign]);
Amit Chaudhary 8Amit Chaudhary 8
Hi DD,

Please try below code.

String strQuery = ' Select Id, Name, Type, Status,(select Id from CampaignMembers where ContactId=\''+objContact.Id+'\' ) from Campaign' ;
qLoc = Database.getQueryLocator( strQuery );

Please let us know if this will help you

Thanks
Amit Chaudhary
This was selected as the best answer
#DD#DD
Hi Himanshu, I tried the one you suggested earlier, but it didn't work.

@Amit, Thanks, it did the trick. but can you explain the logic bechind quotes and backslsahes you have incorporated in the query?