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
John L.ax1429John L.ax1429 

Help with Developing code for simple roll up summary fields.

I need to roll up from the contract related list on accounts the two fields Mth_Support_Hours__c and Yearly_Support_Hours__c based on the active status of the contract  Status = Activated ,  The account fields that they need to roll up to are Current_Total_Contract_Support_Hours__c  and Current_Monthly_Support_Contract_Hours__c .  If you can help me with this I would appriciate this as I do not know how to develop this code.

Ankit AroraAnkit Arora

Generally native roll-up fields does it all, but in your case both objects are native object and we can not create a mast detail relationship in them. So you need to write a custom logic for it. Either use trigger which will calculate the totals and update the account everytime contract is created or updated OR write a batch class which do the same.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

John L.ax1429John L.ax1429

I kind of figured that would be the case just I do not know how to write either one, is this something that you can help me if it is an easy code development?

Laxman RaoLaxman Rao

Hi John,

 

I have written a trriger, which will roll up the number of contacts on account.

Done only for insert, you can do same for update, delete etc.

 

try this

 

trigger CountOfChild on Contact (after insert)
{
List<Contact> VLstContact = trigger.new;
set<String> vAccId = new set<String>();
for(Contact vContact : VLstContact)
{
if(vContact.AccountId != null)
{
vAccId.add(vContact.AccountId);
}
}
List<AggregateResult> vLstAggr = [SELECT count(Id) NoOfContacts,AccountId
FROM Contact
WHERE AccountId In :vAccId
GROUP BY AccountId];
Account vAcccount;
list<Account> vLstAccounts = new list<Account>();
for(AggregateResult vAggr : vLstAggr)
{
string accId = (string) vAggr.get('AccountId');
decimal countOfChild = (decimal) vAggr.get('NoOfContacts');

vAcccount = new Account(Id = accId , Count_Of_Contact__c = countOfChild);
vLstAccounts.add(vAcccount);
}

update vLstAccounts;
}

Laxman RaoLaxman Rao

I have done on contact, you can implement on contract object

John L.ax1429John L.ax1429

Laxman Rao wrote:

I have done on contact, you can implement on contract object



Laxman Rao wrote:

I have done on contact, you can implement on contract object




I need to roll up from the contract related list on accounts the two fields

 

Mth_Support_Hours__c  and Yearly_Support_Hours__c

 

based on the active status of the contract  Status = Activated

 

The account fields that they need to roll up to are

 

Current_Total_Contract_Support_Hours__c    and    Current_Monthly_Support_Contract_Hours__c

 

 

I am looking to use these two fields to do calculations on the account record. 

Laxman RaoLaxman Rao

you can try this:

 

List<AggregateResult> vLstAggr = [SELECT sum(Mth_Support_Hours__c),sum(Yearly_Support_Hours__c),AccountId
FROM Contract
WHERE AccountId = 'Your account Id' AND Status = 'Activated'
GROUP BY AccountId];