You are on page 1of 3

trigger ContactAmountAccountTrigger on Contact (after insert, after update) {

set<ID> Setid=new set<ID>();


for(Contact C:trigger.new)
{
Setid.add(C.Accountid);
}
Map<id,Account> Maplist=new Map<id,Account>();
for(Account Acc:[select id,Total_Amount__c from Account where id=:Setid])
{
Maplist.put(Acc.Id, Acc);
}
list<Account> Acclist=new list<Account>();
for(Contact Con:trigger.new)
{
if(Maplist.containsKey(Con.Accountid))
{

Maplist.get(Con.Accountid).Total_Amount__c=Maplist.get(Con.Accountid).Total_Amount_
_c+Con.Amount__c;
}
Acclist.addAll(Maplist.values());
database.update(Acclist,false);
}
}

-----------------------------------------------------------------------------------
-------------------
Child to Parent:
Scenario: Add Opportunitys Amount in the Annual Revenue of the its related Account
Object-Opportunity
Event-After
Operation-insert and Update
STEPS:
1]Parent Id-Set
2]Map- ParantID,ParantObject(SOQL to retrieve the ParenetData for the above Set)
3]Search ParantID-In the Map and Update the ParentObject Accordingly.
------------------------
trigger OpportunityAmountAcc on Opportunity (after insert, after update) {
Set<Id> accIdSet=New Set<Id>();
for(Opportunity oppObj:trigger.new){
if(trigger.isInsert){
accIdSet.add(oppObj.AccountId);
}
if(trigger.isUpdate){
if(trigger.OldMap.get(oppObj.Id).Amount!=oppObj.Amount){
accIDSet.add(oppObj.AccountId);
}
}
}
Map<Id,Account>accMap=New Map<Id,Account>();
for(Account acc:[select id,AnnualRevenue from Account where id in:accIdSet]){
accMap.put(acc.id,acc);
}
List<Account> accUpdateList=new List<Account>();
for(Opportunity oppObj:trigger.new){
if(accMap.containsKey(oppObj.AccountId)){
Decimal oppAmount=oppObj.Amount;
Decimal accAmount=accMap.get(oppObj.AccountId).AnnualRevenue;
Decimal Total=oppAmount+accAmount;

Account objAcc=new Account();


objAcc.Id=oppObj.AccountId;
objAcc.AnnualRevenue=Total;
accUpdateList.add(objAcc);
system.debug('::OppAmount'+oppAmount);
system.debug('::AccountAmount'+accAmount);
}
}
if(!accUpdateList.isEmpty())
Database.update(accUpdateList,false);
}
----------------------------------------------------------
trigger OpportunityAmountAcc on Opportunity (after insert, after update) {
Set<Id> accIDSet=New Set<Id>();
for(Opportunity oppObj:trigger.New){
if(trigger.isInsert&&oppObj.Amount!=null&&oppObj.AccountId!=null){
accIDset.add(oppObj.AccountId);
}
if(trigger.isUpdate&&oppObj.Amount!=null&&oppObj.AccountId!=null) {
if(trigger.OldMap.get(oppObj.Id).Amount!=oppObj.Amount){
accIDset.add(oppObj.AccountId);
}
}
}
Map<Id,Account> accMap=new Map<Id,Account>();
for(Account acc:[select id,AnnualRevenue from Account where Id in:accIDSet])
{
accMap.put(acc.id, acc);
}
List<Account> accUpdateList=New List<Account>();
if(!accMap.isEmpty()){
for(Opportunity oppObj:trigger.new){
if(accMap.containsKey(oppObj.AccountId)){
Account objAcc=new Account();
objAcc.Id=oppObj.AccountId;
objAcc.AnnualRevenue=oppObj.Amount+
(accMap.get(oppObj.AccountId).AnnualRevenue);
accUpdateList.add(objAcc);
}
}
}
if(!accUpdateList.isEmpty())
Database.update(accUpdateList,false);
}
----------------------------------------------------------------------------------
trigger OpportunityAmountAcc on Opportunity (after insert, after update) {
Set<Id> accIDSet=new Set<Id>();
for(Opportunity opp:trigger.new){
if(trigger.isInsert&&opp.Amount!=null&&opp.AccountId!=null){
accIDset.add(opp.AccountId);
}
if(trigger.isUpdate&&opp.Amount!=null&&opp.AccountId!=null){
if(trigger.OldMap.get(opp.Id).Amount!=opp.Amount){
accIDSet.add(opp.AccountId);
}
}
}
Map<Id,Account> accMap=new Map<Id,Account>();
for(Account a:[select Id,AnnualRevenue from Account Where Id in:accIDSet]){
accMap.put(a.Id, a);
}
List<Account> accUpdateList=new List<Account>();
if(!accMap.isEmpty()){
for(Opportunity opp:trigger.new){

if(accMap.containsKey(opp.AccountId)&&accMap.get(opp.AccountId).AnnualRevenue!
=null){

accMap.get(opp.AccountId).AnnualRevenue=opp.Amount+accMap.get(opp.AccountId).Annual
Revenue;
}
}
}
if(!accUpdateList.isEmpty()){
Database.SaveResult[]srlist=Database.update(accUpdateList,false);
for(Database.SaveResultsr : srList){
if(sr.isSuccess()){
system.debug('Succesfully inserted Account.AccountID:'+sr.getId());
}
else{
for(Database.Error err:sr.getErrors()){
system.debug('The Following error has occoured');
system.debug(err.getStatusCode()+':'+err.getMessage());
system.debug('Account Fields that afftected this
error:'+err.getFields());
}
}
}

}
}
-----------------------------------------------------------------------------------

You might also like