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
larvalarva 

Apex Trigger LOOK UP RELATIONSHIP

Apex Trigger code I have to get the sum of "field : total_order_amount" in childs Object which is Status ="Closed". But only who choose from the Parent Object Record type ="Single Order".

1st OBJECT "Child" 
OBJECT NAME: Order
field: Open and Closed
field : total_order_amount.

2nd Object "MASTER DETAIL"
OBJECT NAME:
SureBuyer__c
Record Type: Single Order and Bulk Order.
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

trigger ContactTrigger on Contact (After Insert) {
    
   if(trigger.isAfter && trigger.isInsert){
set<Id> accountSetId=new set<Id>();
for(Contact con:trigger.new){
if(con.status__c=='Closed'){
accountSetId.add(con.accountid);
}

}
List<Contact> contactList=new List<Contact>([select id, accountId from Contact where accountid in: accountSetId]);
List<Account> accountList=new List<Account>([select id from Account where id in: accountSetId and recordtype.Name='Single Order']);
       for(Account ac:accountList){
           Integer sum=0;
           for(Contact con:contactList){
               
               if(ac.Id==con.accountid){
                   sum=sum+con.total_order_amount;
               }
           }
           ac.Total__c=sum;
           
       }
       
       if(accountList.size()>0){
           update accountList;
       }

   }
   }

Please do some needful changes according to your code.
Please mark it as the Best Answer if it helps you.

Thank You

CharuDuttCharuDutt
Hii md0000
Try Below Trigger
SureBuyer__c Lookup field on order Object

trigger SumAmountBuyer on Order(After Insert,After Update,After Delete) {
 List<SureBuyer__c> oList=new List<SureBuyer__c>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Order con : Trigger.new){
        if(Status == "Closed"){
            setAccIds.add(con.SureBuyer__c );
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Order con : Trigger.new){ 
            if(con.SureBuyer__c !=Trigger.oldMap.get(con.Id).SureBuyer__c &&  Status == "Closed"){
               	setAccIds.add(con.SureBuyer__c );
                setAccIds.add(Trigger.oldMap.get(con.Id).SureBuyer__c );
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Order con : Trigger.old) { 
            if(con.Order__c!= null &&  Status == "Closed"){
            setAccIds.add(con.SureBuyer__c);
            	}
        	}
        }
    }    
    for(SureBuyer__c acc :[Select id, total__c,(Select id,Name,.total_order_amount__c from Orders AND Status = 'Closed') from SureBuyer__c where Id in : setAccIds AND recordtype.Name='Single Order' OR   recordtype.Name = Bulk Order]){
			integer val = 0;
        for(Order con : acc.Orders){
            
            val += integer.valueOf(con.total_order_amount__c);
            system.debug('====> ' +val);
        }
        system.debug(val);
        acc. total__c = string.valueOf(val);
        oList.add(acc);
        
    }
    if(oList.size()>0){
        update oList;     
    }
    
}
Please Mark It As Best Answer If It Helps
Thank You!