You need to sign in to do that
Don't have an account?
Salesforce Geevan
Apex code & Trigger
The below is my code. when i run this code without trigger , its working properly...
I wrote a trigger for checking duplicates (name,mobile), while submitting it throws,
Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Duplicate name: []
Error is in expression '{!Custom_Save}' in component <apex:commandButton> in page customer: Class.Customer.Custom_Save: line 8, column 1
An unexpected error has occurred. Your development organization has been notified.
VF Page:
<apex:page controller="Customer">
<apex:form > <apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!cus.Name__c}"/><br/>
<apex:inputField value="{!cus.Age__c}"/><br/>
<apex:inputField value="{!cus.DOB__c}"/><br/>
<apex:inputField value="{!cus.ITEM__c}"/><br/>
<apex:inputField value="{!cus.Mobile__c}"/><br/>
<apex:inputField value="{!cus.Quantity__c}"/><br/>
<apex:inputField value="{!cus.Status__c}"/><br/>
<apex:inputField value="{!cus.DATE_TIME__c}"/><br/> <apex:inputField value="{!cus.Yes_No__c}"/>
<apex:commandButton value="Save" action="{!Custom_Save}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Apex Code
Public class Customer{
Public Customer__c cus{get;set;}
public Customer(){
cus = new Customer__c();
}
public void Custom_Save()
{
insert cus;
}
}
Trigger
trigger TriggerCus on Customer__c (before insert) {
for(Customer__c a:Trigger.new)
{
if((a.Mobile__c!=null)&&(a.Name__c!=null))
{
list<Customer__c> Var=[select Mobile__c,Name__c from Customer__c];
if(var.size()>0)
{
a.adderror('Duplicate name');
}
}
}
}
I wrote a trigger for checking duplicates (name,mobile), while submitting it throws,
Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Duplicate name: []
Error is in expression '{!Custom_Save}' in component <apex:commandButton> in page customer: Class.Customer.Custom_Save: line 8, column 1
An unexpected error has occurred. Your development organization has been notified.
VF Page:
<apex:page controller="Customer">
<apex:form > <apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!cus.Name__c}"/><br/>
<apex:inputField value="{!cus.Age__c}"/><br/>
<apex:inputField value="{!cus.DOB__c}"/><br/>
<apex:inputField value="{!cus.ITEM__c}"/><br/>
<apex:inputField value="{!cus.Mobile__c}"/><br/>
<apex:inputField value="{!cus.Quantity__c}"/><br/>
<apex:inputField value="{!cus.Status__c}"/><br/>
<apex:inputField value="{!cus.DATE_TIME__c}"/><br/> <apex:inputField value="{!cus.Yes_No__c}"/>
<apex:commandButton value="Save" action="{!Custom_Save}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Apex Code
Public class Customer{
Public Customer__c cus{get;set;}
public Customer(){
cus = new Customer__c();
}
public void Custom_Save()
{
insert cus;
}
}
Trigger
trigger TriggerCus on Customer__c (before insert) {
for(Customer__c a:Trigger.new)
{
if((a.Mobile__c!=null)&&(a.Name__c!=null))
{
list<Customer__c> Var=[select Mobile__c,Name__c from Customer__c];
if(var.size()>0)
{
a.adderror('Duplicate name');
}
}
}
}
In trigger there is a problem :
1.First you are query all the customer and check the size. It means If any one customer is exists then it will raise error.
Use below code for trigger:
Thanks.
Just you have to add try and catch in your controller.
so replace your controller with this: Regards,
Ajay