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
srashti jain 10srashti jain 10 

I need to create a trigger with the use of helper class..Please help me in this

there is a custom object 'Amount' with number value field 'Amnt' which is related to account by lookup. There is a field on account- 'Amt' it should show the sum of amount if the sum of amount is greater then 100 then it should not allow to insert any child record for a perticular account and should give an error- 'you have reached a maximum limit'
Best Answer chosen by srashti jain 10
Ajay K DubediAjay K Dubedi
Hi Srashti,
Try this trigger and it's handler class:

Trigger:
 
trigger AmountTrigger on Amount__c (before insert) {
    if(trigger.IsInsert && trigger.IsBefore) {
        AmountTrigger_handler.restrictInsertion(trigger.new);
    }
}

Handler Class:
 
public class AmountTrigger_handler {
    public static void restrictInsertion(List<Amount__c> amountList) {
        try 
        {
            Set<Id> accountSet = new Set<Id>();
            List<Account> acList = new List<Account>();
            decimal temp;
            for(Amount__c am : amountList) {
                if(am.Amnt__c != null && am.Account__c != null) {
                    accountSet.add(am.Account__c);
                }
            }
            
            if(accountSet.size() > 0) {
                Map<Id, Account> IdVsAccountMap = new Map<Id, Account>([Select Id, Amt__c FROM Account WHERE Id IN : accountSet]);
                if(IdVsAccountMap.size() > 0) {
                    for(Amount__c a : amountList) {
                        if(IdVsAccountMap.containsKey(a.Account__c)) {
                            if(IdVsAccountMap.get(a.Account__c).Amt__c != null) {
                                temp = IdVsAccountMap.get(a.Account__c).Amt__c + a.Amnt__c;
                            }
                            else {
                                IdVsAccountMap.get(a.Account__c).Amt__c = 0;
                                temp = IdVsAccountMap.get(a.Account__c).Amt__c + a.Amnt__c;
                            }
                            
                            if(temp > 100) {
                                a.addError('you have reached a maximum limit');
                            }
                            else
                            {
                                Account ac = new Account();
                                ac.Id = IdVsAccountMap.get(a.Account__c).Id;
                                ac.Amt__c = temp;
                                acList.add(ac);
                            }
                        }
                    }
                }
                update acList;
            }
        }
        catch(Exception ex)
        {
            System.debug('Exception on Line Number ' + ex.getLineNumber() + ' Message ---- ' + ex.getMessage());
        }
    }
}


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

All Answers

Sophia petersonSophia peterson
Thanks for sharing the details
https://resultwala.in/name-wise-result-of-brij-university-result-ba-bcom-b-sc-b-ed/
srashti jain 10srashti jain 10
Can anyone give me the solution of this trigger code. ASAP..??
Ajay K DubediAjay K Dubedi
Hi Srashti,
Try this trigger and it's handler class:

Trigger:
 
trigger AmountTrigger on Amount__c (before insert) {
    if(trigger.IsInsert && trigger.IsBefore) {
        AmountTrigger_handler.restrictInsertion(trigger.new);
    }
}

Handler Class:
 
public class AmountTrigger_handler {
    public static void restrictInsertion(List<Amount__c> amountList) {
        try 
        {
            Set<Id> accountSet = new Set<Id>();
            List<Account> acList = new List<Account>();
            decimal temp;
            for(Amount__c am : amountList) {
                if(am.Amnt__c != null && am.Account__c != null) {
                    accountSet.add(am.Account__c);
                }
            }
            
            if(accountSet.size() > 0) {
                Map<Id, Account> IdVsAccountMap = new Map<Id, Account>([Select Id, Amt__c FROM Account WHERE Id IN : accountSet]);
                if(IdVsAccountMap.size() > 0) {
                    for(Amount__c a : amountList) {
                        if(IdVsAccountMap.containsKey(a.Account__c)) {
                            if(IdVsAccountMap.get(a.Account__c).Amt__c != null) {
                                temp = IdVsAccountMap.get(a.Account__c).Amt__c + a.Amnt__c;
                            }
                            else {
                                IdVsAccountMap.get(a.Account__c).Amt__c = 0;
                                temp = IdVsAccountMap.get(a.Account__c).Amt__c + a.Amnt__c;
                            }
                            
                            if(temp > 100) {
                                a.addError('you have reached a maximum limit');
                            }
                            else
                            {
                                Account ac = new Account();
                                ac.Id = IdVsAccountMap.get(a.Account__c).Id;
                                ac.Amt__c = temp;
                                acList.add(ac);
                            }
                        }
                    }
                }
                update acList;
            }
        }
        catch(Exception ex)
        {
            System.debug('Exception on Line Number ' + ex.getLineNumber() + ' Message ---- ' + ex.getMessage());
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer