0

I'm trying to pass a picklist value to a method if statement to change up the query based on what is selected form a picklist. When I try to use the if statement below I get a variable does not exist. I'm using public String SearchField {get;set;} to bring in the picklist values to the controller. Is there a way to use the SearchField Value from the picklist in the method below?

method:

@RemoteAction
global static List<InnerClassAccount> getAccounts(String toQuery) {

    List<InnerClassAccount> ret = new List<InnerClassAccount>();
    List<Schema.FieldSetMember> fields = Schema.SObjectType.Account.fieldSets.Search_Businesses.getFields();
    List<Fields> fieldsInner = new List<Fields>();
    String query = 'SELECT ';
    String SearchField = 'd';

    for(Schema.FieldSetMember f : fields) {
        if(f.getFieldPath().contains('.')) {
            String [] split = f.getFieldPath().split('\\.');
            fieldsInner.add(new Fields(f.getFieldPath(),split[0] + ' '+f.label));
        }else {
            fieldsInner.add(new Fields(f.getFieldPath(),f.label));
        }

        if(f.getFieldPath()!= 'Id' &&  f.getFieldPath()!= 'Name'&& f.getFieldPath()!= 'ShippingAddress' && f.getFieldPath()!= 'BillingAddress'&& f.getFieldPath()!= 'OwnerID' ){
          query += f.getFieldPath() + ', ';
        }
    }

    if (SearchField = 'Name') {
        String filter = '%' + toQuery + '%';
    query += 'Id, Name,ShippingAddress,BillingAddress, OwnerID, TSGADX__MainParentId__c FROM Account WHERE Name LIKE :filter';

    if (SearchField = 'Phone') {
        String filter1 = '%' + toQuery + '%';
        query += 'Id, Name,ShippingAddress,BillingAddress, OwnerID, TSGADX__MainParentId__c FROM Account WHERE Phone LIKE :filter1';        

    if (SearchField = 'Address') {
        String filter2 = '%' + toQuery + '%';
        query += 'Id, Name,ShippingAddress,BillingAddress, OwnerID, TSGADX__MainParentId__c FROM Account WHERE BillingAddress LIKE :filter2 or ShippingAddress like :filter2';
    }}}
1

= Is Not Equality

You need to use == instead:

if (SearchField == 'Name') {

Your Braces Are Tangled

Your braces should look like:

if (SearchField == 'Name') {
    String filter = '%' + toQuery + '%';
    query += 'Id, Name,ShippingAddress,BillingAddress, OwnerID, TSGADX__MainParentId__c FROM Account WHERE Name LIKE :filter';
}
if (SearchField == 'Phone') {
    String filter1 = '%' + toQuery + '%';
    query += 'Id, Name,ShippingAddress,BillingAddress, OwnerID, TSGADX__MainParentId__c FROM Account WHERE Phone LIKE :filter1';        
}
if (SearchField == 'Address') {
    String filter2 = '%' + toQuery + '%';
    query += 'Id, Name,ShippingAddress,BillingAddress, OwnerID, TSGADX__MainParentId__c FROM Account WHERE BillingAddress LIKE :filter2 or ShippingAddress like :filter2';
}

You Cannot Access Instance Variables In Static Code

You need to pass in the SearchField parameter:

@RemoteAction
global static List<InnerClassAccount> getAccounts(String SearchField, String toQuery) {
  • Thanks! worked to clear the errors. There is a js function that passes the toQuery to the method. For this to work would I need to add the searchField to this as well? example function getRecords(toQuery, searchFields) { Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.SearchAllBusinessesControllerV3.getAccounts}',toQuery, SearchFields, function(result, event) { – DGOU182 May 1 '20 at 0:26
  • @DGOU182 Yes, that's correct. – sfdcfox May 1 '20 at 0:32
  • I haven't been able to get the method to work when adding the variable. The variable is from a drop down on the VF page. I have public String SearchField {get;set;} in the controller. Is there something else I'm missing? This was just to give the option to search by different fields. – DGOU182 May 1 '20 at 16:58
  • You have to get the SearchField value from the page's input; you cannot depend on the controller's value being set or even accessible when using a RemoteAction. You may want to consider using apex:actionFunction instead. – sfdcfox May 1 '20 at 17:01
0

Since the method is static, it can only work with parameters that you pass to it, and by default any controller instance variables are not visible (unless you pass the whole controller into the method).

Are you able to declare the method like so?

global static List getAccounts(String toQuery, String SearchField)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.