• JBernd
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi
As a newbie to Apex, I'm looking for pointers on how to best formulate a bulkified trigger.
The intent of the trigger is to take all trigger.new on the contact object where field1 OR field2 OR field3 OR field4 are empty. 
Then I want to match a set of the zip codes in the above selection of trigger.new contact records to a custom lookup object.
I want to match the zips in my set to zips in the custom lookup.
Then I want to update fields on the contact object with corresponding values for Field1, Field2, Field3, Field4, but only where Field1, Field2, Field3, Field4 are empty. (In other words, I don't want to overwrite a value that already exists in any of these fields on the contact object).

Question:
There could be times when >50k records will be loaded, but most of the time, it will be small numbers with the occasional list load.

How would you best bulkify this?  [Where would you make sets/maps - at what point would you update]

Thanks
  • January 24, 2014
  • Like
  • 0

Hi

I'm new to Apex and teaching myself. I've got a simple trigger that works in a sandbox, but to deploy it to production I need code coverage with a unit test.  Here's where the problem is.

 

This is where I got so far. (Trigger takes Billiing Zip off Account object - looks up a related object - finds County - returns to County__c field on Account object)

 

This is the working trigger

trigger conZipcodeAcc on Account (before insert, before update) {
    for(Account a : Trigger.new){
        List<ZipCounty_Chart_del__c> zcc = [select t.id,t.county__c,t.Postal_Code__c from ZipCounty_Chart_del__c t where t.Postal_Code__c =: a.BillingPostalCode ];
        if(!zcc.isEmpty()){
            for(ZipCounty_Chart_del__c t : zcc){
                a.County__c = String.valueOf(t.County__c);
            }
        }
    }
}

 

Here is where I've got with my unit test, but I get a compile error (Error: Compile Error: expecting right curly bracket, found 'EOF' at line 0 column -1).

 

@isTest
private class TestConzipcodeAcc{
static testMethod void TestConzipcodeAcc (){
//Set up the account record
List<Account> accounts = new List<Account>{};

For (Integer x=0; x<200;x++)
{
//This iterates and creates 200 Accounts with a postal code of 19001 (something known). 
Account a = new Account (Test= 'School' + x, BillingPostalCode = '19001');
accounts.add(a);
}


test.startTest();
   insert accounts;
test.stopTest();

 

 

For (Account a : accounts) {
System.AssertEquals (County__c = 'Montgomery');
}
}

 

  • September 11, 2013
  • Like
  • 0
Hi
As a newbie to Apex, I'm looking for pointers on how to best formulate a bulkified trigger.
The intent of the trigger is to take all trigger.new on the contact object where field1 OR field2 OR field3 OR field4 are empty. 
Then I want to match a set of the zip codes in the above selection of trigger.new contact records to a custom lookup object.
I want to match the zips in my set to zips in the custom lookup.
Then I want to update fields on the contact object with corresponding values for Field1, Field2, Field3, Field4, but only where Field1, Field2, Field3, Field4 are empty. (In other words, I don't want to overwrite a value that already exists in any of these fields on the contact object).

Question:
There could be times when >50k records will be loaded, but most of the time, it will be small numbers with the occasional list load.

How would you best bulkify this?  [Where would you make sets/maps - at what point would you update]

Thanks
  • January 24, 2014
  • Like
  • 0

Hi

I'm new to Apex and teaching myself. I've got a simple trigger that works in a sandbox, but to deploy it to production I need code coverage with a unit test.  Here's where the problem is.

 

This is where I got so far. (Trigger takes Billiing Zip off Account object - looks up a related object - finds County - returns to County__c field on Account object)

 

This is the working trigger

trigger conZipcodeAcc on Account (before insert, before update) {
    for(Account a : Trigger.new){
        List<ZipCounty_Chart_del__c> zcc = [select t.id,t.county__c,t.Postal_Code__c from ZipCounty_Chart_del__c t where t.Postal_Code__c =: a.BillingPostalCode ];
        if(!zcc.isEmpty()){
            for(ZipCounty_Chart_del__c t : zcc){
                a.County__c = String.valueOf(t.County__c);
            }
        }
    }
}

 

Here is where I've got with my unit test, but I get a compile error (Error: Compile Error: expecting right curly bracket, found 'EOF' at line 0 column -1).

 

@isTest
private class TestConzipcodeAcc{
static testMethod void TestConzipcodeAcc (){
//Set up the account record
List<Account> accounts = new List<Account>{};

For (Integer x=0; x<200;x++)
{
//This iterates and creates 200 Accounts with a postal code of 19001 (something known). 
Account a = new Account (Test= 'School' + x, BillingPostalCode = '19001');
accounts.add(a);
}


test.startTest();
   insert accounts;
test.stopTest();

 

 

For (Account a : accounts) {
System.AssertEquals (County__c = 'Montgomery');
}
}

 

  • September 11, 2013
  • Like
  • 0