0
public with sharing class TRAEstimateValidation {

    public static List<CloudbyzITPM__Task_Resource_Assignment__c>   newTRAList = new List<CloudbyzITPM__Task_Resource_Assignment__c >();
    //public static List<CloudbyzITPM__Task_Resource_Assignment__c>   oldTRAList = new List<CloudbyzITPM__Task_Resource_Assignment__c >();

    public static void beforeInsertOperation(List<CloudbyzITPM__Task_Resource_Assignment__c>   newTRAList) {

        Set<Id> taskIdSet = new Set<Id>();

        for(CloudbyzITPM__Task_Resource_Assignment__c traRec : newTRAList) {
            taskIdSet.add(traRec.CloudbyzITPM__Task__c);
        }

        if(taskIdSet.size() > 0) {
            //system.debug('In Task:'+taskIdSet);
            Decimal totalEstimate = 0; // Total Estimate
            List<CloudbyzITPM__Task__c> taskList = [select Id,CloudbyzITPM__Estimate__c, (select  Allocated_Hours__c from CloudbyzITPM__Task_Resource_Assignments__r ) 
                                                    from CloudbyzITPM__Task__c where Id IN: taskIdSet];
            for(CloudbyzITPM__Task__c taskRec : taskList) {
                //System.debug('task rec'+taskRec);
                for(CloudbyzITPM__Task_Resource_Assignment__c  traRec : taskRec.CloudbyzITPM__Task_Resource_Assignments__r) {
                    totalEstimate += ((traRec.Allocated_Hours__c != null) ? traRec.Allocated_Hours__c : 0);
                    //System.debug('Total Estimate:-' + totalEstimate);
                    if(totalEstimate > taskRec.CloudbyzITPM__Estimate__c){
                        System.debug('Task estimate' + taskRec.CloudbyzITPM__Estimate__c + ' ' + 'Total Estimate :-'+ totalEstimate );
                        traRec.addError('Total Task Resource Assignments Allocation hours is greater than Task Estimate');
                        System.debug('Before insert or update done');

                    }
                }
            }
        }
    }
}

Trigger

trigger TRAEstimateValidation on CloudbyzITPM__Task_Resource_Assignment__c (before insert,before update) {
    CloudbyzITPM__Task_Resource_Assignment__c tra = new CloudbyzITPM__Task_Resource_Assignment__c ();
    if(Trigger.isBefore && (Trigger.isInsert)){
        TRAEstimateValidation.beforeInsertOperation(Trigger.new);
    }
    if(trigger.isUpdate && trigger.isBefore) {
        if(CheckRecursionforPM.runOnce()){
            TRAEstimateValidation.beforeInsertOperation(Trigger.old);
        }
    } 
}
  • 3
    If you're getting an error message, it generally helps to include the entire text of the error verbatim. You can add this information by making an edit to your question. Also, as a note to others, line numbers in the error message may be off because of extraneous spaces that were edited out. – Derek F Jun 25 '18 at 14:41
  • 2
    Hi @sfdcfox, I believe this is not a duplicate. The duplicate one is doing adderror on trigger variable, but here the OP is using add error on SOQL retrieved records. It indeed is low quality, but not a duplicate. – Pranay Jaiswal Jun 25 '18 at 14:57
  • @PranayJaiswal If you feel it's significantly different, feel free to vote to reopen. – sfdcfox Jun 25 '18 at 15:42
5

Lets dig into this.

You have written a trigger on CloudbyzITPM__Task_Resource_Assignment__c and you are adding addError to the records of the same list, then why is it giving you the above-mentioned error?

Because its a different instance of CloudbyzITPM__Task_Resource_Assignment__c . You are using addError on record which was retrieved via SOQL and not provided to you via Trigger.new context variable.

We can quickly solve this by creating a Map<Id,List<CloudbyzITPM__Task_Resource_Assignment__c>> . This will help us finding the record from trigger context where we need to use add error.

    public with sharing class TRAEstimateValidation {

        public static List<CloudbyzITPM__Task_Resource_Assignment__c>   newTRAList = new List<CloudbyzITPM__Task_Resource_Assignment__c >();
        //public static List<CloudbyzITPM__Task_Resource_Assignment__c>   oldTRAList = new List<CloudbyzITPM__Task_Resource_Assignment__c >();

        public static void beforeInsertOperation(List<CloudbyzITPM__Task_Resource_Assignment__c>   newTRAList) {

            Set<Id> taskIdSet = new Set<Id>();
            Map<Id,List<CloudbyzITPM__Task_Resource_Assignment__c>> taskIdVsResouceAssignmentMap =new Map<Id,List<CloudbyzITPM__Task_Resource_Assignment__c>>();


            for(CloudbyzITPM__Task_Resource_Assignment__c traRec : newTRAList) {
                taskIdSet.add(traRec.CloudbyzITPM__Task__c);
                if(taskIdVsResouceAssignmentMap.containsKey(traRec.CloudbyzITPM__Task__c)){
                    taskIdVsResouceAssignmentMap.get(traRec.CloudbyzITPM__Task__c).put(traRec);
                }else{
                    taskIdVsResouceAssignmentMap.put(traRec.CloudbyzITPM__Task__c,new List<CloudbyzITPM__Task_Resource_Assignment__c>{traRec});
                }
            }

            if(taskIdSet.size() > 0) {
                //system.debug('In Task:'+taskIdSet);
                Decimal totalEstimate = 0; // Total Estimate
                List<CloudbyzITPM__Task__c> taskList = [select Id,CloudbyzITPM__Estimate__c, (select  Allocated_Hours__c from CloudbyzITPM__Task_Resource_Assignments__r ) 
                                                        from CloudbyzITPM__Task__c where Id IN: taskIdSet];
                for(CloudbyzITPM__Task__c taskRec : taskList) {
                    //System.debug('task rec'+taskRec);
                    for(CloudbyzITPM__Task_Resource_Assignment__c  traRec : taskRec.CloudbyzITPM__Task_Resource_Assignments__r) {
                        totalEstimate += ((traRec.Allocated_Hours__c != null) ? traRec.Allocated_Hours__c : 0);
                        //System.debug('Total Estimate:-' + totalEstimate);
                        if(totalEstimate > taskRec.CloudbyzITPM__Estimate__c){
                            System.debug('Task estimate' + taskRec.CloudbyzITPM__Estimate__c + ' ' + 'Total Estimate :-'+ totalEstimate );

                            CloudbyzITPM__Task_Resource_Assignment__c  triggerVariable= taskIdVsResouceAssignmentMap.get(taskRec.Id)[0];
                            triggerVariable.addError('Total Task Resource Assignments Allocation hours is greater than Task Estimate');
                            System.debug('Before insert or update done');

                        }
                    }
                }
            }
        }
    }
|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.