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
Scott.MScott.M 

Extra DML Validation Error Display

Hi all,

I've created a visual force page with a controller extension that makes edits to a custom object and a list of related custom objects. I've added a custom validation rule to the related object and I would like the error messages for the related object to appear correctly in the page however only error messages for the parent object which the standard controller handles displayes the validation error correctly. If there is a validation error in the child objects it throws the exception and goes to an ugly exception page.

Here's the save function:

Code:
public PageReference save(){
  upsert PartOrder;

  for(PartOrderPart p: PartOrderParts) {
    p.pop.Part_Order__c = this.PartOrder.Id;
    upsert p.pop;
  }


  PageReference poPage = new PageReference('/' + PartOrder.id);
  poPage.setRedirect(true);
  return poPage;

}

 
I thought about using a try catch to catch the exceptions and store them in an array then display them

Code:
for(PartOrderPart p: PartOrderParts) {
  try {
    p.pop.Part_Order__c = this.PartOrder.Id;
    upsert p.pop;
  } catch (System.DmlException e) {
    //store error in an array
  }

} 

But there's no if type statements in the visualforce tag library so I'm not sure how I would check to see if there are errors and if there are display them, and if not don't display anything. Any help is much appreciated.



Message Edited by Scott.M on 06-03-2008 06:47 AM
Best Answer chosen by Admin (Salesforce Developers) 
Scott.MScott.M
I figured out the solution and it's pretty simple :) . The key is the rendered attribute. What you can do is put an expression in the rendered attribute to determine whether to display it or not. so the code ends up being somthing like:
 
Code:
try{
  upsert upops;
} catch (System.DmlException e) {
  for(Integer i =0; i<e.getNumDml(); ++i) { 
this.PartOrderParts[e.getDmlIndex(i)].setErrorMessage(e.getDmlMessage(i)); }
return null; } PageReference poPage = new PageReference('/' + PartOrder.id); poPage.setRedirect(true); return poPage;

Then in the visualforce page doing something like this will show the error only if there is an error message

Code:
<apex:messages rendered="{!PartOrderPart.ErrorMessage != null}" >
  {!PartOrderPart.ErrorMessage}
</apex:messages>

Pretty slick and no need for if statements. Hopefully this helps someone in the future

 


 




Message Edited by Scott.M on 06-04-2008 07:16 AM

Message Edited by Scott.M on 06-04-2008 07:18 AM

All Answers

Scott.MScott.M
I figured out the solution and it's pretty simple :) . The key is the rendered attribute. What you can do is put an expression in the rendered attribute to determine whether to display it or not. so the code ends up being somthing like:
 
Code:
try{
  upsert upops;
} catch (System.DmlException e) {
  for(Integer i =0; i<e.getNumDml(); ++i) { 
this.PartOrderParts[e.getDmlIndex(i)].setErrorMessage(e.getDmlMessage(i)); }
return null; } PageReference poPage = new PageReference('/' + PartOrder.id); poPage.setRedirect(true); return poPage;

Then in the visualforce page doing something like this will show the error only if there is an error message

Code:
<apex:messages rendered="{!PartOrderPart.ErrorMessage != null}" >
  {!PartOrderPart.ErrorMessage}
</apex:messages>

Pretty slick and no need for if statements. Hopefully this helps someone in the future

 


 




Message Edited by Scott.M on 06-04-2008 07:16 AM

Message Edited by Scott.M on 06-04-2008 07:18 AM
This was selected as the best answer
dchasmandchasman
You answered your own question before I had a chance to respone :-) I wanted to thank you for taking the time to post a solution to your own question - that is extremely helpful for the VF community!

As a side note rendered is very often the best fit once you get used to this style of conditional rendering. With that said there are times when we know that something more along the lines of an if-then-else style or case statements would be useful (can always be acoomplished with rendered but when we're talking about one or more releated alternatives this can seem a bit unnatural or cumbersome) and we are evaluating additional components that would support this.

http://ideas.salesforce.com/article/show/10089418/Add_conditional_block_Visualforce_components_eg_ltapexif_ltapexcase_etc?skin=null

Please promote/vote on this idea if you want to see us add this functionality :-)


Message Edited by dchasman on 06-06-2008 10:24 AM
thecoldfusionthecoldfusion
While the conditional

{!PartOrderPart.ErrorMessage != null}

is great for generating truth value for rendered component, I am finding it hard to generate conditional output to be used in regular html element attributes.

For example, I want to generate title of an anchor for jquery-cluetip. For my case, The title attribute should be either empty string or a pipe character prepended to tooltip text. This is what I want to do

title = " ({!tooltip != null}) ? (|{!tooltip}) : (''))

If getTooltip() returns something like 'test', title is set to

(true) ? (|test) : ('')

instead of just

|test


What am I missing here?
I can prepend the pipe character in getTooltip method but that would be my last resort. Please help.
Thanks.
dchasmandchasman
Ideally you would be able to use the following but there is a known issue with the use of the page formule concat operator '&' that eliminates this approach for now:

Code:
title = "{!if(tooltip != null, '|' & tooltip, '')}"

so for now this is a slightly more verose way to achieve the same thing:

Code:
title = "{!if(tooltip != null, '|', '')}{!nullValue(tooltip, '')}"


Message Edited by dchasman on 06-18-2008 03:50 PM