+ Start a Discussion
Nicholas PalattaoNicholas Palattao 

Can't enter SOQL for loop

I wrote a small trigger to populate a lookup field on the Opportunity object, and I can't seem to get my test class to enter the loop which contains the assignment of this field. The query appears to have all the necessary info, I'm not sure what's wrong.
Here is the trigger: 
 

trigger FillPrimaryPartner on Opportunity (before insert, before update) {
    for (Opportunity opp : trigger.new){
        for (Partner p : [select id from Partner where IsPrimary=true and AccountFromId =: opp.AccountId]){
            opp.Primary_Partner__c = p.Id; 
        }
    }
}
and here is its test class:
@isTest
public class TESTFillPrimaryPartner {
	static testMethod void insertNewData(){
        
        Account partAcct = new Account();
        partAcct.Type = 'Signed Partner';
        partAcct.Industry = 'Retail';
        partAcct.BillingCountry = 'Italy';
        insert partAcct;
        
        Account oppAcct = new Account();
        oppAcct.Type = 'Customer';
        oppAcct.Industry = 'CPG';
        oppAcct.BillingCountry = 'Italy';
        insert oppAcct;
        
        Opportunity opp1 = new Opportunity();
        Opportunity.AccountId = oppAcct.Id;
        insert opp1;
        
        Partner part1 = new Partner();
        part1.IsPrimary = true;
        part1.AccountFromId = oppAcct.Id;
        part1.AccountToId = partAcct.Id;
        insert part1;
    }
}


 
Best Answer chosen by Nicholas Palattao
George AdamsGeorge Adams
Could it be because you're inserting the Partner after inserting the Opportunity (which is what fires your trigger)?

Try:
 
@isTest
public class TESTFillPrimaryPartner {
	static testMethod void insertNewData(){
        
        Account partAcct = new Account();
        partAcct.Type = 'Signed Partner';
        partAcct.Industry = 'Retail';
        partAcct.BillingCountry = 'Italy';
        insert partAcct;
        
        Account oppAcct = new Account();
        oppAcct.Type = 'Customer';
        oppAcct.Industry = 'CPG';
        oppAcct.BillingCountry = 'Italy';
        insert oppAcct;
        
        Partner part1 = new Partner();
        part1.IsPrimary = true;
        part1.AccountFromId = oppAcct.Id;
        part1.AccountToId = partAcct.Id;
        insert part1;

        Opportunity opp1 = new Opportunity();
        Opportunity.AccountId = oppAcct.Id;
        insert opp1;        
    }
}

 

All Answers

Nicholas PalattaoNicholas Palattao
edit: the loop in question that I'm unable to enter is on line 3 of my trigger
George AdamsGeorge Adams
Could it be because you're inserting the Partner after inserting the Opportunity (which is what fires your trigger)?

Try:
 
@isTest
public class TESTFillPrimaryPartner {
	static testMethod void insertNewData(){
        
        Account partAcct = new Account();
        partAcct.Type = 'Signed Partner';
        partAcct.Industry = 'Retail';
        partAcct.BillingCountry = 'Italy';
        insert partAcct;
        
        Account oppAcct = new Account();
        oppAcct.Type = 'Customer';
        oppAcct.Industry = 'CPG';
        oppAcct.BillingCountry = 'Italy';
        insert oppAcct;
        
        Partner part1 = new Partner();
        part1.IsPrimary = true;
        part1.AccountFromId = oppAcct.Id;
        part1.AccountToId = partAcct.Id;
        insert part1;

        Opportunity opp1 = new Opportunity();
        Opportunity.AccountId = oppAcct.Id;
        insert opp1;        
    }
}

 
This was selected as the best answer
Nicholas PalattaoNicholas Palattao
That was absolutely right, coverage looks good now. Thank you!