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 code I have to get the sum of "field : total_order_amount" in  OBJECT NAME: Order but only who choose the "Record Type Single Order" on the second object "OBJECT NAME: SureBuyer__c".

SwethaSwetha (Salesforce Developers) 
HI,
Can you add more details to your question to provide any attempts/code you've started, any errors you're getting, or where exactly you're struggling in achieving this so that the developer community can suggest better?
Thanks
CharuDuttCharuDutt
Hii Md0000
Try Below Code
trigger SumAmountBuyer on SureBuyer__c(After Insert,After Update,After Delete) {
 List<Order> oList=new List<Order>();
Id recordTypeId = Schema.SObjectType.Order.getRecordTypeInfosByName().get('Record Type Single Order').getRecordTypeId();

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

 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

You can take references from the below code

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,SureBuyer__c 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.SureBuyer__c=sum;
           
       }
       
       if(accountList.size()>0){
           update accountList;
       }

   }
   }