You need to sign in to do that
Don't have an account?
Richa_Learning
Validation rule in Trigger
Hi,
I am trying to add a validation rule in trigger that if
Details status = 'In Progress" then Tracker status cannot be updated to Closed.
I get this error "Comparison arguments must be compatible types: Schema.SObjectField"
Please help
I am trying to add a validation rule in trigger that if
Details status = 'In Progress" then Tracker status cannot be updated to Closed.
I get this error "Comparison arguments must be compatible types: Schema.SObjectField"
trigger Tracker_AfterUpdate on Tracker__c (After Update) { if(Trigger.IsAfter && Trigger.IsUpdate) { Set<Id> TrackerIDs = new set<Id>(); for(Tracker__c Tempdata : trigger.new) { if(Tempdata.Status__c=='Closed' && details__c.status__c=='In progress') { Tempdata.Status__c.adderror ('not allowed'); } } } }
Please help
Sorry - one modification:
All Answers
On line 12 - I don't think you can add the error to a field for apex validation. If I recall correctly, addError() is only an sObject method.
Try rewriting it like this - but I would definitely give it a more descriptive error for production since the error will show at the top of the page.
I'm assuming details is a lookup field on the Tracker__c object? If so, then you have to treat it as a relationship.
try this:
Now I get this error:
If I comment the Adderror row , I get this:
Details has lookup relationship to Tracker.
As far as the details - can you describe how tracker & details works in your org? id the details an object related to the tracker or is tracker related item to details? Is this a one to one relationship or a one to many?
There are other options - the first and easiest I can think of depends on whether your details object is master-detail to tracker?
k - so can't go the easy route to where you set up a rollup summary field and then use a traditional validation rule.
You'll need to query all of the details and put them in a list for iterating. Something like this. I haven't compiled it, so I'm not sure if any errors will pop up.
Sorry - one modification:
Glad I could help! Good luck on the test class :)
Last thing I want to caution as it happened to me when I started out - is at some point when you get a lot of code, you're going to want to put more control on when that query fires, such as only when the tracker status changes (compare trigger.old with trigger.new to do this). Also, if you have many triggers you'll want to move this into an apex class so you can control which methods/triggers you want to fire first.