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
Mohd NabeelMohd Nabeel 

Can someone please tell me how can i achieve this task. i am very confused in this. How can i do this

Create two field on Account: 1 - Amount X (Number), 2 - Amount Y (Number).
Create a two fields on Contact: 1 - Amount Type (Picklist: Amount X, Amount Y), 2 - Amount (Number)
You have to rollup amount in the correct field on account record and it depends on the Amount Type field on Contact.

I also need to bulkify this code.
Ajay K DubediAjay K Dubedi
Hi Nabeel,

I have gone through your requirements and created a trigger - 
trigger UpdateAccounts on Contact (after insert, after update) {
    if(Trigger.isAfter) {
        if(Trigger.isInsert || Trigger.isUpdate) {
            List<Contact> conList = new List<Contact>([Select Id, AccountId, Amount_Type__c, Amount__c From Contact Where Id In :Trigger.new Limit 50000]);
            List<Account> accList = new List<Account>([Select Id, AmountX__c, AmountY__c From Account Limit 50000]);
            List<Account> accListToUpdate = new List<Account>();
            for(Account acc: accList) {
                Integer amtX = 0;
                Integer amtY = 0;
                for(Contact con: conList) {
                    if(con.AccountId == acc.Id) {
                        if(con.Amount__c != null) {
                            if(con.Amount_Type__c == 'Amount X') {
                                amtX = amtX + con.Amount__c;
                            } else if(con.Amount_Type__c == 'Amount Y') {
                                amtY = amtY + con.Amount__c;
                            }
                        }
                    }
                }
                if(amtX != null) {
                   acc.AmountX__c += amtX; 
                }
                if(amtY != null) {
                    acc.AmountY__c += amtY;
                }
                accListToUpdate.add(acc);
            }
            if(accListToUpdate.size()>0) {
                update accListToUpdate;
            }
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Mohd NabeelMohd Nabeel
Hello SIr,
I was trying the above code to do with the handler class but it is giving me an Exception 'Attempt to de-reference a null object'on line 27 and 30.
public class ContactAccountRollUpAmount {
    public static void onAfterInsertOrUpdate(List<Contact> cont){
        contAccRollUpAmount(cont);
    }
    public static void contAccRollUpAmount(List<Contact> contactList){
        List<Contact> cont = new List<Contact>([Select AccountId, Amount_Type__c, Amount__c
                                                from Contact 
                                                where id in: contactList]);
        List<Account> accList = new List<Account>([Select id, AmountX__c, AmountY__c 
                                               from Account Limit 50000]);
        List<Account> accListToUpdate = new List<Account>();
        for(Account acct: accList){
            Double amtX = 0;
            Double amtY = 0;
            for(Contact con: cont){
                if(con.AccountId == acct.Id){
                    if(con.Amount__c != NULL){
                        if(con.Amount_Type__c == 'AmountX'){
                            amtX = amtX + con.Amount__c;
                        } else if(con.Amount_Type__c == 'AmountY'){
                            amtY = amtY + con.Amount__c;
                        }
                    }
                }
            }
            if(amtX != NULL){
                acct.AmountX__c += amtX;          
            }
            if(amtY != NULL){
                acct.AmountY__c += amtY;
            }
            accListToUpdate.add(acct);
        }
        if(accListToUpdate.size()>0){
            update accListToUpdate;    
        }
    }
}
Ajay K DubediAjay K Dubedi
Hi Nabeel,

You have three number fields, AmountX, and AmountY on Account and Amount on Contact.
Please set the default value of these fields to zero.

Like this - 

User-added image



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Mohd NabeelMohd Nabeel
Still getting it..
Ajay K DubediAjay K Dubedi
Hi Nabeel,

Add these two lines in your code just below line 12 which is "for(Account acct: accList){"

acct.AmountX__c = 0;
acct.AmountY__c = 0;


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Mohd NabeelMohd Nabeel
But Sir there is a catch it will work fine in this condition but it will never add the amount which is the AmountX and AmountY field because it is making zero again and again. I mean we have to roll up the amount if there is any amount in those fields so it should add. But from this logic first this will make it zero then it will proceed further.
Ajay K DubediAjay K Dubedi
Hi Nabeel,


Please modify the lines I gave you with this code - 
 
if(acct.AmountX__c == Null) {
    acct.AmountX__c = 0;
}
if(acct.AmountY__c == Null) {
    acct.AmountY__c = 0;
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Mohd NabeelMohd Nabeel
Hi Sir,
I have resolved my error, actaully i have realised that instead of comparing a Number with zero i was comparing it with the Null value may be thats why it was giving me that error. So, i have just changed my code to "if(amtX != NULL)" to if(amtX != 0) without adding those lines after the for loop which you have suggested.
and it is working fine.
Thanks for ur help Sir.