You need to sign in to do that
Don't have an account?
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>
<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
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
<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>