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

Apex trigger - faulty adjustment
Hi there,
For the last days I have been struggeling with Apex Triggers, I need to roll up a currency field from the (standard) 'Account' object to the custom 'CustomerGroups__c' object. I'd prefer avoiding Apex, but Sales Force does not allow me to make a parent for the Account object.
From the child 'Account', I'd like to roll up the currency field "Customer_OLB__c" and summarize those amounts in the parents object 'CustomerGroups__c' field 'Customer_Group_OLB__c'.
Adjusting one of the previously provided examples has lead me to to following code, where I get an problem in line 2: "unexpected token: ':' "
Kind regards,
Thijs
For the last days I have been struggeling with Apex Triggers, I need to roll up a currency field from the (standard) 'Account' object to the custom 'CustomerGroups__c' object. I'd prefer avoiding Apex, but Sales Force does not allow me to make a parent for the Account object.
From the child 'Account', I'd like to roll up the currency field "Customer_OLB__c" and summarize those amounts in the parents object 'CustomerGroups__c' field 'Customer_Group_OLB__c'.
Adjusting one of the previously provided examples has lead me to to following code, where I get an problem in line 2: "unexpected token: ':' "
trigger UpdateAccount on Account (after insert, after update, after delete, after undelete) { Map<Id,CustomerGroups__c> updateCustomerGroups = new Map<Id,CustomerGroups__c>; Set<Id> updateCustomerGroupsIds = new Map<Id,CustomerGroups__c>(); if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete) for (Account account:Trigger.new) updateCustomerGroupsIds.add(account.CustomerGroups__c_Name); if(Trigger.isUpdate || Trigger.isDelete) for (Account account:Trigger.old) updateCustomerGroupsIds.add(account.CustomerGroups__c_Name); updateCustomerGroupsIds.remove(null); for(Id CustomerGroupsId:updateCustomerGroupsIds) updateCustomerGroupsIds.put (CustomerGroupsId,new CustomerGroups (id=CustomerGroupsId,Accounts_Count=0); for Account account:[select id,CustomerGroups__c_Name from account where CustomerGroups__c_Name in :updateCustomerGroupsIds]) updateCustomerGroups.get (account.CustomerGroups__c_Name).Accounts_Count++; Database.update(updateCustomerGroups.values()); ) }Any help would be much appreciated!
Kind regards,
Thijs
Use the below simple trigger to rollup the Lookups.This type of code works fine for me in Live Environment.
Here i used the CustomerGroupsName__c field as lookfield API name in Account but you will need to replace with your correct field API in Account.
trigger TotalAmountTrigger on Account (after insert, after update, after delete, after undelete)
{
Set<Id> accountIds = new Set<Id>();
for(Account c: Trigger.isDelete? Trigger.old : Trigger.new){
if(c.CustomerGroupsName__c != null)
{
accountIds.add(c.CustomerGroupsName__c);
}
}
List<CustomerGroups__c> accList = new List<CustomerGroups__c>();
for(AggregateResult currentAggResult:[SELECT CustomerGroupsName__c accId, SUM(Customer_OLB__c) sumAmt FROM Account WHERE CustomerGroupsName__c in:accountIds GROUP BY CustomerGroupsName__c])
{
CustomerGroups__c acc = new CustomerGroups__c();
acc.Id = (Id)currentAggResult.get('accId');
acc.Customer_Group_OLB__c = (Decimal)currentAggResult.get('sumAmt');
accList.add(acc);
accountIds.remove((Id)currentAggResult.get('accId'));
}
for(Id currAccId : accountIds)
{
CustomerGroups__c acc = new CustomerGroups__c();
acc.Id = currAccId;
acc.Customer_Group_OLB__c = null;
accList.add(acc);
}
update accList;
}
Also refer the base code from this Link: http://salesforcekings.blogspot.in/2015/07/rollup-summar-trigger-for-lookup.html
Can you please Let me know if it works or not and also If you face any problems!!!
If it works don't forget to mark this as a best answer!!!
Thanks,
Raj
All Answers
Replace your line 2 with this.
Map<Id,CustomerGroups__c> updateCustomerGroups = new Map<Id,CustomerGroups__c>();
You have missed "()" at the end while instantiating the map.
Best Regards,
Vijay
Thank you for your quick help, that's amazing. You were correct :) After your recommended correction, a few other lines were updates. Now, I got stuck with a new error in line 3: "Illegal assignment from Map<Id,CustomerGroups__c> to Set<Id>". Any thoughts?
This is the amended code:
Thanks again!
Use the below simple trigger to rollup the Lookups.This type of code works fine for me in Live Environment.
Here i used the CustomerGroupsName__c field as lookfield API name in Account but you will need to replace with your correct field API in Account.
trigger TotalAmountTrigger on Account (after insert, after update, after delete, after undelete)
{
Set<Id> accountIds = new Set<Id>();
for(Account c: Trigger.isDelete? Trigger.old : Trigger.new){
if(c.CustomerGroupsName__c != null)
{
accountIds.add(c.CustomerGroupsName__c);
}
}
List<CustomerGroups__c> accList = new List<CustomerGroups__c>();
for(AggregateResult currentAggResult:[SELECT CustomerGroupsName__c accId, SUM(Customer_OLB__c) sumAmt FROM Account WHERE CustomerGroupsName__c in:accountIds GROUP BY CustomerGroupsName__c])
{
CustomerGroups__c acc = new CustomerGroups__c();
acc.Id = (Id)currentAggResult.get('accId');
acc.Customer_Group_OLB__c = (Decimal)currentAggResult.get('sumAmt');
accList.add(acc);
accountIds.remove((Id)currentAggResult.get('accId'));
}
for(Id currAccId : accountIds)
{
CustomerGroups__c acc = new CustomerGroups__c();
acc.Id = currAccId;
acc.Customer_Group_OLB__c = null;
accList.add(acc);
}
update accList;
}
Also refer the base code from this Link: http://salesforcekings.blogspot.in/2015/07/rollup-summar-trigger-for-lookup.html
Can you please Let me know if it works or not and also If you face any problems!!!
If it works don't forget to mark this as a best answer!!!
Thanks,
Raj
After adjusting the Customer group name API in Accounts ('GroupName__c'). After that I still get the error: "Illegal assignment from Map<Id,CustomerGroups__c> to Set<Id>" on line 3.
- The 'GroupName__c' field in the child object Account has a lookup relationship with the 'Name' field in the "parent" 'CustomerGroups__c'.
What do I miss?
Thanks a lot!
Thijs
Did you use the syntax like below in the code then you got the error like what you have now.
Set<Id> updateCustomerGroupsIds = new Map<Id,CustomerGroups__c>();
So avoid this line if you used in the code.use below like:
Set<Id> updateCustomerGroupsIds = new Set<Id,CustomerGroups__c>();
Delete the unwanted Rollup triggers then try create the new one with the reference code what i give
Can you please Let me know if it works or not and also If you face any problems!!!
If it works don't forget to mark this as a best answer!!!
Thanks,
Raj
Just deleted all other triggers. And I used the code that you provided & adjusted for the group name as it is in the Account API. (see code below)
Still got the same problem. Do I need to rename "accountsIds" set name?
When i google the error there is 0 Result completly Disappointed!!!
Could it be that the lookup relationship between name (from Account object; datatype = name) and GroupName__c (from Customergroups__C object; datatype = text(80)) ?
Thank you very much Raj for brneging me a step closer!
In the Trigger we used field GroupName__c which is a lookup field data type in Account to object 'CustomerGroups__c' ?
Because the code only works the field like above.
------
And also Try the below chenges in set:
Change the Line Set<Id> accountIds = new Set<Id>(); as Set<account> accountIds=new Set<Account>();
------
If it is a Text then we have to use like set<string> groupname = new set<string>();
Thanks,
Raj
A new small problem arises, the rolled up filed is a currency (ZAR) and now the summation is in ZAR, but divided by the currency exchange rate that I entered. My un-elegant sulotion is creating a new formula field and adjust accordingly. If you have a better suggestion, let me know. either way, you were great!
(the error I was so keen on, was for an other trigger that appearanly was still on) <-- I know, newbie :)