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
shivram survaseshivram survase 

Hi,How can i use the method parameter in LIKE query to perform substring operation?Help is appreciated in advance.

My code is as follow:
public class AccountController {
    @AuraEnabled
    public static List<Account> getAccounts(String accName,String accCountry) {
        List<Account> objAccount = [SELECT Id, Name, Country__c FROM Account where name like '%:accName%' ];
        return objAccount;
    }
    
}

I need to perform searching on 'accName' value as a substring.
Best Answer chosen by shivram survase
KaranrajKaranraj
Shivram - You can try in these approach
public class AccountController {
    @AuraEnabled
    public static List<Account> getAccounts(String accName,String accCountry) {
       string accountName = '%'+accName+'%';
        List<Account> objAccount = [SELECT Id, Name, Country__c FROM Account where name like :accountName ' ];
        return objAccount;
    }
    
}

or you can use dynamic query string also
public class AccountController {
    @AuraEnabled
    public static List<Account> getAccounts(String accName,String accCountry) {
      string query = 'SELECT Id, Name, Country__c FROM Account where name like \'%'+accName+'%\'';
        List<Account> objAccount = Database.query(query); 
        return objAccount;
    }
    
}

 

All Answers

KaranrajKaranraj
Shivram - You can try in these approach
public class AccountController {
    @AuraEnabled
    public static List<Account> getAccounts(String accName,String accCountry) {
       string accountName = '%'+accName+'%';
        List<Account> objAccount = [SELECT Id, Name, Country__c FROM Account where name like :accountName ' ];
        return objAccount;
    }
    
}

or you can use dynamic query string also
public class AccountController {
    @AuraEnabled
    public static List<Account> getAccounts(String accName,String accCountry) {
      string query = 'SELECT Id, Name, Country__c FROM Account where name like \'%'+accName+'%\'';
        List<Account> objAccount = Database.query(query); 
        return objAccount;
    }
    
}

 
This was selected as the best answer
shivram survaseshivram survase
Thanx..brother.