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
davidjbbdavidjbb 

Apex List

                  for(jbbfc2__MaterialU__c mu :[select jbbfc2__Workorder__c,jbbfc2__Workorder__r.Id, Name,
                  	jbbfc2__Material__r.jbbfc2__ProductCode__c,jbbfc2__Material__r.jbbfc2__UnitPrice__c,jbbfc2__quantity__c
                  	from jbbfc2__MaterialU__c where jbbfc2__Workorder__r.jbbfc2__Status__c ='Closed'])
                  {
              		  
              		  System.debug('Workorder Id' + mu.jbbfc2__Workorder__r.Id);
              		  System.debug('Material' + mu.Name);
              		  
              		  matList.add(mu);
              		  if(muMap.get(mu.jbbfc2__Workorder__r.Id)==null){	
              		  	
                      	muMap.put(mu.jbbfc2__Workorder__r.Id, matList);
              		  }
                  }

 

For matList.add(mu);

It's adding all the materials to the matList regardless which jbbfc2__Workorder__r.Id it belongs too..How can i fix this? Right now i'm just adding all the records in the matList

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

If you are trying to have a materials list per workorder, you need to use the one in the map.

 

Something like:

 

                  for(jbbfc2__MaterialU__c mu :[select jbbfc2__Workorder__c,jbbfc2__Workorder__r.Id, Name,
                  	jbbfc2__Material__r.jbbfc2__ProductCode__c,jbbfc2__Material__r.jbbfc2__UnitPrice__c,jbbfc2__quantity__c
                  	from jbbfc2__MaterialU__c where jbbfc2__Workorder__r.jbbfc2__Status__c ='Closed'])
                  {
              		  
              		  System.debug('Workorder Id' + mu.jbbfc2__Workorder__r.Id);
              		  System.debug('Material' + mu.Name);
              		  List<jbbfc2__MaterialU__c> matsForWO=muMap.get(mu.jbbfc2__Workorder__r.Id);
                          if (matsForWO==null)
                          {
                             matsForWO=new List<jbbfc2__MaterialU__c>();
                             muMap.put(mu.jbbfc2__Workorder__r.Id, matsForWO);
                          }
              		  matsForWO.add(mu);
                  }

 This retrieves the list for the workorder from the map, creates a new one if it is null and stores it in the map, and then appends the material to the list.

All Answers

bob_buzzardbob_buzzard

If you are trying to have a materials list per workorder, you need to use the one in the map.

 

Something like:

 

                  for(jbbfc2__MaterialU__c mu :[select jbbfc2__Workorder__c,jbbfc2__Workorder__r.Id, Name,
                  	jbbfc2__Material__r.jbbfc2__ProductCode__c,jbbfc2__Material__r.jbbfc2__UnitPrice__c,jbbfc2__quantity__c
                  	from jbbfc2__MaterialU__c where jbbfc2__Workorder__r.jbbfc2__Status__c ='Closed'])
                  {
              		  
              		  System.debug('Workorder Id' + mu.jbbfc2__Workorder__r.Id);
              		  System.debug('Material' + mu.Name);
              		  List<jbbfc2__MaterialU__c> matsForWO=muMap.get(mu.jbbfc2__Workorder__r.Id);
                          if (matsForWO==null)
                          {
                             matsForWO=new List<jbbfc2__MaterialU__c>();
                             muMap.put(mu.jbbfc2__Workorder__r.Id, matsForWO);
                          }
              		  matsForWO.add(mu);
                  }

 This retrieves the list for the workorder from the map, creates a new one if it is null and stores it in the map, and then appends the material to the list.

This was selected as the best answer
davidjbbdavidjbb

Exactly what I needed. Thanks!!

Learning so much today.