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
ilewi121ilewi121 

Trigger not populating set


I run the tests and my set and map both come back null. Can anyone spot what I'm missing here?

<pre>
trigger associatePMRtoMode on PMR__c (before insert, before update) {

    Set<ID> AccountIds = new Set<ID>();
    Map<ID,ID> ACC_PMR = new Map<ID,ID>();
   
    for (PMR__c PMR :Trigger.new){
        AccountIds.add(PMR.Opportunity__r.AccountId);
        ACC_PMR.put(PMR.Opportunity__r.AccountId, PMR.Id);
    }

    System.debug('AccountIds = ' + AccountIds);
    System.debug('ACC_PMR = ' + ACC_PMR);
}
</pre>

Best Answer chosen by ilewi121
ilewi121ilewi121
I found the solution here: http://salesforce.stackexchange.com/questions/1885/why-is-my-reference-field-returning-null-my-test-code

<code>
trigger associatePMRtoMode on PMR__c (before insert, before update) {

    Set<Id> OppSet = new Set<Id>();
    for(PMR__c PMR :Trigger.new){
        OppSet.add(PMR.Opportunity__c);
    }
System.debug('OppSet = ' + OppSet);
  
    Map<ID,ID> OPP_ACC = new Map<ID,ID>();
    Set<Id> AccSet = new Set<Id>();
    for(Opportunity OPP:[SELECT Id, AccountId from Opportunity where Id in :OppSet]){
        OPP_ACC.put(OPP.Id, OPP.AccountId);
  AccSet.add(OPP.AccountId);      
    }
System.debug('AccSet = ' + AccSet);
System.debug('OPP_ACC = ' + OPP_ACC);

    Map<ID,ID> ACC_PMR = new Map<ID,ID>();
    for(PMR__c PMR :Trigger.new){
        for(ID OPP :OPP_ACC.keyset()){
            if(OPP == PMR.Opportunity__c){
                ACC_PMR.put(OPP_ACC.get(OPP), PMR.Id);
            }
        }
    }
System.debug('ACC_PMR = ' + ACC_PMR);
}
</code>

All Answers

ShaTShaT
Hi ,

In test class you need to query the object, you cannot directly use the realted fileds.

Eg-
Account a = new Account();
a.name= 'test';
insert a;

Opportunity opp = new Opportunity();\
opp.name=testopp;
opp.accountid=a.id;
insert opp;


PMR__c pm= new PMR__c();
pm.Opportunity=opp.id

insert pm;

list<PMR__c> pmtList=[select id,Opportunity__r.AccountId from pmr  where id=pm.id];

insert pmtList;

Hope this helps..!!

Thanks
ShaT
ilewi121ilewi121
Thank you ShaT, but I'm not having trouble with the test query, I'm having trouble with the trigger query. When I run the test, whether via Apex or via live interface testing, the debug results come back null.
ilewi121ilewi121
Does anyone have any other suggestions as to why my debug log would say that this loop should come back null?
ilewi121ilewi121
I found the solution here: http://salesforce.stackexchange.com/questions/1885/why-is-my-reference-field-returning-null-my-test-code

<code>
trigger associatePMRtoMode on PMR__c (before insert, before update) {

    Set<Id> OppSet = new Set<Id>();
    for(PMR__c PMR :Trigger.new){
        OppSet.add(PMR.Opportunity__c);
    }
System.debug('OppSet = ' + OppSet);
  
    Map<ID,ID> OPP_ACC = new Map<ID,ID>();
    Set<Id> AccSet = new Set<Id>();
    for(Opportunity OPP:[SELECT Id, AccountId from Opportunity where Id in :OppSet]){
        OPP_ACC.put(OPP.Id, OPP.AccountId);
  AccSet.add(OPP.AccountId);      
    }
System.debug('AccSet = ' + AccSet);
System.debug('OPP_ACC = ' + OPP_ACC);

    Map<ID,ID> ACC_PMR = new Map<ID,ID>();
    for(PMR__c PMR :Trigger.new){
        for(ID OPP :OPP_ACC.keyset()){
            if(OPP == PMR.Opportunity__c){
                ACC_PMR.put(OPP_ACC.get(OPP), PMR.Id);
            }
        }
    }
System.debug('ACC_PMR = ' + ACC_PMR);
}
</code>
This was selected as the best answer