Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Re: Why do I get a null object ?
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Why do I get a null object ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 08:41 AM
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 ...
Re: Why do I get a null object ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 08:46 AM
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
Certified Developer | Twitter
Don't forget to give KUDOS if post helped you.
Latest Blog Posts : Streaming API- Easy Code AND Uploading Multiple Attachments into Salesforce - Simple Code
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Re: Why do I get a null object ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 08:49 AM
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 ?
Re: Why do I get a null object ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 08:52 AM
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
Certified Developer | Twitter
Don't forget to give KUDOS if post helped you.
Latest Blog Posts : Streaming API- Easy Code AND Uploading Multiple Attachments into Salesforce - Simple Code
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Re: Why do I get a null object ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 08:56 AM
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})
Re: Why do I get a null object ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 09:06 AM
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
Certified Developer | Twitter
Don't forget to give KUDOS if post helped you.
Latest Blog Posts : Streaming API- Easy Code AND Uploading Multiple Attachments into Salesforce - Simple Code
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Re: Why do I get a null object ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-04-2013 01:39 AM
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 :
- Build a Map<Opportunity,List<OpportunityLineItem>>
- Insert the value in the Map, at this time we can't set OpportunityId for item in List<OpportunityLineItem>
- Insert Opportunity (get them using keySet)
- Go over the Map and update OpportunityLineItem.OpportunitId field accordingly
- Insert OpportunitLineItem
However step 4 won't work, as showed with your previous posrt ...
Is there any alternative ?

