+ Start a Discussion
Kevin RuskKevin Rusk 

New Trigger works under the GUI but Test Class will not pull data

I have created a trigger on the Opportunity Object.  When I test it under the GUI, the field gets populated properly.  Now I need to get the Test Class to run the trigger so that I can move it to Prod.

Here is the trigger:

trigger Referral_Name_autofill on Opportunity (before insert) {

    For (Opportunity n : Trigger.new)    { 
    
        IF (n.referrer_SFID__c != Null) {
        
            String found = '';        
            System.Debug('New SFID In; ' + n.referrer_SFID__c);
            System.Debug('New AccountID In; ' + n.AccountID);
            Contact[] HofH = [SELECT Id, Relationship__c, Name FROM Contact WHERE AccountID = :n.referrer_SFID__c];
        
            FOR(Contact h:HofH){
            
                IF (h.Relationship__c == 'Head of HH') {
                    
                    IF (found == '')    {
                        n.Referral_Name__c = h.Id;
                        found = 'FOUND';
                    }
                
                }
            
            }
            
            IF (found == '')    {
            
                Contact[] contacts = [SELECT Id, TDAI_WM__Relationship__c, Name FROM Contact 
                                        WHERE AccountID = :n.referrer_SFID__c
                                        ORDER BY CreatedDate DESC];
                                        
                FOR(Contact c:contacts){
                    
                    n.Referral_Name__c = c.Id;
                       BREAK;
                
                }
            
            }            
        
        }
    
    }

}

Here is the test class:

@isTest
private class Referral_Name_autofill_Testclass {
static testMethod void myUnitTest() {
System.debug('Here');
  Opportunity op1 = New Opportunity( AccountId='001e000000SobCeAAJ', Name='TestOpp', referrer_SFID__c='001e000000SobCe',
                                   StageName='Prospect Received', CloseDate=Date.today());
        // Insert Opportunity
        insert op1;
        System.debug('Referral Name after trigger fired: ' + op1.Referral_Name__c);
    
    Opportunity op2 = New Opportunity( AccountId='001e000000SZhdgAAD', Name='TestOpp', referrer_SFID__c='001e000000SZhdg',
                                   StageName='Prospect Received', CloseDate=Date.today());
        // Insert Opportunity
        insert op2;
        System.debug('Referral Name after trigger fired: ' + op2.Referral_Name__c);
    
       
    FOR(Contact n:[SELECT Id, AccountId FROM Contact LIMIT 1]){
        System.debug('Here we go');
        Opportunity op = New Opportunity( AccountId=n.AccountId, Name='TestOpp', referrer_SFID__c=n.Id,
                                   StageName='Prospect Received', CloseDate=Date.today() );
        // Insert Opportunity
        insert op;
        
        // Retrive Referal Name
        op = [SELECT Referral_Name__c FROM Opportunity WHERE Id =:op.Id];
        System.debug('Referral Name after trigger fired: ' + op.Referral_Name__c);
    }
}
}

According to the Test Execution Log, none of the SOQL Queries pull any rows.  When I run them individually in DC, I get the correct record I need.  I know it is something that I am not seeing.
Best Answer chosen by Kevin Rusk
StephenKennyStephenKenny
Hi Kevin,

The problem is this line: 'FOR(Contact n:[SELECT Id, AccountId FROM Contact LIMIT 1]){'
This query returns no records because you have not inserted any contacts in your test method, and as a result, none of the code within this block will execute.

To get this to work you should insert a contact above this line in your test method, something like this:
Contact c = new Contact(Lastname = 'test');
insert c;

By the way, it is not a good idea to hard code Ids into test methods. You should not assume that the data will always exist in your environment. for example, if the account with id'001e000000SZhdgAAD' is deleted, then these test methods will fail. Also, the ids can change between sandbox and production environments.

Hope this helps you but let me know if you have any questions.

Please remember to mark this thread as solved with the answer that best helps you.

Kind Regards
Stephen
 

All Answers

StephenKennyStephenKenny
Hi Kevin,

The problem is this line: 'FOR(Contact n:[SELECT Id, AccountId FROM Contact LIMIT 1]){'
This query returns no records because you have not inserted any contacts in your test method, and as a result, none of the code within this block will execute.

To get this to work you should insert a contact above this line in your test method, something like this:
Contact c = new Contact(Lastname = 'test');
insert c;

By the way, it is not a good idea to hard code Ids into test methods. You should not assume that the data will always exist in your environment. for example, if the account with id'001e000000SZhdgAAD' is deleted, then these test methods will fail. Also, the ids can change between sandbox and production environments.

Hope this helps you but let me know if you have any questions.

Please remember to mark this thread as solved with the answer that best helps you.

Kind Regards
Stephen
 
This was selected as the best answer
Kevin RuskKevin Rusk
Stephen,

Yes, that did the trick.  I do understand about hard coding the ID's, but I was trying several different methods to try and fix this.  I did not understand that the testing could not use the current data and had to create all the records, but now I know.