You need to sign in to do that
Don't have an account?
Kabb12
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';
}
}
}
}
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';
}
}
}
}
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
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
getting this error message
Also, is this going to update the existing records as well??
Thanks
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
getting this error message now!!
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.
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
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.
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
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
}
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
}
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.
@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;
}
}
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;
}
}