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
rameshramesh 

My trigger code in below how writte same code Apex Trigger help class

Create a Trigger on the Account Object, to make sure each Account Record should have "Rating, Phone and Fax Field values".

   

    Object Name : Account Object.

    Event Name  : Before Insert.

   

    trigger AccountsTrigger on Account (before insert) 

    {

        if(Trigger.isInsert && Trigger.isBefore)

        {

            for(Account accRecord : Trigger.New)

            {

                if(accRecord.Rating == Null || accRecord.Rating == '')

                    accRecord.Rating.AddError('Please Select the Rating Value.');

                else if(accRecord.Phone == Null || accRecord.Phone == '')

                    accRecord.Phone.AddError('Please Enter the Contact Number.');

                else if(accRecord.Fax == Null || accRecord.Fax == '')

                    accRecord.Fax.AddError('Please Enter the Fax Number.');

            }

        }

    }

   

Best Answer chosen by ramesh
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sai,

try with below code.

trigger:
trigger validation on Account (before insert) {
    
    If(trigger.Isbefore && trigger.Isinsert){
        TriggerhelperClass.PhoneFaxRatingValidation(trigger.new);
    }

}
Class:
public class TriggerhelperClass {
    
    public static void PhoneFaxRatingValidation(List<account> acclist){
        
        for(account acc:acclist) {
            
            if(acc.Rating == Null || acc.Rating == ''|| acc.Fax == Null ||acc.Fax ==''||acc.phone == Null||acc.phone==''){
                acc.Rating.adderror('Please Select the Rating Value');
                acc.Phone.adderror('Please Select the Rating Value');
                acc.Fax.adderror('Please Select the Rating Value');

        }
        
    }

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

Thanks!!
 

All Answers

Vineela ProddaturuVineela Proddaturu
Hi, Bachu sai Kiran
Please,try like this.
trigger AccountTrigger01 on Account (before insert) {
    if(Trigger.isBefore && Trigger.isInsert){
        for(Account acc : Trigger.new){
            if(acc.Phone == Null && (acc.Rating == Null && acc.Fax == Null)){
                acc.Phone.addError('Please, enter phone number');
                acc.Rating.addError('Please,select the rating value');
                acc.Fax.addError('Please,enter fax number');
                
            }
        }   
    }
}    
 If, it works please mark it is best answer.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sai Kiran,

You can do this by using validation rule.
 
OR(ISBLANK(Phone),ISBLANK(TEXT(Rating)),ISBLANK(Fax))

If this helps, Please mark it as best answer.

Thanks!!

 
rameshramesh
I want write same code in class. How to writte
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sai,

try with below code.

trigger:
trigger validation on Account (before insert) {
    
    If(trigger.Isbefore && trigger.Isinsert){
        TriggerhelperClass.PhoneFaxRatingValidation(trigger.new);
    }

}
Class:
public class TriggerhelperClass {
    
    public static void PhoneFaxRatingValidation(List<account> acclist){
        
        for(account acc:acclist) {
            
            if(acc.Rating == Null || acc.Rating == ''|| acc.Fax == Null ||acc.Fax ==''||acc.phone == Null||acc.phone==''){
                acc.Rating.adderror('Please Select the Rating Value');
                acc.Phone.adderror('Please Select the Rating Value');
                acc.Fax.adderror('Please Select the Rating Value');

        }
        
    }

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

Thanks!!
 
This was selected as the best answer
rameshramesh

Ankaiah thanks got it
rameshramesh
look this problem also
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000Ue6jQAC