0

I am inserting a lead record from my test class. When I am printing the name field which is a non writeable field I am getting null. Any help would be appreciated.

   Lead l1 = new Lead(FirstName = 'test23',LastName = 'test12',Status = 'Open',Company='testco',Customer_Mobile__c = '8787878787',DOB__c = date.parse('06/07/1982'),
                         Email = 'test@test.com',SBS_Branch__c = bm.id,Employer__c = segm.id,Resi_Pin_Code__c = '411020',State__c = 'Maharashtra',City__c = 'Pune',
                         Customer_Type__c = 'Individual',Resi_City__c = 'Pune',PAN__c = 'AWEPT9807P');
     leadlist.add(l1);

     insert leadlist; 
     System.debug('LEad name+++'+l1.name+l1.firstname+l1.lastname);
1
  • 1
    It needs to be queried from database. Compound fields (like name) or formulas or any updated in trigger/pb fields are not returned from salesforce database on insert/update (dml) operations – kurunve Feb 2 '17 at 8:01
2

Note from the documentation on Lead that the Name field is a:

Concatenation of FirstName, MiddleName, LastName, and Suffix

Since it is a calculated field, you will have to query back for it:

Lead insertedRecord = [SELECT Name FROM Lead WHERE Id = :l1.Id];
system.assertEquals(/*expected value*/, insertedRecord.Name, '<assertion_message_here>');
2
  • name field is a combination of Salutation,First Name, Last Name, if you only passl1.name its shows null always – cloudZigZag Feb 2 '17 at 8:15
  • 1
    The point is that you have to query for the data. It doesn't get set in memory. – Adrian Larson Feb 2 '17 at 8:18
1

The Name field is not set on your newly inserted lead object. This field is calculated after the insert.

You have to request the field on a SELECT to be able to get the field. Like this:

Lead l1 = new Lead(FirstName = 'test23',LastName = 'test12',Status = 'Open',Company='testco',Customer_Mobile__c = '8787878787',DOB__c = date.parse('06/07/1982'),
                             Email = 'test@test.com',SBS_Branch__c = bm.id,Employer__c = segm.id,Resi_Pin_Code__c = '411020',State__c = 'Maharashtra',City__c = 'Pune',
                             Customer_Type__c = 'Individual',Resi_City__c = 'Pune',PAN__c = 'AWEPT9807P');
leadlist.add(l1);

insert leadlist; 

l1 = [SELECT FirstName, LastName, Name FROM Lead WHERE Id = :l1.Id];

System.debug('LEad name+++'+l1.name+l1.firstname+l1.lastname);
3
  • You don't have to delete your perfectly valid post just because I beat you to the punch by a matter of seconds! You should undelete. :) – Adrian Larson Feb 2 '17 at 8:19
  • Hi @AdrianLarson. I recognize the defeat :D I thought that my post wasn't needed after your answer. – Martin Lezer Feb 2 '17 at 8:33
  • Not needed, sure. But in those cases where a few people are typing the answer at the same time and post within a few seconds/minutes of each other, it doesn't really hurt anything. It's only really an issue when there's a difference of hours or more, where it then starts to seem like the answer is just a copy of someone else's work. Clearly not the case here. Also your post is closer to what the OP wrote, so who knows, it may actually help them understand the concept better! – Adrian Larson Feb 2 '17 at 15:23

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.