public without sharing class searchHospitalController { public String SearchName {get;set;} private String accountid = null; public List at {get;set;} public void init(){ User useracc = [SELECT accountid,UserPro_Type__c FROM user WHERE id = :UserInfo.getUserId() ]; accountid = Useracc.accountid; String soql = 'SELECT id,Name,State_Master__c,State_Master__r.Name,Address__c FROM Account'; soql += ' where id in (SELECT Hospital__c FROM hospitalprice__c WHERE account__c = :accountid) ' ; soql += ' order by Name desc limit 100'; at = Database.query(soql); } public PageReference serContact(){ at = Database.query(this.makeSoql(SearchName,accountid)); return null; } private String makeSoql(String searchName,String accountid){ String soql = 'SELECT id,Name,State_Master__c,State_Master__r.Name,Address__c FROM Account'; soql += ' where id in (SELECT Hospital__c FROM hospitalprice__c WHERE account__c = :accountid) ' ; if(String.isNotBlank(searchName)){ soql += ' AND Name like \'%' + SearchName+ '%\''; } soql += ' order by Name desc limit 100'; return soql; } }