+ Start a Discussion
Hari nadh babu EluruHari nadh babu Eluru 

Fee must be in between 0 to 5000

Hi there !
In this trigger apex code, The amount must be in between 0 to 5000 for Fee__c field
I tried above code
trigger Between_Values on Patient__c (before insert) {
    for(Patient__c a : trigger.new){
        if(a.Fee__c >= 0 || a.Fee__c <= 5000){
            a.Fee__c.adderror('Please enter amount b/w 0 to 5000');
        }
    }
}
Please give me the suggestions to implement the functionality condition
Best Answer chosen by Hari nadh babu Eluru
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

Please update the code as below.
 
trigger Between_Values on Patient__c (before insert) {
    for(Patient__c a : trigger.new){
        if(a.Fee__c < 0 || a.Fee__c > 5000){
            a.Fee__c.adderror('Please enter amount b/w 0 to 5000');
        }
    }
}

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Aditi Singh 41Aditi Singh 41
Better use a custom validation, for this situation.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

Please update the code as below.
 
trigger Between_Values on Patient__c (before insert) {
    for(Patient__c a : trigger.new){
        if(a.Fee__c < 0 || a.Fee__c > 5000){
            a.Fee__c.adderror('Please enter amount b/w 0 to 5000');
        }
    }
}

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
CharuDuttCharuDutt
Hii Hari 
Try Below Code
trigger Between_Values on Patient__c (before insert) {
    for(Patient__c a : trigger.new){
        if(a.Fee__c < 0 || a.Fee__c > 5000){
            a.Fee__c.adderror('Please enter amount b/w 0 to 5000');
        }
    }
}
======================================================================================
AND Validation Rule

AND(Fee__c<0,Fee__c>5000)

Please Mark It As Best Answer If It Helps
Thank You!

Hari nadh babu EluruHari nadh babu Eluru
@Sai Praveen, Thank you very much !