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
Rahul AswaleRahul Aswale 

please can you tell me code apex trigger to show error message when student percentage is above 100or greater than 100

Apex trigger
Best Answer chosen by Rahul Aswale
Ronak Toshniwal 9Ronak Toshniwal 9

Hi Rahul,

i hope you can write apex class with appropriate events 

i am telling you logic
this code will mark the field with error

if(student.Percent__c > 100){
   student.percent__c.addError('Student Percent cannot be more than 100%')
}
OR
this will show error at the bottom of the form
if(student.Percent__c > 100){
   student.addError('Student Percent cannot be more than 100%')
}
 

If it solved your query
please mark my answer as best answer :)


 

All Answers

Ronak Toshniwal 9Ronak Toshniwal 9

Hi Rahul,

i hope you can write apex class with appropriate events 

i am telling you logic
this code will mark the field with error

if(student.Percent__c > 100){
   student.percent__c.addError('Student Percent cannot be more than 100%')
}
OR
this will show error at the bottom of the form
if(student.Percent__c > 100){
   student.addError('Student Percent cannot be more than 100%')
}
 

If it solved your query
please mark my answer as best answer :)


 

This was selected as the best answer
PriyaPriya (Salesforce Developers) 
Hey Rahul,

This is syntax for displaying the error message using trigger. You can add your condition accordingly.
 
for (Account a : Trigger.new) {
  if ( /* whatever your error condition is */ ) {
    a.addError('Your custom error message');
  }
}

For more detail, refer this link (https://www.levelupsalesforce.com/salesforce-display-error-message-from-trigger)
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan


 
Rahul AswaleRahul Aswale
trigger TestPercent on Student__c (after insert) {

   for( Student__c std: trigger.new ){
  
       if(std.Number_Of_Percentage__c > 100){
          
           std.addError('Student Percent cannot be more than 100%');
       }
   }
}
i have write this code but not showing error message when i enter above 100
PriyaPriya (Salesforce Developers) 
Try this code :-
 
trigger TestPercent on Student__c (before insert) {

   for( Student__c std: trigger.new ){
  
       if(std.Number_Of_Percentage__c > 100){
          
           std.addError('Student Percent cannot be more than 100%');
       }
   }
}


If it solved your query
please mark my answer as best answer.

Thanks
Priya Ranjan

PriyaPriya (Salesforce Developers) 
Have you resolved the issue?
 
Vanda EgertonVanda Egerton

Here's an example of an Apex trigger that shows an error message when a student's percentage is above 100 or greater than 100:
trigger StudentPercentageValidation on Student__c (before insert, before update) {
    for(Student__c student : Trigger.new) {
        if(student.Percentage__c > 100) {
            student.addError('Percentage cannot be above 100.');
        }
    }
}

In this example, the trigger StudentPercentageValidation is executed before a student record is inserted or updated (before insert, before update). It loops through each student record in the trigger's context (Trigger.new) and checks if the Percentage__c field value is greater than 100. If it is, the addError() method is called on the student record, which associates an error message with the record and prevents it from being saved.
Make sure to replace Student__c with the actual API name of your student object and Percentage__c with the API name of your percentage field.
Lincoln SchulerLincoln Schuler
Vanda, it seems that you are right, besides I have to say that until not long ago I was also not very good at these skills, and not long ago I discovered this source https://paperap.com/free-papers/study-skills/ which helped me to understand what are study skills, it really adjusted me a lot and now I learn everything much faster, I recommend you to inform yourself about it to have a deeper knowledge of the material.
sravani proddatur 10sravani proddatur 10
Hi Rahul,

trigger stdpercent Student__c (before insert) {

   for( Student__c  std :  trigger.new ){
  
       if(std.Number_Of_Percentage__c > 100){
          
           std.Number_Of_Percentage__c.addError('Student Percent cannot be more than 100%');
       }
   }