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
Somasundaram S 1Somasundaram S 1 

error while update the parent record

I am getting below error while update the value in parent record from the child reocrds collection
Apex trigger Totalquanityleft caused an unexpected exception, contact your administrator: Totalquanityleft: execution of AfterUpdate caused by: System.TypeException: Invalid id value for this SObject type: a0i0E0000000YDuQAM: Trigger.Totalquanityleft: line 31, column 1
My apologies am not so good in cdoing
sample lot is parent object, sample tarnsaction is child , quantiyty left is parent obecjt field, total quantity is child object field 
 
trigger Totalquanityleft   on Sample_Transaction__c( after insert, after update,after delete,after undelete) {
     Set<Id> transactionIdSet=new Set<Id>();
     List<Sample_Lot__c> ordListToUpdate=new List<Sample_Lot__c>();
     if(Trigger.isInsert || Trigger.isUndelete){
          for(Sample_Transaction__c qnty: Trigger.new){
             if(qnty.id != null){
                transactionIdSet.add(qnty.Id);
             }    
          }

     }

     if(Trigger.isUpdate){
        for(Sample_Transaction__c qnty: Trigger.new){
             if(qnty.id != null && qnty.Transaction_Quantity__c != trigger.oldMap.get(qnty.Id).Transaction_Quantity__c){
                 transactionIdSet.add(qnty.Id);
             }    
         }
     }
     If(Trigger.isDelete){
        for(Sample_Transaction__c qnty : Trigger.old){
            if(qnty.Id != null){
                 transactionIdSet.add(qnty.Id);
           }     
        }

   }

    if(!transactionIdSet.isEmpty()){
        for(AggregateResult res : [SELECT id,sum(Transaction_Quantity__c)can FROM Sample_Transaction__c  WHERE id IN :transactionIdSet GROUP BY id]) {
        ordListToUpdate.add(new Sample_Lot__c (Id=(Id)res.get('Sample_Lot__c.id'),Quantity_Left__c=(Double)res.get('can')));
        //ordListToUpdate.add(new Order(Id=(Id)res.get('OrderId'),Total_Pieces1__c=(Double)res.get('can')));    
           
           
        }
        
    }
    if(!ordListToUpdate.isEmpty()){
        try{
            update ordListToUpdate;
         }catch(DmlException de){
            System.debug(de);
         }



     }



}

 
HARSHIL U PARIKHHARSHIL U PARIKH
Here is what I think the problem is but it can be something else,

On line 7, you are putting a record Id. You need to put the lookup field value there though.
If Sample_Lot__c is parent and Sample_Transaction__c child then find out the name of lookup field. ( e.g., Sample_Lot__c ) and change the code in line 7 and on 23 as following,
transactionIdSet.add(qnty.Sample_Lot__c);
Or use the trigger below,
 
// Sample_Lot__c is parent object, Sample_Tarnsaction__c is child , quantiyty left is parent obecjt field, total quantity is child object field 

Trigger Totalquanityleft on Sample_Tarnsaction__c (After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> sampleLotsIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Sample_Tarnsaction__c st : Trigger.New){
            If(st.Sample_Lot__c != null){
                sampleLotsIds.add(st.Sample_Lot__c );
            }
        }
    }
    If(Trigger.IsDelete){
        For(Sample_Tarnsaction__c st : Trigger.old){
            If(st.Sample_Lot__c != null){
                sampleLotsIds.add(st.Sample_Lot__c );
            }
        }
    }
    
    List<Sample_Lot__c> ordListToUpdate=new List<Sample_Lot__c>();
    
    
    For(Sample_Lot__c SampleLot: [Select Id, Quantity_Left__c, (Select Id, total_quantity__c FROM Sample_Tarnsaction__r) FROM Sample_Lot__c WHERE Id =:sampleLotsIds])
    {
        SampleLot.Quantity_Left__c = 0.00;
        
        For(Sample_Tarnsaction__r EverySampleTrans : SampleLot.Sample_Tarnsaction__r)
        {
            If(EverySampleTrans.total_quantity__c  != null)
            {
                SampleLot.Quantity_Left__c += EverySampleTrans.total_quantity__c;
            }
        }
        ordListToUpdate.add(SampleLot)
        
    }
    try{
        If(!ordListToUpdate.IsEmpty()){
            update ordListToUpdate;
        }
    }
    Catch(Exception e){
        System.debug('Thrown Exception Is:: ' + e.getMessage());
    }
    
}