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
Kabb12Kabb12 

Check for a field value from within a available list

Hi,

Just wondering if the trigger below is written properly? this trigger is looking for a newly entered BillingPostalCode value and then check this value against a given list of values if it matches it then updates a custom field Region name by code.

As you can see this trigger only works when a new record is created I would like to update regionName value for existing records too..... I would really help if someone can help, thanks

trigger regionName on Account (before insert) {
set<string> a=new Set<string>{'3125','3352','3014'};
for(Account myAccount: Trigger.new){
     if(myAccount.BillingPostalCode !=NULL){
         String s = myAccount.BillingPostalCode;
                if(a.contains(s)){
               
                string errMsg = 'This area belong to hume!';
                myAccount.regionName__c = 'ABC';
        }
   
     }
  }
}
Best Answer chosen by Kabb12
SFDC_DevloperSFDC_Devloper
Hi,

trigger regionName on Account (before insert,before update) {
set<string> a=new Set<string>{'3125','3352','3014'};
for(Account myAccount: Trigger.new){
     if(myAccount.BillingPostalCode !=NULL){
         String s = myAccount.BillingPostalCode;
                if(a.contains(s)){
                 myAccount.regionName__c = 'ABC';
                myAccount.addError('This area belong to hume!');
    }
    else
    {
    myAccount.addError('This area not belong to hume!');
    }
        }
     }
  
  }

Note:I am Removing the update DML statement at the end. By using the "before" trigger to modify the Trigger.new objects, you don't need to explicitly perform an update DML statement. When the trigger ends, it will implicitly update the data as you have modified the values.

All Answers

SFDC_DevloperSFDC_Devloper
Hi,

  Try below code...


trigger regionName on Account (before insert) {
set<string> a=new Set<string>{'3125','3352','3014'};
for(Account myAccount: Trigger.new){
     if(myAccount.BillingPostalCode !=NULL){
         String s = myAccount.BillingPostalCode;
                if(a.contains(s)){
                 myAccount.regionName__c = 'ABC';
                myAccount.addError('This area belong to hume!');
    }
    else
    {
    myAccount.addError('This area not belong to hume!');
    }
        }
  
     }
    update myAccount;
  }


Thanks,
Rockzz
Kabb12Kabb12
Error: Compile Error: Variable does not exist: myAccount at line 17 column 12 

getting this error message 

Also, is this going to update the existing records as well??

Thanks
SFDC_DevloperSFDC_Devloper
Hi,

  Try below code...


trigger regionName on Account (before insert,before update) {
set<string> a=new Set<string>{'3125','3352','3014'};
for(Account myAccount: Trigger.new){
     if(myAccount.BillingPostalCode !=NULL){
         String s = myAccount.BillingPostalCode;
                if(a.contains(s)){
                 myAccount.regionName__c = 'ABC';
                myAccount.addError('This area belong to hume!');
    }
    else
    {
    myAccount.addError('This area not belong to hume!');
    }
        }
update myAccount;
 
     }
   
  }


Thanks,
Rockzz
Kabb12Kabb12
Apex trigger regionName2 caused an unexpected exception, contact your administrator: regionName2: execution of BeforeInsert caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.regionName2: line 15, column 1

getting this error message now!!
Kabb12Kabb12
when I try to save the record

SFDC_DevloperSFDC_Devloper
Hi,

trigger regionName on Account (before insert,before update) {
set<string> a=new Set<string>{'3125','3352','3014'};
for(Account myAccount: Trigger.new){
     if(myAccount.BillingPostalCode !=NULL){
         String s = myAccount.BillingPostalCode;
                if(a.contains(s)){
                 myAccount.regionName__c = 'ABC';
                myAccount.addError('This area belong to hume!');
    }
    else
    {
    myAccount.addError('This area not belong to hume!');
    }
        }
     }
  
  }

Note:I am Removing the update DML statement at the end. By using the "before" trigger to modify the Trigger.new objects, you don't need to explicitly perform an update DML statement. When the trigger ends, it will implicitly update the data as you have modified the values.
This was selected as the best answer
Kabb12Kabb12
thanks you mate it works in this scenario

1. when i add a new record
2. when i update an existing records.

but tell me please what should I do when I need to update all the existing records when I just refresh the page or open any existing record?

I just want to make sure all the existing records automatially update this new field based on the Postalcode value.

I would really appreciate you help.

Thanks again
Vinit_KumarVinit_Kumar
There are couple of ways of updating the existing records :-

1.) Create a dummy field(may  be a checkbox ,you don't need to show it on layout) and update it to true for all records usning Data Loader which would cause the Trigger to fire and all records would get updated as per Trigger.

2.) Create a batch class as suggested in previous post ans schedule it to run ata a particular time and that would also cause the Trigger to fire.
SFDC_DevloperSFDC_Devloper
Hi,

I hope the below links will help you.....

     http://training.handsonconnect.org/m/data/l/32566-getting-started-with-the-apex-data-loader

    http://stackoverflow.com/questions/10997613/fire-a-trigger-on-existing-data

If this solves your problem, kindly mark it as the best answer.

Thanks,
Rockzz
Kabb12Kabb12
Thanks alot Rcokzz for the help!!
Kabb12Kabb12
Also, thank you Vinit_kumar 
Kabb12Kabb12
Please help me with this modification , there is no error but it not generating right results ......

trigger regionName2 on Account (before insert,before update) {
set<string> a=new Set<string>{'3000','3001','3002'};
set<string> b=new Set<string>{'3111','3222','3333'};
set<string> c=new Set<string>{'3222','3333','3444'};

for(Account myAccount: Trigger.new){
     if(myAccount.BillingPostalCode !=NULL){
         String s = myAccount.BillingPostalCode;
                if(a.contains(s)){
                 myAccount.LGA__c = 'First Block';
                //myAccount.addError('This area belong to hume!');
                }
                if(b.contains(s)){
                 myAccount.LGA__c = 'Second Block';
                //myAccount.addError('This area belong to hume!');
                }
                if(c.contains(s)){
                 myAccount.LGA__c = 'Third Block';
                //myAccount.addError('This area belong to hume!');
                }
                else
                {
                    //myAccount.addError('This area not belong to hume!');
                    myAccount.LGA__c = '';
                }
        } // end main if
//update myAccount;

     }// end for
  
  }
Kabb12Kabb12
This works fine but still need advice if this code looks OK or this can be written in a better way?

rigger regionName2 on Account (before insert,before update) {
set<string> a=new Set<string>{'3000','3001','3002'};
set<string> b=new Set<string>{'3111','3222','3333'};
set<string> c=new Set<string>{'3555','3444','3777'};
set<string> d=new Set<string>{'3200','3300','3400'};

for(Account myAccount: Trigger.new){
     if(myAccount.BillingPostalCode !=NULL){
         String s = myAccount.BillingPostalCode;
                if(a.contains(s)){
                 myAccount.LGA__c = 'First Block';
                //myAccount.addError('This area belong to hume!');
                }
                else if(b.contains(s)){
                 myAccount.LGA__c = 'Second Block';
                //myAccount.addError('This area belong to hume!');
                }
                else if(c.contains(s)){
                 myAccount.LGA__c = 'Third Block';
                //myAccount.addError('This area belong to hume!');
                }
                else if(d.contains(s)){
                 myAccount.LGA__c = 'Fouth Block';
                //myAccount.addError('This area belong to hume!');
                }

                else
                {
                    //myAccount.addError('This area not belong to hume!');
                    myAccount.LGA__c = 't';
                }
        } // end main if
//update myAccount;

     }// end for
  
  }
Kabb12Kabb12
I want to name give a region name to a post code that belong a certain group of post codes 

Am I doing the right thing by creating list of codes as blocks and then trying to match a value enter by user?

I would really appreciate if someone can guide me to help resolve this issue . I can see the above solution is working for me but any other suggestion from you guys will be great.


Kabb12Kabb12
Test is only giving 56% coverage what else can I do with this test class to get atleast 75% coverage

@isTest
public class TestRegionName {
    static testMethod void testRegionName(){
     // Let's create our records from scratch!
       Account acc   = new Account();
       acc.Name = 'Testing Region';
       acc.BillingPostalCode = '3000';
       acc.LGA__c = '';
     insert acc;
      
      
    }
}
Kabb12Kabb12
HMmmmmmm looopppp .... the code below is now giving me 75% coverage still any suggestions tips for Test methods are wellcome

am new to apex development but with your help guys I will learn more and more thanks in advance..................

@isTest
public class TestRegionName {
    static testMethod void testRegionName(){
     // Let's create our records from scratch!
     List<Account> accounts = new List<Account>{};
    for(Integer i = 0; i < 4; i++){
        Account a = new Account(Name = 'Test Account ',BillingPostalCode = '3009');
        //Account a = new Account(Name = '300' + i);
       
        accounts.add(a);
        }

      // Account acc   = new Account();
       //acc.Name = 'Testing Region';
       //acc.BillingPostalCode = '3000';
       //acc.LGA__c = '';
     insert accounts;
      
      
    }
}