+ Start a Discussion
SFDC LearningSFDC Learning 

Need to optimize code

Thanks in advance,

if(searchbyname.length()==0){
    }else{
    
     if(accname==null){
            lstAcct=[Select id,name from Account WHERE  Lock__c=:false AND name LIKE  : searchbyname  ORDER BY Name ASC];

     }else{
    lstAcct=[Select id,name from Account WHERE Sales_Mgr__c= : accname AND Lock__c=:false AND name LIKE  : searchbyname  ORDER BY Name ASC];
   }
    }


I want to execute this code with single query.help me.
Best Answer chosen by SFDC Learning
Nagendra Prasad ChNagendra Prasad Ch
Hi Suml,

Try below code
String soql = 'Select id,name from Account WHERE Lock__c=:false AND name LIKE  : searchbyname ';
        
        if(string.isNotBlank(accname))
            soql += ' AND Sales_Mgr__c= : accname   '; 
			
			 soql += '  ORDER BY Name ASC ';  
        
        lstAcct = Database.query(soql );