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
Salesforce GeevanSalesforce 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');
            }
                
        }
    }
}
Rohit K SethiRohit K Sethi
hi,

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:

trigger TriggerCus on Customer__c (before insert) {
    Set<String> nameSet = new Set<String>();
    Set<String> mobileSet = new Set<String>();
    for(Customer__c a:Trigger.new)
    {
        nameSet.add(a.name);
        mobileSet.add(a.mobile__c);
    }
    list<Customer__c> lstCustomer=[select Mobile__c,Name__c from Customer__c where name in :nameSet and mobile__c in : mobileSet];
    for(Customer__c custOuter : trigger.new){
        for(Customer__c custInner : lstCustomer){
            if(custOuter.name__c==custInner.name__c && custOuter.mobile__c == custInner.mobile__c){
                custOuter.adderror('Duplicate name');
            }
        }
    }
}

Thanks.
Ajay K DubediAjay K Dubedi
Hi Geevan,
Just you have to add try and catch in your controller.
so replace your controller with this:
public class Customer{
Public Customer__c cus{get;set;}

public Customer(){
     cus = new Customer__c();
}
public void Custom_Save()
{
try{  
insert cus;
}
catch (DMLException e){
ApexPages.addMessages(e);
}
}
}
Regards,
Ajay