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
CoffeeMugCoffeeMug 

Map.get() returning Null

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

nandurinanduri

for (Contact c : Trigger.new){ 

if(AccountMap.containsKey(c.k_id__c)){

 Account temp = AccountMap.get(c.k_id__c);  

 c.AccountId = temp.Id; 

}   

}

 

Also fo the Select Stm "List<Account> AllAccounts = [Select Id, k_id__c from Account];" please refer to governor limits, if the size of the list exceeds heap size the code may break.

CoffeeMugCoffeeMug

Problem still exists, it just doesnt like to work outside of the Salesforce IDE which is weird. When I run my test and add a ton of debug lines everything prints out and shows that it is working properly but once I attempt to do it manually via the browser or API it just doesn't work.

ritika@developerforceritika@developerforce

Hi,

 

It seems you are unneccessarily querying all the accounts, even though you need only the ones matching with the field in Contact

 

I'd suggest that first create the set of k_id__c values from Contact and use that as a filter to fetch accounts

 

Set<Decimal> dec = new Set<Decimal>();

Map<Decimal, Account> AccountMap = new Map<Decimal, Account>();

for(Contact c : trigger.new){

    dec.add(c.k_id__c);

}

 

List<Account> AllAccounts = [select Id, k_id__c from Account where k_id__c in: dec];

for(Account a : AllAccounts){

   AccountMap.put(a.k_id__c, a);

}

 

This will ensure that givernor limits are not crossed.

CoffeeMugCoffeeMug

Hi,

 

I have implemented the changes you guys suggested but the problem still occurs. The trigger runs fine when running my tests, however it doesn't seem to work outside of those test.

 

 

Rafael FerrerRafael Ferrer

I have the same problem, using a variable Map<Decimal,Account>. It works fine in the tests, but not in the application. If I change the variable for type Map<String,Account> the error dissapear... Crazy!