• Nitin sharma 424
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
A (parent Acount)          B,C,D E (Multiple child Accounts)                                                                                                                  All of the above given  child Accounts have multiple Opps assocciated with them                  

There are multiple child accounts and they all have multiple Opportunitiies.Roll summary has worked and the I am able to  reflect that amount on the child accounts as well as on the parent account  .Howver,I want the parent account field to keep adding amount  from the roll up fields  which is on on Multiple Accounts..It is replacing value which is on the  parent account when an account is updated and it it different from the previous Account .


I tried with Parent to Child query but could not make it.Can somebody please advise?

trigger UpdateParentAccounFromChild on Account (After Update)
{
   set<id>AccountIds=new set<id>();   
   Map<Id,Account>MapOfParentAccountIdToAccRecord=new map<id,account>();
   if(trigger.isupdate && trigger.isAfter)
    {
        for(Account AccountRecord:trigger.new)
        {
            if(AccountRecord.parentId!=null) //&& AccChildRecord.Total_of_All_Opps__c!=trigger.oldmap.get(AccChildRecord.id).Total_of_All_Opps__c)
                {
                    
                    AccountIds.add(AccountRecord.id);
                     MapOfParentAccountIdToAccRecord.put(AccountRecord.parentId,AccountRecord);   
            
                }
     }        
           }       
    
Account parentRecord=new account();   
list<Account>Acclist=new list<account>();
list<Account>listOfAccReords=new list<account>([Select id,Total_of_All_Opps__c,ParentId from account where id in:AccountIds]);
Map<id,Account>MapOfParentIdToAccount=new map<id,Account>([select id,Sum_of_All_opps_of_Child_Acc__c from account where id in:MapOfParentAccountIdToAccRecord.keyset()]);     
List<account>UpdateParentAccRec=new list<account>();
    Account accrec=new account();
    for(Account Acc:listOfAccReords)
    {
        if(acc.Total_of_All_Opps__c!=null && acc.Total_of_All_Opps__c!=trigger.oldmap.get(acc.id).Total_of_All_Opps__c)
        {
            if(MapOfParentIdToAccount.containsKey(acc.ParentId))
            {
                Account ParentAccount=MapOfParentIdToAccount.get(acc.ParentId);
                accrec.id=ParentAccount.id;
                if(accrec.Sum_of_All_opps_of_Child_Acc__c==null)
                {
                   accrec.Sum_of_All_opps_of_Child_Acc__c=0;
                }
                accrec.Sum_of_All_opps_of_Child_Acc__c=accrec.Sum_of_All_opps_of_Child_Acc__c+acc.Total_of_All_Opps__c;
                   UpdateParentAccRec.add(accrec);                             
            }
               
            
        }
    
        
    }
   
Update UpdateParentAccRec;
}   
    






 
Hi All,I have used aggregate furnctions for the Amount field on the opportunity object and udpated the Related Account obect with the results.
However,Salesforce documentation says

Aggregate function results on currency fields default to the system currency.

I have created currency fields on the Account object and I thought that I will be able to assign values from currency amount field to the currency fields on the Account object.But it gave me an error that object fields cannot be convrted to Decimal fields.I am not sure how it became a decimal field when I have created it as a currency field..

But when I typecased to amount field to decimal in the below given code then it worked fine.

Am I  missing somthing here.?
Need to understand,Why system made me typecast Opportunity Amount  field to Decimal type to save value in the currency field on the Account Object.

Can somebody please explain.?


Trigger UpdatingAccountWithOppInformation on Opportunity (After insert,After Update)
{
List<Account>Acc=new list<account>();
Map<ID,Opportunity>OppRecord=new Map<Id,Opportunity>();
Set<Id>SetOfIdsOfAccountRecords=new set<id>();    
For(Opportunity Opp:trigger.new)
{
If(Opp.AccountId!=null)
{
 
    
//OppRecord.put(opp.accountid,opp);
SetOfIdsOfAccountRecords.add(Opp.accountid);
    
}
}
    
AggregateResult [] Result=[Select AccountId Id,Avg(Amount) Amt, Count(ID) countOfOpp,MIN(Amount) MinAmount,Max(Amount) MaxAmount,Sum(Amount) TotalOfAppOpps from opportunity where AccountId in:SetOfIdsOfAccountRecords Group By AccountId];  
for(AggregateResult rope:result)
{

Acc.add(New Account(Id=(Id)rope.get('Id'),Average_Opportunity_Amount__c=(Decimal)rope.get('Amt'),Count_Of_Opportunities__c=(Decimal)rope.get('countOfOpp'),Opportunity_with_Min_Amount__c=(Decimal)rope.get('MinAmount'),Opp_with_Max_Amount__c=(Decimal)rope.get('MaxAmount'),Sum_Of_All_Opportunities__c=(Decimal)rope.get('TotalOfAppOpps')));

}
Update Acc;
}

    


 
A (parent Acount)          B,C,D E (Multiple child Accounts)                                                                                                                  All of the above given  child Accounts have multiple Opps assocciated with them                  

There are multiple child accounts and they all have multiple Opportunitiies.Roll summary has worked and the I am able to  reflect that amount on the child accounts as well as on the parent account  .Howver,I want the parent account field to keep adding amount  from the roll up fields  which is on on Multiple Accounts..It is replacing value which is on the  parent account when an account is updated and it it different from the previous Account .


I tried with Parent to Child query but could not make it.Can somebody please advise?

trigger UpdateParentAccounFromChild on Account (After Update)
{
   set<id>AccountIds=new set<id>();   
   Map<Id,Account>MapOfParentAccountIdToAccRecord=new map<id,account>();
   if(trigger.isupdate && trigger.isAfter)
    {
        for(Account AccountRecord:trigger.new)
        {
            if(AccountRecord.parentId!=null) //&& AccChildRecord.Total_of_All_Opps__c!=trigger.oldmap.get(AccChildRecord.id).Total_of_All_Opps__c)
                {
                    
                    AccountIds.add(AccountRecord.id);
                     MapOfParentAccountIdToAccRecord.put(AccountRecord.parentId,AccountRecord);   
            
                }
     }        
           }       
    
Account parentRecord=new account();   
list<Account>Acclist=new list<account>();
list<Account>listOfAccReords=new list<account>([Select id,Total_of_All_Opps__c,ParentId from account where id in:AccountIds]);
Map<id,Account>MapOfParentIdToAccount=new map<id,Account>([select id,Sum_of_All_opps_of_Child_Acc__c from account where id in:MapOfParentAccountIdToAccRecord.keyset()]);     
List<account>UpdateParentAccRec=new list<account>();
    Account accrec=new account();
    for(Account Acc:listOfAccReords)
    {
        if(acc.Total_of_All_Opps__c!=null && acc.Total_of_All_Opps__c!=trigger.oldmap.get(acc.id).Total_of_All_Opps__c)
        {
            if(MapOfParentIdToAccount.containsKey(acc.ParentId))
            {
                Account ParentAccount=MapOfParentIdToAccount.get(acc.ParentId);
                accrec.id=ParentAccount.id;
                if(accrec.Sum_of_All_opps_of_Child_Acc__c==null)
                {
                   accrec.Sum_of_All_opps_of_Child_Acc__c=0;
                }
                accrec.Sum_of_All_opps_of_Child_Acc__c=accrec.Sum_of_All_opps_of_Child_Acc__c+acc.Total_of_All_Opps__c;
                   UpdateParentAccRec.add(accrec);                             
            }
               
            
        }
    
        
    }
   
Update UpdateParentAccRec;
}   
    






 
Hi All,I have used aggregate furnctions for the Amount field on the opportunity object and udpated the Related Account obect with the results.
However,Salesforce documentation says

Aggregate function results on currency fields default to the system currency.

I have created currency fields on the Account object and I thought that I will be able to assign values from currency amount field to the currency fields on the Account object.But it gave me an error that object fields cannot be convrted to Decimal fields.I am not sure how it became a decimal field when I have created it as a currency field..

But when I typecased to amount field to decimal in the below given code then it worked fine.

Am I  missing somthing here.?
Need to understand,Why system made me typecast Opportunity Amount  field to Decimal type to save value in the currency field on the Account Object.

Can somebody please explain.?


Trigger UpdatingAccountWithOppInformation on Opportunity (After insert,After Update)
{
List<Account>Acc=new list<account>();
Map<ID,Opportunity>OppRecord=new Map<Id,Opportunity>();
Set<Id>SetOfIdsOfAccountRecords=new set<id>();    
For(Opportunity Opp:trigger.new)
{
If(Opp.AccountId!=null)
{
 
    
//OppRecord.put(opp.accountid,opp);
SetOfIdsOfAccountRecords.add(Opp.accountid);
    
}
}
    
AggregateResult [] Result=[Select AccountId Id,Avg(Amount) Amt, Count(ID) countOfOpp,MIN(Amount) MinAmount,Max(Amount) MaxAmount,Sum(Amount) TotalOfAppOpps from opportunity where AccountId in:SetOfIdsOfAccountRecords Group By AccountId];  
for(AggregateResult rope:result)
{

Acc.add(New Account(Id=(Id)rope.get('Id'),Average_Opportunity_Amount__c=(Decimal)rope.get('Amt'),Count_Of_Opportunities__c=(Decimal)rope.get('countOfOpp'),Opportunity_with_Min_Amount__c=(Decimal)rope.get('MinAmount'),Opp_with_Max_Amount__c=(Decimal)rope.get('MaxAmount'),Sum_Of_All_Opportunities__c=(Decimal)rope.get('TotalOfAppOpps')));

}
Update Acc;
}