• CoffeeMug
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

I wrote two very similar triggers, one of them works the other one does not. The triggers simply attempt to populate the account lookup field in Contacts by using an external id added to each account. The first trigger simple loops through all the accounts attempting to find one with a matching external id but that seemed to be inefficient and would eat up a look for script statements. So I decided to create a second similar trigger that would use maps.

 

//this one works fine
trigger FindAccount on Contact (before insert, before update){
    List<Account> AllAccounts = [Select Id, k_id__c from Account];
    
    for(Contact c: Trigger.new){
        for (Account acct : AllAccounts){
            if (acct.k_id__c == c.k_id__c){
                c.AccountId = acct.Id;
                break;
            }   
        }
    }
}

##################################################################

//this one does not
trigger FindAccount on Contact (before insert, before update){
    List<Account> AllAccounts = [Select Id, k_id__c from Account];
    Map<Decimal, Account> AccountMap = new Map<Decimal, Account>();
    
    for(Account a : AllAccounts){
    	AccountMap.put(a.k_id__c,a);
    }     
    
    for (Contact c : Trigger.new){
    	Account temp = AccountMap.get(c.k_id__c);
        if (temp != null)
    	     c.AccountId = temp.Id;
    }

}

 The problem is that the second trigger passes the same tests that I had initially wrote, however it doesn't work when I add or modify a contact through the browser or api. When I do 'Execute Anonymous' via the Salesforce API it seems work. Does anyone know what the problem is?

 

Execute Anonymous Output

Anonymous execution was successful.

20.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: Contact test = new Contact (FirstName = 'test', LastName = 'test', k_id__c = 999, u_id__c = 90);
Execute Anonymous: Insert test;
Execute Anonymous:
Execute Anonymous: test = [select accountId from Contact where u_id__c = 90];
Execute Anonymous: system.debug ('test account lookup: ' + test.accountId);
15:56:58.033 (33252000)|EXECUTION_STARTED
15:56:58.033 (33295000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
15:56:58.034 (34677000)|DML_BEGIN|[2]|Op:Insert|Type:Contact|Rows:1
15:56:58.063 (63635000)|CODE_UNIT_STARTED|[EXTERNAL]|01qK0000000Cd7P|FindAccount on Contact trigger event BeforeInsert for [new]
15:56:58.063 (63928000)|SOQL_EXECUTE_BEGIN|[2]|Aggregations:0|Select Id, k_id__c from Account
15:56:58.072 (72120000)|SOQL_EXECUTE_END|[2]|Rows:2

15:56:58.073 (73294000)|CODE_UNIT_FINISHED|FindAccount on Contact trigger event BeforeInsert for [new]
15:56:58.122 (122561000)|DML_END|[2]
15:56:58.122 (122817000)|SOQL_EXECUTE_BEGIN|[4]|Aggregations:0|select accountId from Contact where u_id__c = 90
15:56:58.128 (128040000)|SOQL_EXECUTE_END|[4]|Rows:1
15:56:58.128 (128267000)|USER_DEBUG|[5]|DEBUG|test account lookup: 001K0000005yGyMIAU

I wrote two very similar triggers, one of them works the other one does not. The triggers simply attempt to populate the account lookup field in Contacts by using an external id added to each account. The first trigger simple loops through all the accounts attempting to find one with a matching external id but that seemed to be inefficient and would eat up a look for script statements. So I decided to create a second similar trigger that would use maps.

 

//this one works fine
trigger FindAccount on Contact (before insert, before update){
    List<Account> AllAccounts = [Select Id, k_id__c from Account];
    
    for(Contact c: Trigger.new){
        for (Account acct : AllAccounts){
            if (acct.k_id__c == c.k_id__c){
                c.AccountId = acct.Id;
                break;
            }   
        }
    }
}

##################################################################

//this one does not
trigger FindAccount on Contact (before insert, before update){
    List<Account> AllAccounts = [Select Id, k_id__c from Account];
    Map<Decimal, Account> AccountMap = new Map<Decimal, Account>();
    
    for(Account a : AllAccounts){
    	AccountMap.put(a.k_id__c,a);
    }     
    
    for (Contact c : Trigger.new){
    	Account temp = AccountMap.get(c.k_id__c);
        if (temp != null)
    	     c.AccountId = temp.Id;
    }

}

 The problem is that the second trigger passes the same tests that I had initially wrote, however it doesn't work when I add or modify a contact through the browser or api. When I do 'Execute Anonymous' via the Salesforce API it seems work. Does anyone know what the problem is?

 

Execute Anonymous Output

Anonymous execution was successful.

20.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: Contact test = new Contact (FirstName = 'test', LastName = 'test', k_id__c = 999, u_id__c = 90);
Execute Anonymous: Insert test;
Execute Anonymous:
Execute Anonymous: test = [select accountId from Contact where u_id__c = 90];
Execute Anonymous: system.debug ('test account lookup: ' + test.accountId);
15:56:58.033 (33252000)|EXECUTION_STARTED
15:56:58.033 (33295000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
15:56:58.034 (34677000)|DML_BEGIN|[2]|Op:Insert|Type:Contact|Rows:1
15:56:58.063 (63635000)|CODE_UNIT_STARTED|[EXTERNAL]|01qK0000000Cd7P|FindAccount on Contact trigger event BeforeInsert for [new]
15:56:58.063 (63928000)|SOQL_EXECUTE_BEGIN|[2]|Aggregations:0|Select Id, k_id__c from Account
15:56:58.072 (72120000)|SOQL_EXECUTE_END|[2]|Rows:2

15:56:58.073 (73294000)|CODE_UNIT_FINISHED|FindAccount on Contact trigger event BeforeInsert for [new]
15:56:58.122 (122561000)|DML_END|[2]
15:56:58.122 (122817000)|SOQL_EXECUTE_BEGIN|[4]|Aggregations:0|select accountId from Contact where u_id__c = 90
15:56:58.128 (128040000)|SOQL_EXECUTE_END|[4]|Rows:1
15:56:58.128 (128267000)|USER_DEBUG|[5]|DEBUG|test account lookup: 001K0000005yGyMIAU