You need to sign in to do that
Don't have an account?

trigger to display Total number of amount and Invoices associated with account and Invoices object
Hi Team,
We have a requirement where two Objects Invoice and account . whenever an account is created it should update total number of invoices and amount on Sum of amount object on account .
Account and Invoice object are having lookup relationship
Account and Amount are the fileds on Invoices object
We have a requirement where two Objects Invoice and account . whenever an account is created it should update total number of invoices and amount on Sum of amount object on account .
Account and Invoice object are having lookup relationship
Account and Amount are the fileds on Invoices object
I was looking for a programtic approach and the objects are having a lookup relationship
Thanks,
Vijay.
trigger NumberOfChild on Contact (After insert,After Update,after delete) {
set<id> lst = new set <id>();
if(trigger.isInsert){
if(trigger.isAfter){
for(contact lstCon : trigger.new){
lst.add(lstcon.AccountId);
}
}
}
if(trigger.isUpdate){
if(trigger.isAfter){
for(contact lstCon : trigger.new){
lst.add(lstcon.AccountId);
}
}
}
if(trigger.isDelete){
if(trigger.isAfter){
for(contact lstCon : trigger.old){
lst.add(lstcon.AccountId);
}
}
}
List<Account> listAccs = [Select id,name,Total_Contacts__c ,(Select id from contacts) from Account where Id in :lst];
for(Account acc :listAccs)
{
acc.Total_Contacts__c = acc.contacts.size();
}
update listAccs;
}
whenever an account is created it should update total number ofinvoices and amount on Sum of amount object on account .
Account and Invoice object are having lookup relationship
Account and Amount are the fileds on Invoices object
trigger on invoices(after insert,after update,after delete){
if(trigger.isAfter && ((Trigger.isInsert || Trigger.isUpdate || Trigger.isDelete)
{
triggerHandler.updatesum(Trigger.newMap && Trigger.oldMap);
}
}
public class triggerHandler{
public static void updatesum(Map<Id>, <Invoice__c> invoices>{
AggregateResult[] results = [select total_number_of_invoices_c, sum(amount) FROM Invoice__c WHERE Id IN :invoices.keySet GROUP BY Account__c];
Set<Account> accountsToUpdate = new Set<Account>();
for(AggregateResult result:results){
accountsToUpdate.add(new Account(
Id = result.get('Account__c'),
Account_Balance__c = result.get('expr0');
results.total_number_of_invoices_c = result.Invoice.size();
);
}
update accountsToUpdate;
}
}