0

I have a visualforce page for a custom object MyActivity__c. MyActivity__c has two child objects, Actions and Issues. I am using wrapper classes to add/ create new records for the 2 child objects on the page. Each action and issue added has a custom field 'Description__c' that must be filled out before saving. (see below image) enter image description here Rather than having the standard "Error: You must enter a value" message displayed by salesforce, I would like make my own custom error messages. My PageReference save looks like:

public PageReference save()
{
    List<Issue__c> issues=new List<Issue__c>();
    for (IssueKeyWrapper wrapper : issueWrappers)
    {
        issues.add(wrapper.is);
    }

    upsert issues;

    List<Action__c> actions=new List<Action__c>();
    for (ActionKeyWrapper wrapper : actionWrappers)
    {
        actions.add(wrapper.ac);
    }

    upsert actions;

    delete issuesToDelete; 
    delete actionsToDelete; 

    return stdCtrl.save();
}

and my wrapper looks like:

public with sharing class IssueKeyWrapper 
{
    public Integer key {get; set;}
    public Issue__c is {get; set;}

public IssueKeyWrapper(Integer inKey, Issue__c inCs)
{
    is=inCs;
    key=inKey;

    }

}

My VF wrapper code looks like

  <table class="list" border="1" cellpadding="0" cellspacing="0" style="display: {!IF(issueWrappers.size=0, 'none', 'table')};">
    <tr class="headerRow">
      <th colspan="2" style="width:50%">Issue Description</th>
      <th style="width:5%"></th>
      <th style="width:10%"></th>
    </tr>
    <apex:repeat value="{!issueWrappers}" var="issueWrap">
      <tr class="dataRow"> 
        <td colspan="2" >
          <apex:inputField style="width:80%" value="{!issueWrap.is.Description__c}" styleClass="masterClass commentBox2"/>
        </td>
        <td >

        </td>
        <td >
          <apex:commandButton value="Delete" action="{!deleteIssue}" rerender="tableTwo">
            <apex:param name="issueToDel" value="CT{!issueWrap.key}" assignTo="{!issueToDel}"/>
          </apex:commandButton> 
        </td>
      </tr>
    </apex:repeat>
  </table>
  <apex:commandButton value="Add Issue" action="{!addIssue}" rerender="tableTwo" immediate="true" />

In the PageReference save method I want to put some logic such as`

PageReference result = null; ..... return result; 

and in the wrapper I'd like to add the logic `

if(String.isBlank(is.Description__c))
{
is.Description__c.addError('Hey, don't forget to add a description');
}
else {result = stdCtrl.save();}

Obviously this doesn't work, or I wouldn't be here :-) So I guess my question is, how do I correctly declare the result variable in the wrapper class, and then how do I connect that to the PageReference save method in my custom controller class? Or is there a better way to do this?

UPDATE: I found a piece of code from the VF cookbook that I'm trying to use in my code. Their example is for the contact object. Is there a way I can do this?

public PageReference save()
{
    PageReference result=null;
    Contact cont=(Contact) stdCtrl.getRecord();

    if ( (String.IsBlank(cont.Email)) )
    {
        cont.email.addError('Please enter an email address');
    }
    else
    {
        result=stdCtrl.save();
    }      
    return result;
}

Where I'm stuck is with the else { result=stdCtrl.save();}. How can I put that in the wrapper and have it be referenced in the PageReference method?

  • Can you post some of the Visualforce? Presumably you're not using <apex:inputField> for the description? – metadaddy Jan 15 '15 at 18:36
  • Hi @metadaddy . Thanks for answering, I do have <apex:inputField>, please see modified question above – rwegner7 Jan 15 '15 at 18:47
  • What behavior are you seeing? An Apex/Visualforce error? The error message not appearing as expected? – metadaddy Jan 15 '15 at 18:54
  • I think I understand what you are trying to do. Have a look at this resource, focus on how the code is adding the error message on the lead. developer.force.com/cookbook/recipe/… Also your idea about returning a null page reference. See this question salesforce.stackexchange.com/questions/63286/… . Let me know how you get on and will see if we can help you further. – Mihai Neagoe Jan 15 '15 at 19:02
  • @metadaddy Thank you, I'm trying to integrate custom error logic from the VF cookbook in my code (please see the answer I posted below which shows an example of this). My thought is that I should declare the if(isBlank(field)){addError();)} logic in my wrapper class, not the PageReference save method. But what should I do with the 'result' variable? Put it in the method or the wrapper? Or both? – rwegner7 Jan 15 '15 at 19:43
1

You should continue saving the record as it's a valid object.

public PageReference save()
{
    PageReference result=null;
    Contact cont=(Contact) stdCtrl.getRecord();
    if(String.IsNotBlank(cont.Email) || String.isNotEmpty(cont.Email)){
            insert contact;
            // Send the user to the detail page for the new contact.
            PageReference result = new Page.Your_VF_Page;
            result.getParameters().put('id',contact.Id);
            result.setRedirect(true);
    }
    else{
       cont.email.addError('Please enter an email address');
    }
    return result;
}

Can you also see if you can use this method.

PageReference contactPage = new ApexPages.StandardController(contact).view();
contactPage.setRedirect(true);
return contactPage;

Personally I think the second option is more elegant.

Hope it helps

  • Sorry, you will need the else ... how you then add the error if the email is blank or empty? My bad, updated my answer, hope it didn`t cause you too much trouble. – Mihai Neagoe Jan 16 '15 at 8:33
0

Here's my not so elegant answer, but it works!`

public PageReference save()
    {

        PageReference result = null;
        Boolean boolIssue = false;
        Boolean boolAction = false;

        List<VOIssue__c> issues=new List<VOIssue__c>();
        for (IssueKeyWrapper wrapper : issueWrappers)
        {
           issues.add(wrapper.is);

           if(String.isBlank(wrapper.is.Description__c))
           {
               wrapper.is.Description__c.addError('Please enter a description for Issue');
               boolIssue = true;
           }

        }

        if(boolIssue == false)
        {
            upsert issues;
        }

        List<VOAction__c> actions=new List<VOAction__c>();
        for (ActionKeyWrapper wrapper : actionWrappers)
        {
            actions.add(wrapper.ac);

            if(String.isBlank(wrapper.ac.Description__c))
            {
               wrapper.ac.Description__c.addError('Please enter a description for Action');
               boolAction = true;
            }
            if(wrapper.ac.Date__c==null || wrapper.ac.Date__c > dateTime.now())
            {
               wrapper.ac.Date__c.addError('Date must be either day of or in the past');
               boolAction = true; 
            }            
        }

        if(boolAction == false)
        {
            upsert actions;
        }

        delete issuesToDelete; 
        delete actionsToDelete;

        if(boolIssue == false && boolAction == false)
        {
            result = stdCtrl.save();
        }

        return result;

    }
}`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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