I'm an Apex beginner. I have a publically visible Force.com site/page that references an Apex class/controller. The page functions as a web form so customers can submit/create a case.

Because the page is public, I don't display any Account info. But I ask users to complete two mandatory fields on the web form before submitting a case so I can manually lookup/verify the Account info after the case is created:

  • Company ID = {!Case.CompanyID__c}
  • Company Name = {!Case.SuppliedCompany}

After clicking the submit button, I'd like to apply the following logic before the case is created so I don't need to manually lookup the Account info after the case is created. But I don't know how or where to code it:

1.IF the value in the 'Company ID' web form field {!Case.CompanyID__c} is an exact match on the 'Company ID' field in the Account table {!Account.CompanyID__c}, use value {!Account.CompanyID__c} to look up the 'Account Name' {!Account.Name} so we can populate the case's 'Account Name' field {!Case.Account} when the case is created.

2.ELSE IF the value in the 'Company Name' web form field {!Case.SuppliedCompany} is a match (not case sensitive) on the 'Account Name' field in the Account table {!Account.Name}, use value {!Account.Name} so we can populate the case's 'Account Name' field {!Case.Account} when the case is created.

3.ELSE leave the ‘Account Name’ field {!Case.Account} blank when the case is created

  • 1
    Where have you started / please share the code you're already developed. Jun 15, 2015 at 0:07
  • That is part of the issue. I don't know where to start. I assume it will require an apex trigger + class.
    – egts
    Jun 16, 2015 at 1:04

1 Answer 1


Add the following below logic to your controller (this is assuming the values of CompanyID__c on Account are unique, since you didn't specify what to do if there was more than one match in the first if).

Just as an aside, you have some mildly confusing bits in your question. I assume you mean SuppliedCompany__c? And you refer to multiple Account fields on Case, but there is one standard field that I believe you're referring to...its field label is Account Name, its field name is Account, and it is a lookup to Account, so you assign it an Account ID. This is a sort of confusing bit:

return the AccountID {!Account.Id} = {!Case.AccountId} and populate the case's 'Account Name' field {!Case.Account}

because you say "return" a value but then it appears you want an assignment, and setting AccountID would automatically populate Account Name, since Account Name is the label for the Account field - again, just to reiterate, it would be nice if next time you post the controller code and page you already have so we can see firsthand exactly what you mean, and give you alternative suggestions if necessary :)

List<Accounts> companyIDAccounts = [SELECT id, Name, CompanyID__c FROM Account WHERE CompanyID__c =: Case.CompanyID__c LIMIT 1];
if (companyIDAccounts.size() > 0) {
  Case.AccountId = companyIDAccounts[0].id;
else {
  List<Accounts> suppliedCompanyAccounts = [SELECT id, Name FROM Account WHERE Name =: Case.SuppliedCompany__c LIMIT 1];
  if (suppliedCompanyAccounts.size() > 0) {
    Case.AccountId = suppliedCompanyAccounts[0].id;

You must log in to answer this question.

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