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
Yuvraj .Yuvraj . 

Need an Apex Trigger Solution

Annual Revenue to be equally shared (divided) between number of Contacts related to that Account and everytime the currency value change whenever any new contact added or removed from that particular Account . 
I had created one currency data type  " Share__c " in Contacts.
CharuDuttCharuDutt
Hii Yuvraj
Try Below Trigger
trigger AnnualRevenueSharedContacts on Contact (After Insert,After Update,After Delete) {
   List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,AnnualRevenue ,(Select id,name,Share__c from contacts) from Account where Id in : setAccIds]){
      Integer i = 0;
        i = (acc.AnnualRevenue/acc.contacts.size());
        for(Contact Con :acc.contacts){
         Con.Share__c  = i ;
        }
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark it As Best Answer If It Helps
Thank You!

 
Yuvraj .Yuvraj .
Hii @CharuDutt , thanks for your solution But when i insert or update the Annual Revenue field from Account , there no any changes happen in related contacts . That " Share__c " field is Blank in all three related contacts.
CharuDuttCharuDutt
Hii
Share__c Will Update Only When Contact Is Insert Update Or Delete
 
Maharajan CMaharajan C
But when i insert or update the Annual Revenue field from Account , there no any changes happen in related contacts ==> You have to write one more trigger in Account Object for this:
 
trigger AnnualRevenueSharedContactsAcc on Account (After Update) {
	List<Contact> contactList = new  List<Contact>();
	Set<Id> setAccIds = new Set<Id>();
	for(Account acc : trigger.New){
		if(acc.AnnualRevenue != Trigger.oldMap.get(acc.Id).AnnualRevenue && acc.AnnualRevenue != null){
			setAccIds.add(acc.Id);
		}
	}
	
	for(Account acc :[Select id,AnnualRevenue ,(Select id,name,Share__c from contacts) from Account where Id in : setAccIds]){
      integer i = 0;
        i = (acc.AnnualRevenue/acc.contacts.size());
        for(Contact Con :acc.contacts){
         Con.Share__c  = i ;
		 contactList.add(Con);
        }
    }
	
    if(contactList.size()>0){
        update contactList;     
    }	
}


===============================


Small change in contact trigger:
 
trigger AnnualRevenueSharedContacts on Contact (After Insert,After Update,After Delete) {
	List<Contact> contactList = new  List<Contact>();
    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,AnnualRevenue ,(Select id,name,Share__c from contacts) from Account where Id in : setAccIds]){
      integer i = 0;
        i = (acc.AnnualRevenue/acc.contacts.size());
        for(Contact Con :acc.contacts){
         Con.Share__c  = i ;
		 contactList.add(Con);
        }
    }
    if(contactList.size()>0){
        update contactList;     
    }
}


Thanks,
Maharajan.C
CharuDuttCharuDutt
Hii YuvRaj
Try Below Code
trigger AnnualRevenueSharedContactsAcc on Account (After insert,After Update) {
	List<Contact> contactList = new  List<Contact>();
	Set<Id> setAccIds = new Set<Id>();
if(Trigger.IsAfter && trigger.IsInsert){
	for(Account acc : trigger.New){	
			setAccIds.add(acc.Id);
	  }
	}

if(Trigger.IsAfter && trigger.IsInsert){ 
for(Account acc : trigger.New){ 
if(acc.AnnualRevenue != Trigger.oldMap.get(acc.Id).AnnualRevenue && acc.AnnualRevenue != null){
setAccIds.add(acc.Id); 
}
} 
}
	for(Account acc :[Select id,AnnualRevenue ,(Select id,name,Share__c from contacts) from Account where Id in : setAccIds]){
      integer i = 0;
        i = (acc.AnnualRevenue/acc.contacts.size());
        for(Contact Con :acc.contacts){
         Con.Share__c  = i ;
		 contactList.add(Con);
        }
    }
	
    if(contactList.size()>0){
        update contactList;     
    }	
}
Please Mark it As Best Answer If It Helps
Thank You!