+ Start a Discussion
Hari nadh babu EluruHari nadh babu Eluru 

Pincode must be exactly 6 digits

In this trigger apex, we can give Pincode__c text field. Pincode must be only 6 digits. If Pincode is exceeded 6 digits then we can show error message as "Pincode must be 6 digits"
I tried above code but not getting output:-
trigger Exact_Six_Digits on Customer__c (before insert) {
    for(Customer__c u : trigger.new){
        if(u.Pincode__c.length()!=6){
            u.Pincode__c.adderror('Pincode must be 6 digits');
        }
    }
}
Please give me suggestions for how to implement the condition. Thank you !
 
Best Answer chosen by Hari nadh babu Eluru
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

I don't see any issue with the code. Can you refresh the page and try to create a new record and check it.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

What type of field is pincode?

Thanks,
 
Hari nadh babu EluruHari nadh babu Eluru
text field
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

I don't see any issue with the code. Can you refresh the page and try to create a new record and check it.

Thanks,
 
This was selected as the best answer
CharuDuttCharuDutt
Hii Hari
Try Below Code
trigger Exact_Six_Digits on Customer__c (before insert) {
    for(Customer__c u : trigger.new){
        if(u.Pincode__c.length()>6 && u.Pincode__c.length()<=0){
            u.Pincode__c.adderror('Pincode must be 6 digits');
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!