1

Hi,

In my understanding, we can set Salutation, FirstName and LastName for the Contact object ; The Name field is automatically updated by concatening these three fields (and this field isn't writable).

So how is this possible :

// TODO #5: Use the Database.insert command to insert the Contacts
    //          in contacts, with a partial commit.
    Database.SaveResult[] sr = Database.insert(contacts, false);

    Map <Id, contact> conMap = new Map <Id, Contact>();
    for (Contact con : contacts) {
        if(con.id!=null) {
            // Contact inserted, print id / name
            conMap.put(con.id, con);
            System.debug('conMap.get(con.id).LastName : ' + conMap.get(con.id).LastName);
            System.debug('conMap.get(con.id).NAME (not null ?) : ' + conMap.get(con.id).Name);
        }
    }

Nb : this is the contact declaration :

Contact name2 = new Contact(LastName='Picasso');

Thanks !

enter image description here

2
  • you want combine first name,lastname and Salutation? Jun 29 '18 at 12:11
  • Thanks for helping, I only tried to debug the Name ! I understood that 'The Name field is automatically updated by concatening these three fields (and this field isn't writable).', isn't it true ? So how come I can't debug it the way I tried ? Jun 29 '18 at 13:28
2

In order to get this field's value - you will need to query the Contact after inserting it. Since this field is a concatenated field, it is null in the instance that you are requesting it. All you will get from your save result is the new Id value.

This behavior is the same as any other formula field in Salesforce. The value is created upon insert for this. Salesforce will only return you the fields that you have in your Contact instance and ONLY additionally the Id of the object upon insert.

Contact Developer Reference

2

In contact object,

select id,lastname,firstname,name,Salutation from contact

Here name is combination of first name and last name.

In your case name is null, So you should get last name in your map

For Example,

Map <Id, contact> conMap = new Map <Id, Contact>();
contact con=[select id,lastname,firstname,name,Salutation from contact where 
  lastname='Annappa PH' limit 1];
          conMap.put(con.id, con); 
      system.debug('contmap values:::'+conMap.values());

In my system contact first name is null,

enter image description here

If I print contact map values. I got below result: You can see name in my map value.

enter image description here

2
  • I tried your code, it works. So the Name field is automatically created.. I don't know when, but it's created if you fetched a soql result, but it's not possible with only filling the list as I did. I can't understand the logic, but ok. good to know .. Jun 29 '18 at 14:18
  • If it's works.. Please make it as best answer and Can you please close the question Jun 30 '18 at 12:14

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.