3

I wrote a trigger to throw validation error on ILR object record. This is a custom object. The purpose of this trigger is : when any ilr record is cloned from the parent using custom cloning functionality it checks that no child record should have same month entered as parent. And child record is named as : 'Copy of '+.

I want my adderror string to come up in the VF page with red colour..but nothing is coming up..and I am able to save child record with same month as parent. While checking in debug log there I found my code is reaching the adderror message block.

Could you pls suggest if there is anything wrong with this code below.
and suggest the syntax of to display teh error mesg from triiger in VF page?

8
  • 2
    Do you have an <apex:pageMessages/> on your VF page? Commented Dec 17, 2013 at 13:54
  • You would have to handle it yourself in the VF page with your styling. You would need to capture the exception and display it using apex:pageMessages. Take a look at this link boards.developerforce.com/t5/Visualforce-Development/…
    – Prady
    Commented Dec 17, 2013 at 14:03
  • Are you sure your trigger works ? I think you'll only execute logic on the last item given to the trigger. And are you sure "FROM ILR__c where Name=:Name];" shouldn't be "where Name =:pName];' ? Commented Dec 17, 2013 at 14:05
  • Daniel--> yes, I have page message in VF..I could see error msg from Apex class in the screen bt from trigger its not coming..this trigger works as I am getting the debug statement in debug log..Sdry-->pName is added to List "Name".Prady --> why it isnt possible from trigger?
    – user4644
    Commented Dec 17, 2013 at 14:26
  • Please post your VF code as well. Commented Dec 17, 2013 at 17:27

2 Answers 2

3

May be I am seeing a fundamental issue here. You can loop through your query object to access the fields; but use Trigger.new or trigger.newMap to add your errors. Specifically, your addError method should be changed like this.

ILR__c trigObj = trigger.newMap.get(ilrCld.id);
if(trigObj != null){
    trigObj.addError('Cloned record should not have the same period as that of Parent record');
}

Hope this will resolve your issue.

1

I can think about two issue that can be problem here: First of all please try add error to object from Trigger.new list (but I think in your code better way will be use Trigger.newMap). Also you can try move tag inside the form tag.

But In my opinion you should consider moving this validation to Page controller class.

You must log in to answer this question.