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
Vladimir BessonovVladimir Bessonov 

how to cast one list into another?

I am getting APEX CPU issue as I have to process a lot of records 

I found one example from Salesforce on how to use Maps instead of slow list iteration. 

My question is how I can apply the same principle if I need to assign values of Map to another object. 
 
for (NonSerializedPart__c part : NonSerPartsConfig) {
        Double Total = part.Number_of_units__c * part.Quantity__c;
        project_Non_Serialized_Part__c NewPart = New project_Non_Serialized_Part__c(Name = part.Name, Project__c =ProjectID,
        Material_Number__c = part.Material_Number__c, Supplier__c = part.Supplier__c, 
        Supplier_Part_Number__c = part.Supplier_Part_Number__c, 
        Total_Quantity__c = Total ,Unit_of_measurement__c = part.Unit_of_measurement__c,
        Number_of_units__c = part.Number_of_units__c, unit_Quantity__c = part.Quantity__c);

        NonSerParts.add(NewPart);
        
        }
        insert NonSerParts;

so I need to assign somehow the values of Map to the list. A simple cast doesn't work in this case. 
 
Map<id, NonSerializedPart__c>  NSPM = new Map <id, NonSerializedPart__c>([Select ID, Name, Number_of_units__c, Quantity__c, Unit_of_measurement__c, Material_Number__c, Supplier_Part_Number__c, Supplier__c from NonSerializedPart__c where Project__c =: 'a3ae0000003ZTBmAAO']);


NonSerParts = (project_Non_Serialized_Part__c) NSPM.values();

I have 1 field in the object that the original one doesn't have. 
Double Total = part.Number_of_units__c * part.Quantity__c; 

Please advise. 




 
Suraj Tripathi 47Suraj Tripathi 47
Hi Vladimir,
Check this code:-
for (NonSerializedPart__c part : NonSerPartsConfig) {
  if(part.Number_of_units__c !=null && part.Quantity__c!=null && part.Material_Number__c!=null && part.Supplier__c!=null && part.Supplier_Part_Number__c!=null && part.Unit_of_measurement__c!=null && part.Number_of_units__c!=null){
        Double Total = part.Number_of_units__c * part.Quantity__c;
        project_Non_Serialized_Part__c NewPart = New project_Non_Serialized_Part__c(Name = part.Name, Project__c =ProjectID,
        Material_Number__c = part.Material_Number__c, Supplier__c = part.Supplier__c, 
        Supplier_Part_Number__c = part.Supplier_Part_Number__c, 
        Total_Quantity__c = Total ,Unit_of_measurement__c = part.Unit_of_measurement__c,
        Number_of_units__c = part.Number_of_units__c, unit_Quantity__c = part.Quantity__c);

        NonSerParts.add(NewPart);
        }
        }
        insert NonSerParts;

And maybe you are getting the error in the project__c field(line no. 6). Is projectId a variable or a field?
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
Vladimir BessonovVladimir Bessonov
it is var... No, your answer is absolutly missing the point.