2

I have been working on this on and off for quite sometime now, and have hit a wall. So what I'm trying to do is pass a Super Customer to my controller where it will get picked up and pushed over to getAccountAlias which in turn will do some work, I just can't figure out how to get that input value to my controller..

VF Page

  <table cellpadding="2" cellspacing="2">
  <tr>
    <td style="font-weight:bold;">Super Customer<br/>
    <apex:inputText value ="{!superCustomer}"/>
    <apex:commandButton action = "{!runSearch}" value = "Search"/>   
    </td>
    </tr>  
</table>

Controller

public String superCustomer {get;set;}
public SuperSearchController(){
   // controller = stdController;
   superCustomer = '';
}

public Map <Id, Account> getAccountAlias(String alias){
    alias = '%' + superCustomer + '%'; 
    Set<id> aliasId = new Set<id>();     
    for(Alias__c a : [SELECT id,Name,Super_Customer__c FROM Alias__c WHERE Super_Customer__c =: alias]){
        aliasId.add(a.id);
    }

    List<Account> accountKeys = [SELECT Name, Super_Customer1__c FROM Account WHERE Super_Customer1__c IN :aliasId];

    Map<id, Account> aMap  = new Map<id,Account>();        
    for(Account account: accountKeys){
        aMap.put(account.Id, account);
    }
    system.debug('This is aMap ' +aMap);
    return aMap;
}

Public PageReference runSearch() {
String superCustomer = Apexpages.currentPage().getParameters().get('superCustomer');        
Map<Id, Account> accountMap = getAccountAlias(superCustomer);

  if (!accountMap.isEmpty()){        
        this.optyList = this.getOpps(accountMap.keySet());
        //this.sampList = this.getSamp(accountMap.keySet());
        this.cList = this.getCase(accountMap.keySet());
        this.alist=this.getAcct(accountMap.keySet());
        // Repeat for other types
        }  
    return null;
}
}
2
  • What errors do you get when you run this? Does the value load? Does it not? Did you try setting the action to a setSuperCustomer method? Jul 31, 2014 at 20:47
  • I'm not getting any errors, and I have tried everything that Google could find me. At one point I did have it searching but I realized it was giving me results for all my Accounts, it went unnoticed because I don't have a lot of data. So I'm trying to figure out how to pass a value through, and will work from there
    – EricSSH
    Jul 31, 2014 at 21:01

2 Answers 2

2

You need to do very little: when the apex:commandButton is clicked the superCustomer field on the controller will be automatically populated.

You don't explain your data model, but if Alias__c.Super_Customer__c is a text field that you want to match with your superCustomer field and Account.Super_Customer1__c is an Alias ID then something like this should work:

public String superCustomer {get;set;}

public PageReference runSearch() {     
    Map<Id, Account> accountMap = getAccountMap(superCustomer);
    if (!accountMap.isEmpty()){        
        // Use the map
    }  
    return null;
}

private static Map<Id, Account> getAccountMap(String alias) {
    String aliasLike = '%' + alias + '%';
    Map<Id, Account> m = new Map<Id, Account>([
            SELECT Id, Name, Super_Customer1__c
            FROM Account
            WHERE Super_Customer1__r.Super_Customer__c like :aliasLike
            ]);
    return m;
}
4
  • Invalid operator on id field here Map<Id, Account> m = new Map<Id, Account>([ SELECT id,Name, Super_Customer1__c FROM Account WHERE Super_Customer1__r.Super_Customer__c like :aliasLike ]);
    – EricSSH
    Jul 31, 2014 at 22:27
  • @EricSSH As I mentioned in the answer, I had to make some assumptions about the data model. A partial fix might be WHERE Super_Customer1__r.Name like :aliasLike. But if Alias__c is is self referencing and you want to go up that hierarchy something a bit fancier would be needed. If you are still stuck please explain the relevant fields of the objects a bit more.
    – Keith C
    Aug 1, 2014 at 7:11
  • Awesome, thanks @Keith C it works.. Never seen it done like that before, WHERE Super_Customer1__r.Name
    – EricSSH
    Aug 1, 2014 at 15:50
  • @EricSSH Using the available relationship queries often makes code simpler. Well worth a careful read.
    – Keith C
    Aug 1, 2014 at 16:07
2

I think the problem is with this line, inside your runSearch() method:

String superCustomer = Apexpages.currentPage().getParameters().get('superCustomer');

You already have a public class-level variable called this.superCustomer (or just superCustomer for short), which is set up to be populated from the VF page. But then on this line you define a method-level variable with the same name, which is populated from the URL parameters. I'm guessing this was part of your troubleshooting process, but it looks like now it's the actual problem.

If you comment this line out of your code, then you will be passing the proper superCustomer (aka this.superCustomer: the class-level one, which comes from the page) into your getAccountAlias() method.

2
  • Your idea didn't work =/
    – EricSSH
    Jul 31, 2014 at 22:34
  • What happened? What results are you currently getting from the code you posted? If you replace the line I reference above with a system.debug() that outputs the superCustomer variable, what do you see in the log? Jul 31, 2014 at 23:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .