+ Start a Discussion
swapna9swapna9 

Problem with raising DML exception error in visualforce page

Hi,

 

in certailn condition i want dispaly an error in visual force page.but it is not happening in my code.

in my visual force page i am inserting records.in one condition i dont want to insert the record just display an error while saving the record.

in my code in that condition it is not inserted record but it is not displaying error message.it is going back to decision object.

 

Please guide me how to display an error in visual froce page.

 

my code is as follows.

 

vf page:

 

<apex:page controller="AddAssignments_controller" sidebar="false">
<script type = "text/javascript">
    function MutExChkList(chk)
    {
        var chkList = chk.parentNode.parentNode.parentNode;
        var chks = chkList.getElementsByTagName("input");
        for(var i=0;i<chks.length;i++)
        {
            if(chks[i] != chk && chk.checked)
            {
                chks[i].checked=false;
            }
        }
    }
</script>
<!--<style>
.employeeError { color: green; font-weight: strong;}
</style>-->
<apex:messages />
<apex:pageBlock >
    <html xmlns="http://www.w3.org/1999/xhtml">
    <apex:form >
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
   
    <apex:sectionHeader title="Add Assignments"/>
         
         
        <table width="60%" cellspacing="3" cellpading="2">
       <!-- <tr><td><table width="70%" cellpadding="1" cellspacing="3"> -->
        <tr>
           
            <td align="center"><b>Name</b></td>           
            <td align="center"><b>Accountable</b></td>          
                               
        </tr>    
       
        <tr><td>
        <table width="70%" cellpadding="1" cellspacing="3"> -->
        <tr>      
        <td align="center" width="14%"><apex:inputField value="{!assignment1.NameofUser__c}"/></td>       
        <td align="center" width="14%"><apex:inputField value="{!assignment1.Accountable__c}"  /></td>     
        
        </tr>
        
        <tr>      
        <td align="center" width="14%"><apex:inputField value="{!assignment2.NameofUser__c}"/></td>       
        <td align="center" width="14%"><apex:inputField value="{!assignment2.Accountable__c}" /></td>       
        </tr>
        
        <tr>       
        <td align="center" width="14%"><apex:inputField value="{!assignment3.NameofUser__c}"/></td>       
        <td align="center" width="14%"><apex:inputField value="{!assignment3.Accountable__c}" /></td>       
        </tr>      
               
        </table>          
              
        <apex:commandButton value="Save" action="{!save}"/>
    
    </apex:form>
   </html>
   </apex:pageBlock>  
</apex:page>

 

Apex class:

 

public class AddAssignments_controller  {
    public String getGetraci() {
        return null;
    }
    

integer tttt;
    public AddAssignments_controller(ApexPages.StandardSetController controller) {

    }
    public AddAssignments_controller(ApexPages.StandardController controller) {
    }
     Raci__c r = new Raci__c();
    public Raci__c getraci()    
    {      
    r = [select  Name,decision__c,
    Id from Raci__c
    where Id =:ApexPages.CurrentPage().getParameters().get('id')];              
    return r;
    }
    
   
    Public Id I = ApexPages.CurrentPage().getParameters().get('id');       
    public Raci__c assignment1{ get; set; }
    public Raci__c assignment2{ get; set; }     
             
    public AddAssignments_controller()
    {
        assignment1= new Raci__c();
        assignment2= new Raci__c();          
    }   
     
     public  PageReference save()   {
     
      if((assignment1.NameofUser__c!=null))
      {
          try
          {
          if( assignment1.Decision__r.Number_of_Accountable_Members__c >= 1 )
          {
             ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
            //assignment1.addError('Failed');            
           //here i need raise an error and should display in visual force page
          }
          else
          {
             assignment1.Decision__c=I;  
             insert assignment1;
          }
          }
           catch(DmlException ex){
            ApexPages.addMessages(ex);        
           
          }
      }     
       
       if(assignment2.NameofUser__c!=null)
       {
         assignment2.Decision__c=I;  
         insert assignment2;        
       }    
                 
      
        PageReference customer = new PageReference('https://ap1.salesforce.com/'+ I);
        customer.setRedirect(true);
        return customer ;
   }
    
     
}

 

Thanks in advance

 

Chamil MadusankaChamil Madusanka

Try following changes

 

<apex:page controller="AddAssignments_controller" sidebar="false">
<script type = "text/javascript">
    function MutExChkList(chk)
    {
        var chkList = chk.parentNode.parentNode.parentNode;
        var chks = chkList.getElementsByTagName("input");
        for(var i=0;i<chks.length;i++)
        {
            if(chks[i] != chk && chk.checked)
            {
                chks[i].checked=false;
            }
        }
    }
</script>
<!--<style>
.employeeError { color: green; font-weight: strong;}
</style>-->

<apex:pageBlock id="blockOne"> <apex:pageMessages ></apex:pageMessages>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <apex:form >
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
   
    <apex:sectionHeader title="Add Assignments"/>
         
         
        <table width="60%" cellspacing="3" cellpading="2">
       <!-- <tr><td><table width="70%" cellpadding="1" cellspacing="3"> -->
        <tr>
           
            <td align="center"><b>Name</b></td>           
            <td align="center"><b>Accountable</b></td>          
                               
        </tr>    
       
        <tr><td>
        <table width="70%" cellpadding="1" cellspacing="3"> -->
        <tr>      
        <td align="center" width="14%"><apex:inputField value="{!assignment1.NameofUser__c}"/></td>       
        <td align="center" width="14%"><apex:inputField value="{!assignment1.Accountable__c}"  /></td>     
        
        </tr>
        
        <tr>      
        <td align="center" width="14%"><apex:inputField value="{!assignment2.NameofUser__c}"/></td>       
        <td align="center" width="14%"><apex:inputField value="{!assignment2.Accountable__c}" /></td>       
        </tr>
        
        <tr>       
        <td align="center" width="14%"><apex:inputField value="{!assignment3.NameofUser__c}"/></td>       
        <td align="center" width="14%"><apex:inputField value="{!assignment3.Accountable__c}" /></td>       
        </tr>      
               
        </table>          
              
        <apex:commandButton value="Save" action="{!save}" reRender="blockOne"/>
    
    </apex:form>
   </html>
   </apex:pageBlock>  
</apex:page>

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

swapna9swapna9

Thanks for ur reply,

 

But still it is not displaying error message.

can u please tel me where i can change...