0

How do I get the Name of a Contact record based on its ID? Something like:

getContactById('id').Name

Unfortunately, the above does not work. Is there a repository of APEX functions? Where can I find it in order not to ask so basic questions?

My code is :

enter image description here

EDIT:

I tried this

Contact con = Database.query('SELECT Name FROM Contact WHERE Id = ' + saveR.id);

I got an exception :

System.QueryException: unexpected token: 'H00001s0qZNQAY'

3
  • For the Contact.Name, I think I may use a query to return it, from the ID... But isn't there a simplier way to do it ? Something like [SELECT Name from Contact where Id = :conId] Jun 28, 2018 at 9:31
  • I found the same question here but the only answer don't satisfy me ... salesforce.stackexchange.com/questions/45380/get-sobject-by-id Jun 28, 2018 at 9:36
  • Please include your code as text, not images. Please also limit your questions to one topic, as it is very difficult to provide one quality answer to multiple disparate questions. One last thing: questions about how to use the Stack Exchange network belong on Salesforce Meta. Questions about how Salesforce works are all that is on topic here on the main exchange.
    – Adrian Larson
    Jun 28, 2018 at 21:59

2 Answers 2

1

Querying record is obviously an option but if information is already in scope (for example- name of contact is already provided while initializing the object) then why should we query. Also, if there are multiple rows then we may need to query for each record or make a set of records with help of for loop on Databse.SaveResult; then again iterate over that queried list then show message- which will cost too much!

Despite of that I would follow another approach as below:

// Create two accounts, one of which is missing a required field
Account[] accts = new List<Account>{
    new Account(Name='AccountTesting'),
    new Account()};

Database.SaveResult[] srList = Database.insert(accts, false);
Map<Id, Account> accmap = new Map<Id, Account>();
for(Account ac: accts){
    if(ac.Id!= null){
        accmap.put(ac.Id, ac);
    }
}
// Iterate through each returned result
for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account name: ' + accmap.get(sr.getId()).Name);
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}

P.S.: I would not make query until I need to ge fields which is not given while inserting the records.

1
  • 1
    Very good answer, with a good practise. But still, it remains easy but long. Thansk, tho :) Jun 28, 2018 at 12:34
4

For your first question, you have already found the solution.

[SELECT Name from Contact where Id = :conId]

We need SOQL query to fetch details about the data. We use describe to fetch details about Metadata.

You can use @Alexis MASSON to mention users. If you comment under my answer I will get notified but if you also want to tag other users just use @ with there name.

For formatting, the code select the code you want to highlight and click the {} icon in the box and it will do the formatting for you. For highlight some variable in inline use them '``'.

Also, you should try Trailhead Modules to get the basic idea of APEX and Salesforce platform.

You don't need dynamic SOQL here. Normal SOQL will work. [SELECT Name FROM Contact WHERE Id =: saveR.id]

Or if you want dynamic SOQL do this

Id recId = saveR.id;
Database.query('SELECT Name FROM Contact WHERE Id =:recId  ');
0

You must log in to answer this question.

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