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
Sangeet kaseraSangeet kasera 

Dynamic SOQL query StartsWith To: ?

Hi All,

I am creating a filter with checkboxes, I want when case comment checkbox is true then all comment associated with case are shown and when we check checkbox Email, Then only comments which are added by mail should be shown means-
  • If Email is check for email-to-case then only comment are shown which startsWith To:
  • and If Case Comment is checked then except To: comments should be shown.
I don't know startsWith is used in SOQL or not but i Used like operater but it is showing error please help me to find the correct query

My sample code are -
if(filterLabel.isEmpty() && commentFilter==true){
    System.debug('Inside custom setting Enable If query');
    String srchValue = 'To:';
    string tempInput = '%' + srchValue + '%';
    commentQuery = commentQuery + ' And CommentBody NOT LIKE \'%' + srchValue + '%\'';
    System.debug('Inside if of custom setting check'+commentQuery);
}
else if(!filterLabel.isEmpty() && commentFilter==true && !filterLabel.contains('Email')){
    String srchValue = 'To:';
    string tempInput = '%' + srchValue + '%';
    commentQuery = commentQuery + ' And CommentBody LIKE \'%' + srchValue + '%\'';
    System.debug('Inside first else of custom setting check'+commentQuery);
}
else if(!filterLabel.isEmpty() && !commentFilter==true && !filterLabel.contains('Email')){
    commentQuery = commentQuery +' And Createdby.Id IN: Ids';
    System.debug('Inside second else of custom setting check'+commentQuery);
}
else if(!filterLabel.isEmpty() && !commentFilter==true && FilterStringValue=='Email' && filterLabel.contains('Email')){
    String srchValue = 'To:';
    string tempInput = '%' + srchValue + '%';
    commentQuery = commentQuery + ' And CommentBody LIKE \'%' + srchValue + '%\'';
    System.debug('Inside third else of custom setting check'+ commentQuery);
}
else if(!filterLabel.isEmpty() && commentFilter==true && FilterStringValue=='Email' && filterLabel.contains('Email')){
    commentQuery = commentQuery;
    System.debug('Inside fourth else of custom setting check'+ commentQuery);
}
else if(!filterLabel.isEmpty() && commentFilter==true && filterLabel.contains('Email')){
    commentQuery = commentQuery;
    System.debug('Inside fifth else of custom setting check'+commentQuery);
}

I also used below query - 

String
startValue = 'To:';
   commentQuery = commentQuery + ' And CommentBody NOT LIKE '+ startValue +'%\'';

Regards,
Sangeet
Best Answer chosen by Sangeet kasera
Jithesh VasudevanJithesh Vasudevan
Hi Sangeet,
Why don't you try any of the below code snippets,

String srchValue = 'To:';
String tempInput = '%' + srchValue + '%';
String commentQuery  = commentQuery + ' And NOT CommentBody  LIKE: tempInput' ;

OR
String srchValue = 'To:';
String tempInput = '\'%' +srchValue + '%\'';
String commentQuery  = commentQuery + ' And NOT CommentBody  LIKE' +  tempInput;

OR
String srchValue = 'To:';
String commentQuery  = commentQuery + ' And NOT CommentBody  LIKE' +  '\'%' +srchValue + '%\'';