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
jhelblingjhelbling 

Why do I get a null object ?

Hi everyone,

 

When running the following code (anonymous APEX) :

 

Opportunity o1 = new Opportunity(Name='test',StageName='6',CloseDate=Date.newInstance(2013,4,4));
OpportunityLineItem oli1 = new OpportunityLineItem(Quantity=2);
List<OpportunityLineItem> listOli1 = new List<OpportunityLineItem>{oli1};
Map<Opportunity,List<OpportunityLineItem>> m = new Map<Opportunity,List<OpportunityLineItem>>{};
m.put(o1,listOli1);
List<Opportunity> listOpp = new List<Opportunity>{};
listOpp.addall(m.keySet());
insert listOpp;
For(Opportunity o : listOpp) {
	System.Debug(m.get(o));
}

 

My Debug message only shows "null" ....

 

Why can't I find the OLI within my Map ?

 

Thanks

 

P.S. : I can provide further information regarding the background explaining why I need to proceed this way ... but maybe somebody sees a stupid thing I don't see myself ...

Ankit AroraAnkit Arora

Is this it? I don't see code where you are relating opp line item to opportunity. There must be some exception, have you debug the code?

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

jhelblingjhelbling

Effectively the relation is not built yet, but the Map shouldn't be empty isn't it ?

 

I mean, o1 is the key for listOli1. Even if OLI are not linked to Opportunity (with OpportunityId field), when performing a get(o1) on the Map, shouldn't I receive the list of OLI back ?

Ankit AroraAnkit Arora

I think, if we debug "listOpp" and "m" before

 

For(Opportunity o : listOpp) {
	System.Debug(m.get(o));
}

 

then we will get the answer :-)

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

jhelblingjhelbling

Just did it, here is what I get :

 

17:55:36.537 (537498000)|USER_DEBUG|[17]|DEBUG|$$$M$$$ : {Opportunity:{Name=test, StageName=6, Id=006W0000003M6aCIAS, CloseDate=2013-04-04 00:00:00}=null}
17:55:36.537 (537576000)|USER_DEBUG|[18]|DEBUG|$$$listOpp$$$ : (Opportunity:{Name=test, StageName=6, Id=006W0000003M6aCIAS, CloseDate=2013-04-04 00:00:00})

 

Ankit AroraAnkit Arora

Okay try this code and you will get the answer:

 

System.debug('My Map Before Insert ::: ' + m) ;
insert listOpp;
System.debug('My Map After Insert ::: ' + m) ;

 

You are inserting the map and line items will not get inserted so they will be null after insert call.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

jhelblingjhelbling

Ok thanks for this, much more clearer now.

 

So is the following ever possible : I need to mass insert Opportunity and related OpportunityLineItem. I need to do it in a single shot to avoid governor limits to be reached. It will in fact be 2 operations : one to insert Opportunities and one to insert OpportunityLineItems.

 

In pseudo code, the idea was :

 

  1. Build a Map<Opportunity,List<OpportunityLineItem>>
  2. Insert the value in the Map, at this time we can't set OpportunityId for item in List<OpportunityLineItem>
  3. Insert Opportunity (get them using keySet)
  4. Go over the Map and update OpportunityLineItem.OpportunitId field accordingly
  5. Insert OpportunitLineItem

However step 4 won't work, as showed with your previous posrt ...

 

Is there any alternative ?