function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ankur SrivastavaAnkur Srivastava 

Value of Name field in OpportunityLineItem populating as NULL in the trigger

I have to insert the value of Name field present in OpportunityLineItem record in another holder object. I am using doing this by an after insert trigger on OpportunityLineItem object. All the other fields are getting inserted on the trigger execution. But the Name field is populating as null in the Holder object.

Sample code: (just for ref.)

List <B> b = new List<B>();

for(OpportunityLineItem opli : Trigger.New )
{
  B bi = new B();

 bi.Product_Name__c = opli.Name;
b.add(bi);
}
insert b;

I have skipped the other fields in the snippet..

Please let me know how to resolve this.
Best Answer chosen by Ankur Srivastava
Ankur SrivastavaAnkur Srivastava
Bob.. It is indeed peculiar issue. Since its an after insert trigger, so Trigger.New should populate the value of Name field.

I have to now modify my code to now select the name field based on the OpportunityLineItemId from Trigger.New. And populate the same to the corresponding field of my holder object.
This approach is though not the best practice, but still can't help.

All Answers

bob_buzzardbob_buzzard
That seems very peculiar - have you added some debug information to the trigger to see what the value of the opportunity line item name field is?  Do you have any triggers/workflow on the holder object that could be clearing the field down when it is saved to the database?
Ankur SrivastavaAnkur Srivastava
Yes i did add system.debug to check the value of opli.Name. I got NULL as the value. Its the only trigger under execution. No other trigger/ WF is defined for the object. The values of all the other fields are getting populated in my Holder object. But only Name field is giving NULL. 
I even executed a SOQL query on my object to check for the value of NAME field.. There I am getting relevant value.
But that value is not populating in my trigger execution
bob_buzzardbob_buzzard
According to the docs this is a read only field which suggests it is populated by the database.  It sounds like you are hitting a race condition of some sort, or that the transaction needs to commit before this field is populated.

Can you use a formula field for this?  Failing that, It sounds like you'll need to do this asynchronously - I'd be inclined to look at time based workflow if you can live with the fact that you can't set the criteria to when the record is created and every time it is edited.  If not, you'll need to use an @future call or set up some scheduled apex to fix things up.
Ankur SrivastavaAnkur Srivastava
Bob.. It is indeed peculiar issue. Since its an after insert trigger, so Trigger.New should populate the value of Name field.

I have to now modify my code to now select the name field based on the OpportunityLineItemId from Trigger.New. And populate the same to the corresponding field of my holder object.
This approach is though not the best practice, but still can't help.
This was selected as the best answer