+ Start a Discussion
Masechaba Maseli 8Masechaba Maseli 8 

list has more than 1 row for assignment

Hi all,

May I have some assistance with the below code , this works perfectly if one record is being created but when i create multiple records at the same time I get an error that the List has more than 1 row for assignment to SObject.  

Can someone take a look at what I may be doing wrong. This is the line where the error is originating. 

newSOWrappers.Add(new SOWrapper(shipmentOrder));
 
public class SOWrapper
    {
        public SOWrapper(Shipment_Order__c so, boolean recalculateCosts)
        {
            this.SO = so;
            this.CAP = [SELECT Related_Costing_CPA__c, VAT_Rate__c  FROM CPA_v2_0__c WHERE ID =: so.CPA_v2_0__c];
            this.Client = so.Account__r;

            if (recalculateCosts )
                recalculateCostings();
            else
                buildCostings();
        }

        public SOWrapper(Shipment_Order__c so)
        {
            this.SO = so;
            this.CAP = [SELECT Related_Costing_CPA__c, VAT_Rate__c FROM CPA_v2_0__c WHERE ID =: so.CPA_v2_0__c];
            this.Client = so.Account__r;

            if (so.CPA_v2_0__c != null)

            buildCostings();
        }

        public Shipment_Order__c SO { set; get; }
        public CPA_v2_0__c CAP { set; get; }
        public Account Client { set; get; }
        private List<CPA_Costing__c> relatedCostings;
        private List<CPA_Costing__c> relatedCostingsIOR;
        private List<CPA_Costing__c> relatedCostingsEOR;
        private List<CPA_Costing__c> addedCostings;
        private List<CPA_Costing__c> updatedCostings;

        private Id recordTypeId = Schema.SObjectType.CPA_Costing__c.getRecordTypeInfosByName()
                .get('Display').getRecordTypeId();

        public void buildCostings()
        {
            relatedCostingsIOR = [SELECT Name, Shipment_Order_Old__c, Amount__c, Applied_to__c, Ceiling__c, Conditional_value__c, Condition__c, Floor__c, Cost_Category__c, Cost_Type__c, Max__c, Min__c, Rate__c, CostStatus__c, Updating__c, RecordTypeID, IOR_EOR__c, VAT_applicable__c, Variable_threshold__c, VAT_Rate__c, CPA_v2_0__r.VAT_Rate__c  FROM CPA_Costing__c WHERE CPA_v2_0__c =: CAP.Id AND IOR_EOR__c = 'IOR' ];
            relatedCostingsEOR = [SELECT Name, Shipment_Order_Old__c, Amount__c, Applied_to__c, Ceiling__c, Conditional_value__c, Condition__c, Floor__c, Cost_Category__c, Cost_Type__c, Max__c, Min__c, Rate__c, CostStatus__c, Updating__c, RecordTypeID, IOR_EOR__c, VAT_applicable__c, Variable_threshold__c, VAT_Rate__c, CPA_v2_0__r.VAT_Rate__c  FROM CPA_Costing__c WHERE CPA_v2_0__c =: CAP.Id AND IOR_EOR__c = 'EOR'];       
    
         
         If(SO.Service_Type__c == 'IOR') {
          
            List<CPA_Costing__c>  SOCPAcostingsIOR = relatedCostingsIOR.deepClone();
           
            for(CPA_Costing__c cost : SOCPAcostingsIOR)
            
            {
           
                cost.Shipment_Order_Old__c = SO.ID;
                cost.CPA_v2_0__c = CAP.Related_Costing_CPA__c;
                cost.recordTypeID = recordTypeId;
                cost.VAT_Rate__c =  CAP.VAT_Rate__c;
                
                

                if (SO.Shipment_Value_USD__c != null)
                {
                    cost.Updating__c = !cost.Updating__c;
                    SOTriggerHelper.calculateSOCostingAmount(so, cost, false);
                }
            }
            addedCostings = SOCPAcostingsIOR;
        }
        
        Else If(SO.Service_Type__c == 'EOR') {
          
            List<CPA_Costing__c>  SOCPAcostingsEOR = relatedCostingsEOR.deepClone();
           
            for(CPA_Costing__c cost : SOCPAcostingsEOR)
            
            {
           
                cost.Shipment_Order_Old__c = SO.ID;
                cost.CPA_v2_0__c = CAP.Related_Costing_CPA__c;
                cost.recordTypeID = recordTypeId;
                cost.VAT_Rate__c =  CAP.VAT_Rate__c; 

                if (SO.Shipment_Value_USD__c != null && (SO.FC_Total__c == null || SO.FC_Total__c == 0))
                {
                    cost.Updating__c = !cost.Updating__c;
                    SOTriggerHelper.calculateSOCostingAmount(so, cost, false);
                }
            }
            addedCostings = SOCPAcostingsEOR;
        }
        
        }


         public void recalculateCostings()
        {
            
            relatedCostings = [SELECT Name, Shipment_Order_Old__c, IOR_EOR__c,Amount__c, Applied_to__c, Ceiling__c, Conditional_value__c, Condition__c, Floor__c, Cost_Category__c, Cost_Type__c, Max__c, Min__c, Rate__c, CostStatus__c, Updating__c, RecordTypeID, VAT_Rate__c, VAT_applicable__c, Variable_threshold__c FROM CPA_Costing__c WHERE Shipment_Order_Old__c =: SO.ID];
            System.debug('relatedCostings --- '+relatedCostings.size());
            
            List<CPA_Costing__c> delList = new List<CPA_Costing__c>();
            for(CPA_Costing__c var: relatedCostings )
            {
                if(var.Cost_Type__c == 'Variable'){
                    CPA_Costing__c del = new CPA_Costing__c();
                    del.id =var.id;
                    delList.add(del);                    
                }
            }
            

            
            updatedCostings = new List<CPA_Costing__c>();
            System.debug('updatedCostings --- '+updatedCostings.size());
            List<CPA_Costing__c>  relatedTemplateCPAcostings = [SELECT Name, IOR_EOR__c,Shipment_Order_Old__c, Amount__c, Applied_to__c, Ceiling__c, Conditional_value__c, Condition__c, Floor__c, Cost_Category__c, Cost_Type__c, Max__c, Min__c, Rate__c, CostStatus__c, Updating__c, RecordTypeID, CPA_v2_0__r.VAT_Rate__c, VAT_Rate__c, VAT_applicable__c, Variable_threshold__c FROM CPA_Costing__c WHERE CPA_v2_0__c =: CAP.Id ];
               
               List<CPA_Costing__c> updateCosting = new List<CPA_Costing__c >(); 
               Set<Id> ids = new Set<id>();
              for (CPA_Costing__c templateCost : relatedTemplateCPAcostings)
              {    
               
                  SOTriggerHelper.calculateSOCostingAmount(so, templateCost, false);                       
                  System.debug('The cost id; ----------# '+templateCost.Id+' Amount:- '+templateCost.Amount__c);
                  for(CPA_Costing__c cost : relatedCostings){
                      if (templateCost.Cost_Category__c == cost.Cost_Category__c && templateCost.Name != cost.Name && templateCost.Cost_Type__c == cost.Cost_Type__c && templateCost.Applied_to__c == cost.Applied_to__c && templateCost.IOR_EOR__c == cost.IOR_EOR__c){
                           System.debug(' Do update: '+cost.Id+' the amount '+templateCost.Amount__c+'  --- name '+ templateCost.Name);
                            if(!ids.contains(cost.id) && templateCost.Amount__c > -1){
                                cost.Id = cost.Id;  
                                cost.Name =  templateCost.Name;
                                cost.Amount__c =  templateCost.VAT_applicable__c == TRUE ? templateCost.Amount__c * (1 + (CAP.VAT_Rate__c/ 100)): templateCost.Amount__c;
                                cost.Ceiling__c = templateCost.Ceiling__c;
                                cost.Conditional_value__c = templateCost.Conditional_value__c;
                                cost.Condition__c = templateCost.Condition__c;
                                cost.Cost_Type__c = templateCost.Cost_Type__c;
                                cost.Floor__c = templateCost.Floor__c;
                                cost.Max__c = templateCost.Max__c;
                                cost.Min__c = templateCost.Min__c;
                                cost.Rate__c = templateCost.Rate__c;
                                cost.Variable_threshold__c = templateCost.Variable_threshold__c;
                                cost.VAT_Rate__c = templateCost.CPA_v2_0__r.VAT_Rate__c; 
                                cost.Cost_Category__c   = templateCost.Cost_Category__c ;                                  
                                cost.IOR_EOR__c = templateCost.IOR_EOR__c;                                
                                updateCosting.add(cost);
                                ids.add(cost.id);
                            }
                      }                   
                  }    
              }        
     if(!updateCosting.IsEmpty()){       
        update updateCosting;
     }
            List<CPA_Costing__c> updateCostList = new List<CPA_Costing__c>();
            for(CPA_Costing__c cost : relatedCostings)
            {
                cost.Updating__c = !cost.Updating__c;
                SOTriggerHelper.calculateSOCostingAmount(so, cost, false);
                  System.debug('The cost id; --------'+cost.Id+' Amount:- '+cost.Amount__c);
                  IF(cost.Amount__c == -1){
                                    

                  }
                System.debug(' --- cost '+cost.Amount__c);
                if (cost.Amount__c >= 0)
                {
                System.debug(' --- amount '+relatedTemplateCPAcostings);
                    for (CPA_Costing__c templateCost : relatedTemplateCPAcostings)
                    {
                    
                   
                    System.debug(' --- templateCost ');
                        if (templateCost.Cost_Category__c == cost.Cost_Category__c && templateCost.Name == cost.Name)
                        {
                            cost.Updating__c = !cost.Updating__c;
                            SOTriggerHelper.calculateSOCostingAmount(so, templateCost, false);
                             System.debug(' --- templateCost2  '+templateCost.Amount__c);

                            if (templateCost.Amount__c > 0)
                            {
                             System.debug(' ---inside  '+templateCost.Amount__c);
                                cost.Amount__c =  templateCost.VAT_applicable__c == TRUE ? templateCost.Amount__c * (1 + (CAP.VAT_Rate__c/ 100)): templateCost.Amount__c;
                                cost.Ceiling__c = templateCost.Ceiling__c;
                                cost.Conditional_value__c = templateCost.Conditional_value__c;
                                cost.Condition__c = templateCost.Condition__c;
                                cost.Cost_Type__c = templateCost.Cost_Type__c;
                                cost.Floor__c = templateCost.Floor__c;
                                cost.Max__c = templateCost.Max__c;
                                cost.Min__c = templateCost.Min__c;
                                cost.Rate__c = templateCost.Rate__c;
                                cost.Variable_threshold__c = templateCost.Variable_threshold__c;
                                cost.VAT_Rate__c = templateCost.CPA_v2_0__r.VAT_Rate__c;
                                
                              system.debug('THE COST ' +  cost.ID+' '+cost.Name);  
                                
                            }
                        }
                    }
                }

                updatedCostings.Add(cost);
            }
                       
            if(!updateCostList.IsEmpty()){
                    insert updateCostList;
            }
        }
        
        
        

        public List<CPA_Costing__c> getApplicableCostings()
        {
            List<CPA_Costing__c> applicableCostings = new List<CPA_Costing__c>();
            if (addedCostings != null)
                for (CPA_Costing__c cost : addedCostings)
                    if (cost.Amount__c > -1 && cost.Shipment_Order_Old__c != null)
                        applicableCostings.Add(cost);
            if (updatedCostings != null)
                for (CPA_Costing__c cost : updatedCostings)
                    if (cost.Amount__c > -1)
                        applicableCostings.Add(cost);

            return applicableCostings;
        }
    }

public class CalculateCosts
{

    @InvocableMethod
    public static void createSOCosts(List<Id> SOIds)  
    
    
    {
        List<SOWrapper> newSOWrappers = new List<SOWrapper>();
        List<ID> SOsToUpdate = new List<Id>();
        
        List<Shipment_Order__c> shipmentOrder = [Select Id, Name,  Insurance_Cost_CIF__c, Shipment_Value_USD__c, IOR_Fee_new__c, FC_IOR_and_Import_Compliance_Fee_USD__c, FC_Total_Customs_Brokerage_Cost__c,
        FC_Total_Handling_Costs__c, FC_Total_License_Cost__c, FC_Total_Clearance_Costs__c, FC_Insurance_Fee_USD__c, FC_Miscellaneous_Fee__c, On_Charge_Mark_up__c , FC_International_Delivery_Fee__c,
        TecEx_Shipping_Fee_Markup__c, CPA_v2_0__c, Chargeable_Weight__c, Account__c, Service_Type__c, Total_Taxes__c FROM Shipment_Order__c where id IN :SOIds];
        
        List<Shipment_Order__c> SOs = new List<Shipment_Order__c>();

        List<CPA_Costing__c> costingsToAddUpdate = new List<CPA_Costing__c>();

        List<Shipment_Order__c> updatedSOs =
        [SELECt Id, Name,  Insurance_Cost_CIF__c, Shipment_Value_USD__c, IOR_Fee_new__c, FC_IOR_and_Import_Compliance_Fee_USD__c, FC_Total_Customs_Brokerage_Cost__c,
        FC_Total_Handling_Costs__c, FC_Total_License_Cost__c, FC_Total_Clearance_Costs__c, FC_Insurance_Fee_USD__c, FC_Miscellaneous_Fee__c, On_Charge_Mark_up__c , FC_International_Delivery_Fee__c,
        TecEx_Shipping_Fee_Markup__c, CPA_v2_0__c, Chargeable_Weight__c    FROM Shipment_Order__c  WHERE ID in: SOsToUpdate];
        
        newSOWrappers.Add(new SOWrapper(shipmentOrder));

        if (newSOWrappers.size() > 0 && shipmentOrder.size() > 0 )
       {
            for (SOWrapper wrp : newSOWrappers)
           {
                Shipment_Order__c SO;
               for (Shipment_Order__c SOToUpdate : updatedSOs)
                    if (SOToUpdate.ID == wrp.SO.ID)
                    {
      
        
                    SO = SOToUpdate;
                       break;
          
                  }

               costingsToAddUpdate.AddAll(wrp.getApplicableCostings());
                 
            }
            
           

            
        }
         Upsert costingsToAddUpdate;
        update SOs;
        
        
        
       }
            
                                                   
        
    }

 
Best Answer chosen by Masechaba Maseli 8
Steven NsubugaSteven Nsubuga
shipmentOrder is a list of Shipment_Order__c and yet the SOWrapper constructor only accepts a single Shipment_Order__c record.
Try this in stead
public class CalculateCosts
{

    @InvocableMethod
    public static void createSOCosts(List<Id> SOIds)  
    
    
    {
        List<SOWrapper> newSOWrappers = new List<SOWrapper>();
        List<ID> SOsToUpdate = new List<Id>();
        
        List<Shipment_Order__c> shipmentOrder = [Select Id, Name,  Insurance_Cost_CIF__c, Shipment_Value_USD__c, IOR_Fee_new__c, FC_IOR_and_Import_Compliance_Fee_USD__c, FC_Total_Customs_Brokerage_Cost__c,
        FC_Total_Handling_Costs__c, FC_Total_License_Cost__c, FC_Total_Clearance_Costs__c, FC_Insurance_Fee_USD__c, FC_Miscellaneous_Fee__c, On_Charge_Mark_up__c , FC_International_Delivery_Fee__c,
        TecEx_Shipping_Fee_Markup__c, CPA_v2_0__c, Chargeable_Weight__c, Account__c, Service_Type__c, Total_Taxes__c FROM Shipment_Order__c where id IN :SOIds];
        
        List<Shipment_Order__c> SOs = new List<Shipment_Order__c>();

        List<CPA_Costing__c> costingsToAddUpdate = new List<CPA_Costing__c>();

        List<Shipment_Order__c> updatedSOs =
        [SELECt Id, Name,  Insurance_Cost_CIF__c, Shipment_Value_USD__c, IOR_Fee_new__c, FC_IOR_and_Import_Compliance_Fee_USD__c, FC_Total_Customs_Brokerage_Cost__c,
        FC_Total_Handling_Costs__c, FC_Total_License_Cost__c, FC_Total_Clearance_Costs__c, FC_Insurance_Fee_USD__c, FC_Miscellaneous_Fee__c, On_Charge_Mark_up__c , FC_International_Delivery_Fee__c,
        TecEx_Shipping_Fee_Markup__c, CPA_v2_0__c, Chargeable_Weight__c    FROM Shipment_Order__c  WHERE ID in: SOsToUpdate];
        
		for (Shipment_Order__c sho : shipmentOrder) {
			newSOWrappers.Add(new SOWrapper(sho));
		}

        if (newSOWrappers.size() > 0 && shipmentOrder.size() > 0 )
       {
            for (SOWrapper wrp : newSOWrappers)
           {
                Shipment_Order__c SO;
               for (Shipment_Order__c SOToUpdate : updatedSOs)
                    if (SOToUpdate.ID == wrp.SO.ID)
                    {
      
        
                    SO = SOToUpdate;
                       break;
          
                  }

               costingsToAddUpdate.AddAll(wrp.getApplicableCostings());
                 
            }
            
           

            
        }
         Upsert costingsToAddUpdate;
        update SOs;
        
        
        
       }
            
                                                   
        
    }


 

All Answers

Steven NsubugaSteven Nsubuga
shipmentOrder is a list of Shipment_Order__c and yet the SOWrapper constructor only accepts a single Shipment_Order__c record.
Try this in stead
public class CalculateCosts
{

    @InvocableMethod
    public static void createSOCosts(List<Id> SOIds)  
    
    
    {
        List<SOWrapper> newSOWrappers = new List<SOWrapper>();
        List<ID> SOsToUpdate = new List<Id>();
        
        List<Shipment_Order__c> shipmentOrder = [Select Id, Name,  Insurance_Cost_CIF__c, Shipment_Value_USD__c, IOR_Fee_new__c, FC_IOR_and_Import_Compliance_Fee_USD__c, FC_Total_Customs_Brokerage_Cost__c,
        FC_Total_Handling_Costs__c, FC_Total_License_Cost__c, FC_Total_Clearance_Costs__c, FC_Insurance_Fee_USD__c, FC_Miscellaneous_Fee__c, On_Charge_Mark_up__c , FC_International_Delivery_Fee__c,
        TecEx_Shipping_Fee_Markup__c, CPA_v2_0__c, Chargeable_Weight__c, Account__c, Service_Type__c, Total_Taxes__c FROM Shipment_Order__c where id IN :SOIds];
        
        List<Shipment_Order__c> SOs = new List<Shipment_Order__c>();

        List<CPA_Costing__c> costingsToAddUpdate = new List<CPA_Costing__c>();

        List<Shipment_Order__c> updatedSOs =
        [SELECt Id, Name,  Insurance_Cost_CIF__c, Shipment_Value_USD__c, IOR_Fee_new__c, FC_IOR_and_Import_Compliance_Fee_USD__c, FC_Total_Customs_Brokerage_Cost__c,
        FC_Total_Handling_Costs__c, FC_Total_License_Cost__c, FC_Total_Clearance_Costs__c, FC_Insurance_Fee_USD__c, FC_Miscellaneous_Fee__c, On_Charge_Mark_up__c , FC_International_Delivery_Fee__c,
        TecEx_Shipping_Fee_Markup__c, CPA_v2_0__c, Chargeable_Weight__c    FROM Shipment_Order__c  WHERE ID in: SOsToUpdate];
        
		for (Shipment_Order__c sho : shipmentOrder) {
			newSOWrappers.Add(new SOWrapper(sho));
		}

        if (newSOWrappers.size() > 0 && shipmentOrder.size() > 0 )
       {
            for (SOWrapper wrp : newSOWrappers)
           {
                Shipment_Order__c SO;
               for (Shipment_Order__c SOToUpdate : updatedSOs)
                    if (SOToUpdate.ID == wrp.SO.ID)
                    {
      
        
                    SO = SOToUpdate;
                       break;
          
                  }

               costingsToAddUpdate.AddAll(wrp.getApplicableCostings());
                 
            }
            
           

            
        }
         Upsert costingsToAddUpdate;
        update SOs;
        
        
        
       }
            
                                                   
        
    }


 
This was selected as the best answer
Masechaba Maseli 8Masechaba Maseli 8
@Steven Nsubuga thank you- that worked
Masechaba Maseli 8Masechaba Maseli 8
One more question- I hit the SOQL limits when trying to process more than 10 records. What changes can be done to make sure this doesnt happen?
Steven NsubugaSteven Nsubuga
Hi Masechaba, you need to bulkify the code. Remove the query from the constructor and have it outside. 
This would mean you have all the queries in the code that calls the constructor, and initializes the CAP record at the same time as the Shipment Order. This is very possible because the CPA_v2_0__c and the Shipment_Order__c are related.

You would need a new constructor, one without a query. Like this:
    public SOWrapper(Shipment_Order__c so, CPA_v2_0__c cap)
	{
		this.SO = so;
		this.CAP = cap;
		this.Client = so.Account__r;

		if (so.CPA_v2_0__c != null)

		buildCostings();
	}

Then you would call this constructor in your logic
List <Shipment_Order__c> shipmentOrder = [Select Id, Name,  Insurance_Cost_CIF__c, Shipment_Value_USD__c, IOR_Fee_new__c, FC_IOR_and_Import_Compliance_Fee_USD__c, FC_Total_Customs_Brokerage_Cost__c,
        FC_Total_Handling_Costs__c, FC_Total_License_Cost__c, FC_Total_Clearance_Costs__c, FC_Insurance_Fee_USD__c, FC_Miscellaneous_Fee__c, On_Charge_Mark_up__c , FC_International_Delivery_Fee__c,
        TecEx_Shipping_Fee_Markup__c, CPA_v2_0__c, Chargeable_Weight__c, Account__c, Service_Type__c, Total_Taxes__c FROM Shipment_Order__c where id IN :SOIds];

// a set to contain all the unique CPA_v2_0__cs
Set<Id> soCpas = new Set<Id>();		
for (Shipment_Order__c sho : shipmentOrder) {
	soCpas.add(sho.CPA_v2_0__c);
}
		
Map<Id, CPA_v2_0__c> caps = new Map<Id, CPA_v2_0__c> ([SELECT Id, Related_Costing_CPA__c, VAT_Rate__c FROM CPA_v2_0__c WHERE ID IN :soCpas]);

for (Shipment_Order__c sho : shipmentOrder) {
	newSOWrappers.Add(new SOWrapper(sho, caps.get(so.CPA_v2_0__c)));
}

That is the general flow of how I would bulkify the code. Modify it as you see fit.

 
Masechaba Maseli 8Masechaba Maseli 8
Hi,

I will try that and let you know how it went- thank you for your help today.