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
srikanth chitiralasrikanth chitirala 

Hi all, i have a reqirment like to check syntax of soql thorugh command button in my visualforce page can any one suggest how ??

check syntax of soql thorugh command button in my visualforce page can any one suggest how ??
thatheraherethatherahere
Hi Srikanth,

Please try the below solution.

Apex Class:
public class SOQLSyntaxController{
    
    public String queryString{get;set;}
    
    public SOQLSyntaxController(){
        queryString = '';
    }
    
    public void checkQuerySyntax(){
        
        try{
            if( String.isNotBlank( queryString ) ){
                list<sObject> sobj = Database.query( queryString );
                ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.CONFIRM,'Valid SOQL.' )); 
            }else{
                ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.Error,'Please enter SOQL to check Syntax.' ));    
            }
        }catch(Exception e){
            ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.Error,'Invalid SOQL. '+e.getMessage() ));
        }
    }
    
}

Visualforce page:
<apex:page controller="SOQLSyntaxController">
    <apex:form id="frm">
        <apex:pageBlock >
            <apex:pageMessages ></apex:pageMessages>    
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >SOQL Query: </apex:outputLabel>
                    <apex:inputTextarea value="{!queryString}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            
            <apex:pageBlockButtons >
                <apex:commandButton value="Check Syntax" action="{!checkQuerySyntax}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>    
    </apex:form>

</apex:page>


Let me know if you need more help here.

Thanks,

- thatherahere