+ Start a Discussion
sonam gupthasonam guptha 

rollup trigger to update on account based on different recordtypes

Can any one help me to reslove it.
I have a custom table(Member__c) it has 2 record types(rc1,rc2) and on member__c i have a column total_column__c,And on account, we created 2 custom columns(rc1,rc2),so in this columns now i wanted to print out that particular values,here member__c is lookup to account and opportunity,and member__c is a junction object,i can only select opportunity on member__c not account as per my flow.account is tagged on opportunity,so now i have to consider account from opportunity.and print those values on opportunities related account.

And i have to rollup at the same time as below :—

for example if my member__c have the different values with same recordtype,i have to count those values and place it on account.ITWING__c.thats nothing but rollup or summingup,that should work on all our events like insert,delete,update : -- please check below for more info.

member__c.ITWING(recordtype).total_column__c = 100
member__c.ITWING(recordtype).total_column__c = 200

Result woud be like:--

on account we have to sum up those values with same record type.

aaa.ITWING__c = 300
if we delete 100
aaa.ITWING__c = 200

Here is my current code please have a look :--
 
trigger updatetotalAccount on Member__c (After Insert,After update,After Delete)
{
Id recTypeIT = [SELECT Id FROM RecordType WHERE name='ITWING' AND SObjectType='member__c' LIMIT 1].Id;
Id recTypeFinance = [SELECT Id FROM RecordType WHERE name='Finance' AND SObjectType=‘member__c' LIMIT 1].Id;
string ids;
if(Trigger.isInsert || Trigger.isUpdate)
{
for(Member__c objMember : Trigger.new){
if(objMember.lookupAccount__c != null)
ids= objMember.id;
    }
}
if(Trigger.isDelete){
for(Member__c objMember : Trigger.old){
if(objMember.lookupAccount__c != null)
ids= objMember.id;
    }}
AggregateResult mem=[select Id,lookupAccount__c,RecordTypeId,SUM(total_column__c)sumtc from Member__c where ID=:ids GROUP BY RecordTypeId];
If(mem.RecordTypeId=recTypeIT)
{
opportunity opp=[Select Id,AccountId from Opportunity where Id=:mem.lookupOpportunity__c ];
Account acc=[Select Id,ITWING__c,Finance__c from Account where Id=:opp.AccountId];
for(Account aaa:acc){
aaa.ITWING__c=(Decimal)mem.get('sumtc');
update aaa;
}
}
elseIf(mem.RecordTypeId=recTypeFinance)
{
opportunity opp1=[Select Id,AccountId from Opportunity where Id=:mem.lookupOpportunity__c ];
Account acc1=[Select Id,ITWING__c,Finance__c from Account where Id=:opp1.AccountId];
for(Account bbb:acc1){
bbb.ITWING__c=(Decimal)mem.get('sumtc');
update bbb;
}
}