[SalesForce] Contact – Name is Null, but LastName initialised


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

Best Answer

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

Related Topic