Reply
Regular Contributor
MeanGeeks
Posts: 92
0
Accepted Solution

Problem with raising error in correct format. Please help???


trigger CaseCloseNoOpen on Case (Before update) { List<Id> caseIds = new List<Id>(); Id caseRecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'PM Case' AND SObjectType = 'Case'].Id; Id TaskRecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'PM Task' AND SObjectType = 'Task'].Id; for(Case c : Trigger.New) { if(c.RecordTypeId == caseRecordTypeId && (c.Status == 'Completed' || c.Status == 'Closed')) { caseIds.add(c.Id); } } for(Training_Information__c customobj : [Select Id, Status__c,Case__c From Training_Information__c Where Case__c in: caseIds]) { if(customobj.Status__c == 'Pending' || customobj.Status__c == 'Scheduled') { Trigger.newMap.get(customobj.Case__c).addError('Training has not been completed.'); } } for(Development_Request__c customobj : [Select Id, Stage__c,Case__c From Development_Request__c Where Case__c in: caseIds]) { if(customobj.Stage__c != 'Completed' && customobj.Stage__c != 'Cancelled DR/ VOID' && customobj.Stage__c != 'Draft') { Trigger.newMap.get(customobj.Case__c).addError('There are open Development Request associated with this Case.'); } } for(Task T : [Select Id, Status, WhatId From Task Where WhatId in: caseIds]) { if(T.RecordTypeId == TaskRecordTypeId && (T.Status != 'Completed' && T.Status != 'Discarded')) { Trigger.newMap.get(T.WhatId).addError('There are still open PM Task on this Case.'); } } }

 The .addError formatting for the  Training Information and Development Request above work perfectly.  I do not know what the issue is with the .addError issue with Tasks.....

 

 

for(Task T : [Select Id, Status, WhatId From Task Where WhatId in: caseIds]) { if(T.RecordTypeId == TaskRecordTypeId && (T.Status != 'Completed' && T.Status != 'Discarded')) { Trigger.newMap.get(T.WhatId).addError('There are still open PM Task on this Case.'); } }

 The above code is having issues with the line the .addError is on.

 

Error: Invalid Data.
Review all error messages below to correct your
data.
Apex trigger CaseCloseNoOpen caused an unexpected exception, contact
your administrator: CaseCloseNoOpen: execution of BeforeUpdate caused by:
System.SObjectException: SObject row was retrieved via SOQL without querying the
requested field: Task.RecordTypeId: Trigger.CaseCloseNoOpen: line 40, column 1

 

Any ideas?

 

Thank you,

Steve Laycock

MeanGeeks Founder, SAAS and PAAS Pro-Services
Certified Administrator, Certified Advanced Administrator, Beginning Developer Certs. Currently
Regular Contributor
MeanGeeks
Posts: 92
0

Re: Problem with raising error in correct format. Please help???

Forget it, I got it....   I didn't pull that field.

 

Thanks anyway.

Steve

MeanGeeks Founder, SAAS and PAAS Pro-Services
Certified Administrator, Certified Advanced Administrator, Beginning Developer Certs. Currently