0
votes

Can anyone help clarify how to obtain a field in my salesforce DB as a string (say for example Name from a specific/single Account) where I am searching by lets say custom_ID__c in Accounts? I am familiar with most sql and quering DBs but I cannot seem to get my queries to run and return one specific account record's Name. Here is what I have tried and failed with:

SObject s = [SELECT Name FROM Account WHERE Customer_Number__c = :ApexPages.currentPage().getParameters().get('ARID') LIMIT 1];


SObject s = [SELECT Name FROM Account WHERE Customer_Number__c = :ApexPages.currentPage().getParameters().get('ARID')];


SObject s = [SELECT Name FROM Account WHERE Customer_Number__c = :ARID LIMIT 1][0];

SObject s = Database.query('SELECT Name FROM Account WHERE Customer_Number__c = :ARID LIMIT 1');

Account act = new Account( = [Select Name FROM Account WHERE Customer_Number__c = :ARID LIMIT 1]);

I have tried each of these queries individually and most of them seem to compile but fail at runtime with no real error. :( please help

Thank you very much in advance

1

1 Answers

0
votes

I'm not sure if there's any reason you would want to be casting a query on a specific object to a generic SObject...but this should definitely work, assuming the request actually contains the ARID parameter:

Account act = [select Id, Name from Account where Customer_Number__c =
                :ApexPages.currentPage().getParameters().get('ARID') limit 1];

But if no records are returned by the query, you'll get a runtime error (which might be happening in your case, I'm not sure why it would be suppressing it unless your queries are in a try/catch block that doesn't report the error). So it's usually best to query a list:

Account[] acts = [select Id, Name from Account where Customer_Number__c =
                :ApexPages.currentPage().getParameters().get('ARID')];
if ( acts != null && !acts.isEmpty() ) {
    // do whatever you want with acts[0]
}