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
Cody Sanders 33Cody Sanders 33 

Error when using a second soql query in apex class

I'm trying to use two soql queries to grab the relevant information for an email class, but it is failing when it tries to run the second query when running the test class. I'm actively stumped as to why it's not finding the created product. I'm not super experienced and mostly cut/paste things for apex, so I'm sure there's something goofed along the way. Code below:
Class:

global class EmailPTRAttWearplates implements Messaging.InboundEmailHandler {
	  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
		  Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
          
          Production_Tracking_Record__c ptr;
          Product2 prod;
          
           ptr = 
              [select id, Product__c from Production_Tracking_Record__c
              where Name = :email.subject];
          
           prod =
              [select Wearplate__c, Wearplate_Thickness__c, Pre_Order_Wearplates__c from Product2
              where Name = :ptr.Product__c];
          
          if(prod.Pre_Order_Wearplates__c=false)
          {
           PTR_Attachment__c Att = new PTR_Attachment__c(Name=email.subject,
                                                         PTR__c=ptr.Id,
                                                         Component__c='Wearplate',
                                                         Quantity__c=2,
                                                         Thickness__c=prod.Wearplate_Thickness__c,
                                                         Material__c=prod.Wearplate__c,
                                                         Directory_Link_Text__c='R:\\Parts Orders\\Internal Laser Parts');
          insert Att;
          }
       return result;
      }
}



Test Class:

@IsTest
public class EmailPTRAttWearplates_Test {
 public static list<Production_Tracking_Record__c> PTRlist;
    static testMethod void myTest() {
   		Messaging.InboundEmail email  = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
    
    		email.fromAddress = 'test@test.com';
    		email.subject = '12345';
   
    Test.startTest();
    
        
        Account acct = new Account(Name='Test Account',
                                   Type='Direct Customer',
                                   Shipping__c='Prepaid',
                                   Ship_Via__c='UTS',
                                   Sales_Discount__c=0);
        insert acct;
        
        Product2 prod = new Product2(Name='Test Product',
                                     Rotor_Configuration__c='Closed Adjustable',
                                     Inboard_or_Outboard__c='Outboard',
                                     Endcap_Face_Preparation__c='Wearplate',
                                     Wearplate__c='Mild Steel',
                                     Wearplate_Thickness__c='.375',
                                     Pre_Order_Wearplates__c=false,
                                     Vane_Thickness__c='.375',
                                     Vane_Material__c='AR500',
                                     Shroud_Mat__c='Mild Steel',
                                     Shroud_Thickness__c='.375',
                                     Adjustable_Blade_Thickness__c='0.3125',
                                     Blade_Mat__c='Mild Steel');
        insert prod;
        
        Opportunity opp = new Opportunity(Name='12345',
                                          RA__c='12345',
                                          StageName='Quote Sent',
                                          CloseDate=System.today(),
                                          AccountId=acct.Id,
                                          Make__c='Kice',
                                          Model__c='VJ 14x10x10',
                                          Product__c=prod.Id,
                                          Max_Temp__c='120',
                                          Serial__c='N/A',
                                          Cold_Clr_Radial__c='.004-.007',
                                          Cold_Clr_Side__c='.004-.007');
        
        insert opp;
    		
        Production_Tracking_Record__c ptr = new Production_Tracking_Record__c(Name = '12345',
        																	  RA__c = opp.Id,
            																  Product__c = prod.Id);
        insert ptr; 
        
        EmailPTRAttWearplates paw = new EmailPTRAttWearplates();
        
        Messaging.InboundEmailResult result = paw.handleInboundEmail(email, env);
        
    Test.stopTest();
    
        System.assert (result.success, 'InboundEmailResult returned a failure message');
    
    }
}

 
Greg HGreg H
The query "select id, Product__c from Production_Tracking_Record__c where Name = :email.subject" could return more than one record. Or it may not return a record at all. You need to make sure you have one and only one record returned in that query before making the second query. Also, you should to make sure that the email being received by this class has a subject line before executing the first query. Something like this would be a better option:
if (String.isNotBlank(email.subject)) { //if the subject line of the email is not blank, null or empty ''
    ptr = [SELECT Id, Product__c FROM Production_Tracking_Record__c WHERE Name = :email.subject LIMIT 1]; //query for one matching record
}
if (ptr != null) { //if the ptr variable is not null
    prod = [SELECT Wearplate__c, Wearplate_Thickness__c, Pre_Order_Wearplates__c FROM Product2 WHERE Name = :ptr.Product__c LIMIT 1]; //query for a single product
}

I'm glad your learning how to code.
-greg