You need to sign in to do that
Don't have an account?
jhelbling
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 ...
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
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 ?
I think, if we debug "listOpp" and "m" before
then we will get the answer :-)
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Just did it, here is what I get :
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
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 :
However step 4 won't work, as showed with your previous posrt ...
Is there any alternative ?