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
Rung41Rung41 

Validation error message not displaying correctly.

I have an issue where the validation rule error message on a custom object doesn't appear at the top or field level but instead on a separate page with a DML exception error.  I know I could use a catch statment but not sure where to put it.

 

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Alternative Delivery date must be after the Delivery Date. Please adjust the Alternative Delivery date.: [] 

Class.NewRenterOpportunity.Save: line 12, column 1

 

Class:

public class NewRenterOpportunity {

    public Opportunity opp{get;set;}
    public Renter__c rentr{get;set;}
    public NewRenterOpportunity(ApexPages.StandardController controller)
    {
        rentr = (Renter__c)controller.getRecord();
        Id id = ApexPages.currentPage().getParameters().get('id');
     
    }
    public PageReference Save(){
     insert rentr;
        opp = new Opportunity();
        opp.Name =  rentr.Renter_s_First_Name__c + ' ' + rentr.Name;
        opp.Renters_Email__c = rentr.Renters_Email__c;
        opp.Renters_Phone__c = rentr.Renters_Phone__c;
        opp.StageName = 'Proposal/Price Quote';
        opp.CloseDate = System.today()+2;
        opp.Classification__c = rentr.Classification__c;
        opp.Lease_Term__c = rentr.Lease_Terms__c;
        opp.Residential_Opportunity_Source__c = rentr.Source__c;
        opp.Renter_s_Price_Range__c = rentr.Budget__c;
        opp.Need_Office_Furniture__c = rentr.Need_Office_Furniture__c;
        opp.Need_Housewares__c = rentr.Need_Housewares__c;
        opp.Need_Finishing_Touches__c = rentr.Need_Finishing_Touches__c;
        opp.Need_Electronics_Appliances__c = rentr.Need_Electronics_Appliances__c;
        opp.Description_of_Items__c = rentr.Details_of_what_they_need__c;
        opp.Other_Potential_Renters__c = rentr.Other_that_need_to_rent__c;
        opp.Pertainent_Notes__c = rentr.Why_the_need_to_rent_furniture__c; 
        opp.Potential_Renters__c = rentr.Other_Potential_Renters__c;
        opp.Delivery_Date__c = rentr.Delivery_Date__c;
        opp.Alternate_Delivery_Date__c = rentr.Alternate_Delivery_Date__c;
        opp.Delivery_Street__c = rentr.Delivery_Street__c;
        opp.Delivery_City__c = rentr.Delivery_City__c;
        opp.Delivery_State__c = rentr.Delivery_State__c;
        opp.Delivery_Zip__c = rentr.Delivery_Zip__c;
        opp.Showroom__c = rentr.Showroom__c;
 
        String AccountId;
        opp.Corporation__c = rentr.Company__c; 
        
  
             
        insert opp;
        
        PageReference ref = new PageReference('/'+opp.id);
        return ref;
    }
    public PageReference NewRenters(){
        return null;
    }
    public PageReference Cancel(){
        return null;
    }
  
}

 

 

kiranmutturukiranmutturu

just try this

 public PageReference Save(){
     insert rentr;
try{
        opp = new Opportunity();
        opp.Name =  rentr.Renter_s_First_Name__c + ' ' + rentr.Name;
        opp.Renters_Email__c = rentr.Renters_Email__c;
        opp.Renters_Phone__c = rentr.Renters_Phone__c;
        opp.StageName = 'Proposal/Price Quote';
        opp.CloseDate = System.today()+2;
        opp.Classification__c = rentr.Classification__c;
        opp.Lease_Term__c = rentr.Lease_Terms__c;
        opp.Residential_Opportunity_Source__c = rentr.Source__c;
        opp.Renter_s_Price_Range__c = rentr.Budget__c;
        opp.Need_Office_Furniture__c = rentr.Need_Office_Furniture__c;
        opp.Need_Housewares__c = rentr.Need_Housewares__c;
        opp.Need_Finishing_Touches__c = rentr.Need_Finishing_Touches__c;
        opp.Need_Electronics_Appliances__c = rentr.Need_Electronics_Appliances__c;
        opp.Description_of_Items__c = rentr.Details_of_what_they_need__c;
        opp.Other_Potential_Renters__c = rentr.Other_that_need_to_rent__c;
        opp.Pertainent_Notes__c = rentr.Why_the_need_to_rent_furniture__c; 
        opp.Potential_Renters__c = rentr.Other_Potential_Renters__c;
        opp.Delivery_Date__c = rentr.Delivery_Date__c;
        opp.Alternate_Delivery_Date__c = rentr.Alternate_Delivery_Date__c;
        opp.Delivery_Street__c = rentr.Delivery_Street__c;
        opp.Delivery_City__c = rentr.Delivery_City__c;
        opp.Delivery_State__c = rentr.Delivery_State__c;
        opp.Delivery_Zip__c = rentr.Delivery_Zip__c;
        opp.Showroom__c = rentr.Showroom__c;
 
        String AccountId;
        opp.Corporation__c = rentr.Company__c; 
        
  
             
        insert opp;
    }catch(exception ex){
system.debug('exception mes'+ex.getMessage());
}    
        PageReference ref = new PageReference('/'+opp.id);
        return ref; 
Devendra NataniDevendra Natani

Hello,

 

We should always write a code by proper exception handling. In your code please use the try catch in save method.

 

For e.g. 

try{
 // write your code here
}
catch(Exception e){
Apexpages.addmessages(e);
}

 

And finally on VF page, please add the following code -

 

<apex:pagemessages />

 

 

 

 

 

Rung41Rung41

I tried both suggestions and I still get the same error.

 

public class NewRenterOpportunity {

    public Opportunity opp{get;set;}
    public Renter__c rentr{get;set;}
    public NewRenterOpportunity(ApexPages.StandardController controller)
    {
        rentr = (Renter__c)controller.getRecord();
        Id id = ApexPages.currentPage().getParameters().get('id');
     
    }
    public PageReference Save(){
     insert rentr;
  try{   
        opp = new Opportunity();
        opp.Name =  rentr.Renter_s_First_Name__c + ' ' + rentr.Name;
        opp.Renters_Email__c = rentr.Renters_Email__c;
        opp.Renters_Phone__c = rentr.Renters_Phone__c;
        opp.StageName = 'Proposal/Price Quote';
        opp.CloseDate = System.today()+2;
        opp.Classification__c = rentr.Classification__c;
        opp.Lease_Term__c = rentr.Lease_Terms__c;
        opp.Residential_Opportunity_Source__c = rentr.Source__c;
        opp.Renter_s_Price_Range__c = rentr.Budget__c;
        opp.Need_Office_Furniture__c = rentr.Need_Office_Furniture__c;
        opp.Need_Housewares__c = rentr.Need_Housewares__c;
        opp.Need_Finishing_Touches__c = rentr.Need_Finishing_Touches__c;
        opp.Need_Electronics_Appliances__c = rentr.Need_Electronics_Appliances__c;
        opp.Description_of_Items__c = rentr.Details_of_what_they_need__c;
        opp.Other_Potential_Renters__c = rentr.Other_that_need_to_rent__c;
        opp.Pertainent_Notes__c = rentr.Why_the_need_to_rent_furniture__c; 
        opp.Potential_Renters__c = rentr.Other_Potential_Renters__c;
        opp.Delivery_Date__c = rentr.Delivery_Date__c;
        opp.Alternate_Delivery_Date__c = rentr.Alternate_Delivery_Date__c;
        opp.Delivery_Street__c = rentr.Delivery_Street__c;
        opp.Delivery_City__c = rentr.Delivery_City__c;
        opp.Delivery_State__c = rentr.Delivery_State__c;
        opp.Delivery_Zip__c = rentr.Delivery_Zip__c;
        opp.Showroom__c = rentr.Showroom__c;
 
        String AccountId;
        opp.Corporation__c = rentr.Company__c; 
        
  
             
        insert opp;
   
   
    }
catch(Exception ex){
system.debug('exception mes'+ex.getMessage());

}
   
   
        PageReference ref = new PageReference('/'+opp.id);
        return ref;
        
    }
    public PageReference NewRenters(){
        return null;
    }
    public PageReference Cancel(){
        return null;
    }
  
}