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
KrishnadasKrishnadas 

Getting generic error message

Hi,

I have a Visualforce page that overrides the New button for a custom object. I have a quantity field with the inputtext tag and parameter assigned to it.
In the controller class I have set it up as double so that only numeric values will be accepted. All this is working fine except for the error I receive.
This is the error that I get now if try to enter characters and click on save
j_id0:form1:pb1:pbs2:j_id46:qty: An error occurred when processing your submitted information.
I do not want this. How do I avoid it?

Thanks
Krishnadas
Steadfast Global
www.steadfastglobal.com
wesnoltewesnolte

Hey

 

It's difficult to know without seeing the code but I'd guess that you need to use a try-catch around the code that saves the record. When an error is thrown you can use ApexPages.Message to display a userfriendly message. More on that can be found here.

 

Cheers,

Wes 

KrishnadasKrishnadas
Thanks Wes
Using the try-catch still displays the same error. I get this error only when I enter alphabets or special characters for Quantity. The rest of the validation errors are displayed fine.

Sample page code

<apex:page standardcontroller=”myobj__c” extensions=”myclass”>
<apex:pageMessages/>
<apex:form id=”form1”>
<apex:pageblock id=”pb1”>
<apex:pageblockSection id=”pbs1”>
<apex:pageblockSectionItem id=”pbsi1”>
<apex:inputText id=”name” value=”{!name}”>
</apex:pageblockSectionItem>
<apex:pageblockSectionItem id=”pbsi2”>
<apex:inputText id=”Qty” value=”{!qty}”>
</apex:pageblockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons id=”pbb1”>
<apex:commandbutton value=”Save” action={!Save}/>
</apex:pageBlockButtons>
</apex:pageBlock>
<apex:datatable value=”{!table}” var=”t”>
<apex:column headervalue=”Name”>
<apex:commandLink>
{!t.name}
<apex:param name=”name” assignto={!name} id=pname value=”{!t.Name}”/>
<apex:param name=”qty” assignto={!qty} id=pqty value=”{!t.Qty__c}”/>
</apex:commandLink>
</apex:column>
</apex:form>
</apex:page>

sample Controller code
public class myclass {
 myobj__c myobj;
 string name;
 double qty;
 list<myobj__c> objlist;
 public myclass(apexpages.standardcontroller stdcon) {
  this.myobj = (myobj__c)stdcon.getRecord();
 }
 public list<myobj__c> gettable() {
  objlist = [select id, name, qty__c from myobj__c];
  return objlist;
 }
 public string getname() { return this.name; }
 public void setname(string el) { this.name = el; }
 public double getqty() { return this.qty; }
 public void setname(double el) { this.qty = el; }
 public pagereference save() {
  myobj = new myobj__c(Name = this.name, qty__c = this.qty);
  insert myobj
  pagerference savepage = page.mypage;
  savepage.setredirect(true);
  return savepage();
 }
}
This is similar to what I am trying to do. Though there are other fields and checks that are done. I get the error only when I enter a alphabet or character for qty. Else the rest is fine. I just am trying to use my own error instead of the one that is currently displayed.


Thanks
Krishnadas
Steadfast Global
www.steadfastglobal.com
wesnoltewesnolte

Ah I see.I have some ideas let's try this first,

 

change 

 

myobj__c myobj;

 

to

 

public myobj__c myobj{get;set;} //object is now available to page

 

 and change

 

<apex:smileytongue:ageblockSectionItem id=”pbsi2”>
<apex:inputText id=”Qty” value=”{!qty}”>
</apex:smileytongue:ageblockSectionItem>

 

to

 

 <apex:inputField id=”Qty” value=”{!myobj.qty__c}”/> <!-- Now you have all formatting and any standard validation too -->

 

And you save method becomes

 

 public pagereference save() {
  MyObj__c newObj = new myobj__c(Name =
myobj .name, qty__c = myobj.qty__c); // Are you creating a new object using the old object values?

  insert newObj;
  pagerference savepage = page.mypage;
  savepage.setredirect(true);
  return savepage;
 }

 

You will then get a standard error message if you try to use the wrong type of values in the field. If this isn't what you need I have another solution;)

 

Cheers,

Wes

KrishnadasKrishnadas

Could you also provide me with the other solution. 

 

Thanks

Krishnadas

www.steadfastglobal.com 

wesnoltewesnolte

Sure.

 

Try changing this,

 

double qty;

 

to this

 

public String qty{get;set;]

 

and remove your getQty method.

 

Then change your save method to be

 

public pagereference save() {

Decimal dQty;

try{

   dQty = Decimal.valueOf(qty); // This will throw and exception if qty can't be cast

 

 } catch(System.Exception e){ //I'm not sure of the exact exception so I'm using a generic one

 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter a decimal value.'));

return null;

 

}

  myobj = new myobj__c(Name = this.name, qty__c = dQty);
  insert myobj
  pagerference savepage = page.mypage;
  savepage.setredirect(true);
  return savepage();
 }

 

You also don't need to use 'this' unless a method and the class containing the method have a variable with the same name.

 

Cheers,

Wes

nishad basha 7nishad basha 7

Hi, Krishnadas

my requirement is creating visualforce page when i click sumbit button new user should be created. but  i need now user enter a values click submit button it will be display on error message.give one example for user object. how to solve the this solution[lease give me any ideas.
 

 
nishad basha 7nishad basha 7
Hi, Pradeep Kumar L.G

  
my requirement is creating visualforce page when i click sumbit button new user should be created. but  i need now user enter a values click submit button it will be display on error message.give one example for user object. how to solve the this solution[lease give me any ideas.