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
Rajat Bhatt 4Rajat Bhatt 4 

My Trigger is not working properly , please help

Hi,
I am new to salesforce and i am facing an issue my Trigger is not working properly.
I want the trigger to check whether the all Shipping Address field is filled or not , and if not the display the error message.
Any help will be appreciated.
Thanks.
trigger shippingAddress on Account (before insert,before update)
{

    List<Account> accList=new List<Account>();
    
    for(account accObject:trigger.new)
    {
        if(accObject.ShippingStreet !=null && accObject.ShippingCity!=null &&
           accObject.ShippingState!=null && accObject.ShippingPostalCode!=null && 
           accObject.ShippingCountry!=null)
        {
           accList.add(accObject);
        
        }
        
        else
        {
        
            accObject.addError('All Shipping Fields are required');
            
        }

    }
    
    insert accList;
}

 
Best Answer chosen by Rajat Bhatt 4
Ajay K DubediAjay K Dubedi
Hi Rajat,

Hi have done some small changes to your trigger. Hope this will work for you.

trigger shippingAddress on Account (after insert,after update)
{
    try{
    List<Account> accList=new List<Account>();
    
    for(account accObject:trigger.new)
    {
        if(accObject.ShippingStreet ==null || accObject.ShippingCity ==null ||
           accObject.ShippingState ==null || accObject.ShippingPostalCode==null || 
           accObject.ShippingCountry==null)
        {
           
        accObject.addError('All Shipping Fields are required');
        }
        
        else
        {
        
            accList.add(accObject);
            
        }

    }
    
    insert accList;
    }catch(Exception exp){
        system.debug('The following exception has occurred ' + exp.getMessage() + ' at line no ' + exp.getLineNumber());
    }
}

Thank you
Ajay Dubedi

All Answers

Raj VakatiRaj Vakati
Try this code
 
trigger shippingAddress on Account (before insert,before update)
{

.// List<Account> accList=new List<Account>();
    
    for(account accObject:trigger.new)
    {
        if(accObject.ShippingStreet ==null || accObject.ShippingCity==null ||
           accObject.ShippingState==null || accObject.ShippingPostalCode==null ||
           accObject.ShippingCountry==null)
        {
           
            accObject.addError('All Shipping Fields are required');
        }

    }
    
  //  insert accList;
}

 
Raj VakatiRaj Vakati
trigger shippingAddress on Account (before insert,before update)
{

    for(account accObject:trigger.new)
    {
        if(accObject.ShippingStreet ==null || accObject.ShippingCity==null ||
           accObject.ShippingState==null || accObject.ShippingPostalCode==null ||
           accObject.ShippingCountry==null)
        {
           
            accObject.addError('All Shipping Fields are required');
        }

    }

}

 
Ajay K DubediAjay K Dubedi
Hi Rajat,

Hi have done some small changes to your trigger. Hope this will work for you.

trigger shippingAddress on Account (after insert,after update)
{
    try{
    List<Account> accList=new List<Account>();
    
    for(account accObject:trigger.new)
    {
        if(accObject.ShippingStreet ==null || accObject.ShippingCity ==null ||
           accObject.ShippingState ==null || accObject.ShippingPostalCode==null || 
           accObject.ShippingCountry==null)
        {
           
        accObject.addError('All Shipping Fields are required');
        }
        
        else
        {
        
            accList.add(accObject);
            
        }

    }
    
    insert accList;
    }catch(Exception exp){
        system.debug('The following exception has occurred ' + exp.getMessage() + ' at line no ' + exp.getLineNumber());
    }
}

Thank you
Ajay Dubedi
This was selected as the best answer
Rajat Bhatt 4Rajat Bhatt 4
Thanks Ajay, it works, really appreciate your efforts.
Could you tell me what was wrong with my code as i could not understand the difference.
Thanks a lot again.