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
BenPBenP 

Debug log for test class

Below is a section of a test class that I'm modifying due to a trigger change.  It passes all tests in the sandbox, but when trying to deploy I get an error at the first assert stating it can't find the sample work order.  I'm thinking this is due to a record not being created earlier on, but I can't see the error in the debug log.  My question is how to set/use the debug log correctly so that it shows the error and I can fix it.

 

Thank you for any help.

 

static testMethod void TestUpdateProductCodeTrigger()
     {  
     	Product2 part = [Select Id, Name From Product2 where Family = 'Pumps' and Site__c = 'E' and IsActive = true and status__c = 'A' limit 1];                       
        System.debug('part is: ' +part.Name);
        
		              
        RecordType rtBusinessAcc1 = [SELECT Id FROM RecordType 
                                WHERE SObjectType = 'Account'
                                AND Name = 'Business Account'];
                                
        RecordType rtConsumerAcc1 = [SELECT Id FROM RecordType 
                                WHERE SObjectType = 'Account'
                                AND Name = 'Consumer Account'];                        
                                

        Account ac1 = new Account (Name='test test',Email__c = 'test@test.com', RecordTypeId = rtBusinessAcc1.Id, Phone='4081234567', Type = 'SLS');
        try{
        insert ac1;

        }
               catch (Exception e) {
                system.debug('business account insert error is '+e.getMessage());
                }
                
        Account ac = new Account (
        		FirstName = 'Test',
        		LastName = 'Name',
        		Phone = '9195551212',
        		PersonMailingStreet = '123 Test',
        		PersonMailingCity = 'sanford',
        		PersonMailingState = 'NC',
        		PersonMailingCountry = 'US',
        		Pump_Seal_Test_Site__c = False,
        		RecordTypeId = rtConsumerAcc1.Id
        		);
        try{
        insert ac;
        }
               catch (Exception e) {
                system.debug('person account insert error is '+e.getMessage());
                } 
        System.debug('The person account name is: ' +ac.Name);       
        Contact ct = [Select Id, FirstName, LastName, accountid From Contact Where accountid = : ac.Id ];
        System.debug('The contact name is: ' +ct.FirstName  +ct.LastName);
        
        Case cs1 = new Case (
                    AccountId = ac.Id, 
                    ContactId = ct.Id, 
                    Status = 'New',
                    Origin = 'Email',
                    Type = 'Service',
                    Description = 'Test',
                    Product_Number__c = Part.Id,
                    Product_Model__c = 'Pumps',
                    Problem_Code__c = 'AC01=test',
                    Installation_Date__c = System.today()- 30
                    );
        insert cs1;
		System.debug('The case number is: ' +cs1.CaseNumber);
		
        Case cs2 = new Case (
                    AccountId = ac.Id, 
                    ContactId = ct.Id, 
                    Status = 'New',
                    Origin = 'Email',
                    Type = 'Service',
                    Description = 'Test',
                    Product_Number__c = Part.Id,
                    Installation_Date__c = System.today()
                    );
        insert cs2;
		System.debug('The case number is: ' +cs2.CaseNumber);
		
        
		
        // Invoking the Method "SVMX_GetProductCode()"
        String strProdCodeSP1 = SVMX_ProductInfo.SVMX_GetProductCode(Part.Id);
        
        RecordType rt = [SELECT Id FROM RecordType 
                                WHERE SObjectType = 'SVMXC__Service_Order__c'
                                AND Name = 'Regular'];
        RecordType rt1 = [SELECT Id FROM RecordType 
                                WHERE SObjectType = 'SVMXC__Service_Order__c'
                                AND Name = 'Fast Track'];                        
        
        //Select warranty admin user
		        
        RecordType rType = [SELECT Id from RecordType where SObjectType = 'SVMXC__Service_Group__c' and Name = 'Setup'];
          
        User usr = [Select Id, Name from User Where Profile.Name = 'Warranty Administrator' and IsActive = true limit 1 ];
        
        System.debug('The user name is: ' +usr.Name);
        
          
		SVMXC__Service_Group__c sg = new SVMXC__Service_Group__c(Warranty_Station_Email__c = 'abc@xyz.com', 
																	SVMXC__Active__c = true,
																	Equipment__c = 'a,b',
																	QAD_Vend_Code__c = 'ZZ125487', 
																	QADCode__c = 'ZZ000000', 
																	PolicyExp__c = System.today()+ 7, 
																	Name = 'testsvg', 
																	Insurance_number__c = '4517744', 
																	AppDate__c = System.today(), Region__c = 'Arizona', SVMXC__Group_Type__c = 'Partner', Phone__c = '(919) 566-8899',
																	Address__c = '123 main', City__c = 'sanford', State__c = 'nc', Zip__c = '27330', Country_Code__c = 'US',
																	ABC__c = 'A', RecordTypeId = rType.Id, Sales_Rep__c = ac1.Id, Service_Rep__c = ac1.Id, 
																	Admin__c = usr.Id);
		try{
        insert sg;
        }
               catch (Exception e) {
                system.debug('service team insert error is '+e.getMessage());
                }
		//insert sg;
		System.debug('The team name is: ' +sg.Name);
        
		System.runAs(usr)//run as a warranty admin profile user
        {
        // Create WO with Product                 
        SVMXC__Service_Order__c  SampleWorkOrder1 = new SVMXC__Service_Order__c (
        RecordTypeId = rt.Id,
		SVMXC__Case__c = cs1.Id ,
        SVMXC__Company__c  = ac.Id , 
        SVMXC__Contact__c = ct.Id,
        Product__c = Part.Id,
        SVMXC__Service_Group__c = sg.Id,
		SVMXC__Priority__c = 'Medium',
		SVMXC__Order_Type__c = 'Field Service',
		Repair_Date__c = System.today(),
        SVMXC__Order_Status__c ='Processing Error',
        Reason_For_Denial__c = 'Test denial'
        );
         
        try{
        insert SampleWorkOrder1;
        }
               catch (Exception e) {
                system.debug('sample work order 1 insert error is '+e.getMessage());
                }     
        System.debug('The work order number is: ' +SampleWorkOrder1.Name);
        
        //assert that the processed date and processed by fields are set correctly on insert
        SVMXC__Service_Order__c wo = [Select Id, Processed_Date__c, Processed_By__c From SVMXC__Service_Order__c Where Id = :SampleWorkOrder1.Id];
        System.assertEquals(wo.Processed_Date__c, System.today());
        System.assertEquals(wo.Processed_By__c, UserInfo.getName());
        
                        
        SVMXC__Service_Order__c  SampleWorkOrder2 = [SELECT Id ,Product__c, SVMXC__Order_Status__c, Reason_For_Denial__c FROM SVMXC__Service_Order__c where Id= :SampleWorkOrder1.Id ];
        SampleWorkOrder2.Product__c = Part.Id;        
        SampleWorkOrder2.SVMXC__Service_Group__c =sg.Id;
        SampleWorkOrder2.SVMXC__Company__c  = ac.Id;
        SampleWorkOrder2.SVMXC__Order_Status__c = 'Warranty Denied';
        SampleWorkOrder2.Reason_For_Denial__c = 'Test denial';
        try{
        update SampleWorkOrder2;
        }
               catch (Exception e) {
                system.debug('svol insert error is '+e.getMessage());
                }
        //assert that the processed date and processed by fields are set correctly on update
        SVMXC__Service_Order__c wo2 = [Select Id, Processed_Date__c, Processed_By__c From SVMXC__Service_Order__c Where Id = :SampleWorkOrder2.Id];
        System.assertEquals(wo.Processed_Date__c, System.today());
        System.assertEquals(wo.Processed_By__c, UserInfo.getName()); 
        
        SampleWorkOrder2.Amount__c = 0;
        SampleWorkOrder2.SVMXC__Order_Status__c = 'Warranty Processed';
        
        try{
        	update SampleWorkOrder2;
        }
        catch (Exception e) {
                System.Assert(e.getMessage().contains ('A dollar amount is required'));
                }
        
                                
        SVMXC__Service_Order__c  SampleWorkOrder3 = new SVMXC__Service_Order__c (
        SVMXC__Case__c = cs2.Id ,
        SVMXC__Contact__c =ct.Id,
        Product__c = Part.Id,
        SVMXC__Company__c  = ac.Id,
        Product_Model__c = 'Automation',
        Installation_Date__c = System.today(),
        SVMXC__Order_Status__c = 'Open',
        SVMXC__Priority__c = 'Medium',
        SVMXC__Order_Type__c = 'Field Service',
        Street__c = '123 test street',
        City__c = 'test city',
        State__c = 'test state',
        CaseProductGroup__c = 'Filter',
        Case_Problem_Code__c = 'FP93: WS STOCKING',
        SVMXC__Problem_Description__c = 'test description',
        SVMXC__Service_Group__c =sg.Id,
        RecordTypeId = rt1.Id); 
        try{
        insert SampleWorkOrder3;
        }
               catch (Exception e) {
                system.debug('svol insert error is '+e.getMessage());
                }
        
        SVMXC__Service_Order__c  SampleWorkOrder4 =  [SELECT Id ,Product__c FROM SVMXC__Service_Order__c where Id= :SampleWorkOrder1.Id ];
        SampleWorkOrder2.Product__c = Part.Id;
        SampleWorkOrder2.SVMXC__Service_Group__c =sg.Id;
        SampleWorkOrder2.SVMXC__Company__c  = ac.Id;
        try{
        update SampleWorkOrder4;
        }
               catch (Exception e) {
                system.debug('svol insert error is '+e.getMessage());
                } 
        }                  
     }

 

Best Answer chosen by Admin (Salesforce Developers) 
Michael_TorchedloMichael_Torchedlo

First, crop1645 is right, it is a good idea to add his line to your catch statements.

 

Okay, so what is happening is the insert SampleWorkOrder1 is not working.  But another issue is that the full Apex debug log is only available when you are running a normal test, not when deploying a new change set.  Because you are attempting to deploy, you don't get to see the full apex debug log - you only see the first failure (query exception).  If you could see the full log it would show you more detail about what was going wrong.

 

I have had a similar problem like this once before, and you may have to use trial and error to fix it.  Carefully change individual lines of code and try to redeploy until you are able to find the exact source of the problem.  I suggest first, comment out (remove) the try/catch statments where you insert your SampleWorkOrder1.  Just do a plain insert statment, that way when it fails in deployment, you will get a full DML exception message that will tell you WHY it failed to insert.  That will tell you where to go next.  Once you've fixed the problem and get it to deploy, then you can switch your code back to the try/catch model for long-term.

 

 

If the SampleWorkOrder1 insert is not working, it could be because one of the fields on that object has an invalid value (or an invalid Id).  I think that if the problem were RecordType Id, Case id, Account Id, Contact Id, or Product ID, then your query exception would appear earlier on, and your code would not make it all the way to the SampleWorkOrder1.

 

RecordTypeId = rt.Id,
SVMXC__Case__c = cs1.Id ,
SVMXC__Company__c  = ac.Id , 
SVMXC__Contact__c = ct.Id,
Product__c = Part.Id,
SVMXC__Service_Group__c = sg.Id,
SVMXC__Priority__c = 'Medium',
SVMXC__Order_Type__c = 'Field Service',
Repair_Date__c = System.today(),
SVMXC__Order_Status__c ='Processing Error',
Reason_For_Denial__c = 'Test denial'

 

Therefore, I think the problem may be in at the Service Group.  If the Service Group object fails to insert, the code will not break because it is in a try/catch.  Then, when you try to create the SampleWorkOrder1 it will have an invalid ServiceGroup Id, and that will also not be able to insert. 

SVMXC__Service_Group__c sg = new SVMXC__Service_Group__c(Warranty_Station_Email__c = 'abc@xyz.com', 
 SVMXC__Active__c = true, Equipment__c = 'a,b',
 QAD_Vend_Code__c = 'ZZ125487',  QADCode__c = 'ZZ000000', 
 PolicyExp__c = System.today()+ 7,  Name = 'testsvg', 
 Insurance_number__c = '4517744',  AppDate__c = System.today(),
 Region__c = 'Arizona',  SVMXC__Group_Type__c = 'Partner',
 Phone__c = '(919) 566-8899', Address__c = '123 main',
 City__c = 'sanford', State__c = 'nc', Zip__c = '27330',
 Country_Code__c = 'US', ABC__c = 'A', RecordTypeId = rType.Id,
 Sales_Rep__c = ac1.Id, Service_Rep__c = ac1.Id, 
 Admin__c = usr.Id);

try{
   insert sg;
}
catch (Exception e) {
   system.debug('service team insert error is '+e.getMessage());
}

If you look carefully at those fields and can find nothing that would cause an error in your production environment (check your validation rules!), then it could be "Sales_Rep__c = ac1.Id, Service_Rep__c = ac1.Id,"  

 

ac1 is a new account that you have created, and then you never check it before creating the service group.  It's possible that if that account never successfully inserted, then there is no valid id to use in your service group.  Also, given your field names, I'm not sure I understand why an "Account" would be used as a "Sales Rep" or "Service Rep" (which I think of as individuals.  Are you sure that the Sales_Rep__c relationship field on your Service Group is linked to an Account type, and not to some other sort of record type?

 

 Account ac1 = new Account (Name='test test',Email__c = 'test@test.com', RecordTypeId = rtBusinessAcc1.Id, Phone='4081234567', Type = 'SLS');
        try{
        insert ac1;
        }

 

All Answers

crop1645crop1645

BenP

 

You have several settings for the log - one of which is Database that logs all SOQL and DML operations. Any failed DML operation will be identified at log level INFO

 

Something that runs in sandbox but not PROD is indicative of one or more of the following

 

  • The testmethod depends on data in the org rather than exclusively on testmethod-generated data
  • Your sandbox is out of date with your PROD environment - for example, you have added validation rules or workflows in PROD that aren't present in your sandbox
  • Your sandbox doesn't have the same APEX classes or trigger present in PROD, or, vice-versa, you need to deploy additional resources into PROD in order to get your new trigger to function in PROD
Michael_TorchedloMichael_Torchedlo

It would be easier to zero in on the problem if you indicated the stack trace (which line) for the error.  What you describe sounds like a query exception.  If it were a DML exception, I believe you sould already see that in your debug log, since your catch statments have system.debug() calls outputting the error.

 

It looks like you are using try/catch statements whenever you insert your records, but then your code immediately continues assuming that the insert worked.  For example, if the SampleWorkOrder1 is not inserted, then there is no Id to find in the query.

 

Catching a DML exception does not fix the problem unless you also add logic that changes something to allow the rest of your code to execute properly. 

 

Maybe one of your inserts that worked in the test environment isn't working in production.  It could be because a required field is missing, or a validation rule, but because the insert is in a try/catch, that error does not cause the test to fail.  The failure happens after the try/catch, when your code tries to use the object which never got created.

 

 

try{
   insert SampleWorkOrder1;
}
catch (Exception e) {
   system.debug('sample work order 1 insert error is '+e.getMessage());
}     
System.debug('The work order number is: ' +SampleWorkOrder1.Name);
        
//assert that the processed date and processed by fields are set correctly on insert
SVMXC__Service_Order__c wo = [Select Id, Processed_Date__c, Processed_By__c From SVMXC__Service_Order__c Where Id = :SampleWorkOrder1.Id];

 

BenPBenP

Thank you both for your feedback.  I'm sorry, I should have listed the error.

 

System.QueryException: List has no rows for assignment to SObject

 

 

Michael, you're correct it's a query exception just before that assert statement.  I'm trying to catch the error before the query to see what's going wrong.  Is there a better way to do this?  I'm certainly willing to learn from this.

 

Yeah, it all works in the sandbox, but not in production.  I'm just not sure which recent changed hosed this. 

crop1645crop1645

BenP

 

If you get an exception that your testmethod can't recover from , then the catch block should do a System.assert(false,yourMessage);

 

This will stop the testmethod and give you an opportunity to see the error (use getMessage() method on the exception variable) as well as a chance to echo back any other variables. 

Michael_TorchedloMichael_Torchedlo

First, crop1645 is right, it is a good idea to add his line to your catch statements.

 

Okay, so what is happening is the insert SampleWorkOrder1 is not working.  But another issue is that the full Apex debug log is only available when you are running a normal test, not when deploying a new change set.  Because you are attempting to deploy, you don't get to see the full apex debug log - you only see the first failure (query exception).  If you could see the full log it would show you more detail about what was going wrong.

 

I have had a similar problem like this once before, and you may have to use trial and error to fix it.  Carefully change individual lines of code and try to redeploy until you are able to find the exact source of the problem.  I suggest first, comment out (remove) the try/catch statments where you insert your SampleWorkOrder1.  Just do a plain insert statment, that way when it fails in deployment, you will get a full DML exception message that will tell you WHY it failed to insert.  That will tell you where to go next.  Once you've fixed the problem and get it to deploy, then you can switch your code back to the try/catch model for long-term.

 

 

If the SampleWorkOrder1 insert is not working, it could be because one of the fields on that object has an invalid value (or an invalid Id).  I think that if the problem were RecordType Id, Case id, Account Id, Contact Id, or Product ID, then your query exception would appear earlier on, and your code would not make it all the way to the SampleWorkOrder1.

 

RecordTypeId = rt.Id,
SVMXC__Case__c = cs1.Id ,
SVMXC__Company__c  = ac.Id , 
SVMXC__Contact__c = ct.Id,
Product__c = Part.Id,
SVMXC__Service_Group__c = sg.Id,
SVMXC__Priority__c = 'Medium',
SVMXC__Order_Type__c = 'Field Service',
Repair_Date__c = System.today(),
SVMXC__Order_Status__c ='Processing Error',
Reason_For_Denial__c = 'Test denial'

 

Therefore, I think the problem may be in at the Service Group.  If the Service Group object fails to insert, the code will not break because it is in a try/catch.  Then, when you try to create the SampleWorkOrder1 it will have an invalid ServiceGroup Id, and that will also not be able to insert. 

SVMXC__Service_Group__c sg = new SVMXC__Service_Group__c(Warranty_Station_Email__c = 'abc@xyz.com', 
 SVMXC__Active__c = true, Equipment__c = 'a,b',
 QAD_Vend_Code__c = 'ZZ125487',  QADCode__c = 'ZZ000000', 
 PolicyExp__c = System.today()+ 7,  Name = 'testsvg', 
 Insurance_number__c = '4517744',  AppDate__c = System.today(),
 Region__c = 'Arizona',  SVMXC__Group_Type__c = 'Partner',
 Phone__c = '(919) 566-8899', Address__c = '123 main',
 City__c = 'sanford', State__c = 'nc', Zip__c = '27330',
 Country_Code__c = 'US', ABC__c = 'A', RecordTypeId = rType.Id,
 Sales_Rep__c = ac1.Id, Service_Rep__c = ac1.Id, 
 Admin__c = usr.Id);

try{
   insert sg;
}
catch (Exception e) {
   system.debug('service team insert error is '+e.getMessage());
}

If you look carefully at those fields and can find nothing that would cause an error in your production environment (check your validation rules!), then it could be "Sales_Rep__c = ac1.Id, Service_Rep__c = ac1.Id,"  

 

ac1 is a new account that you have created, and then you never check it before creating the service group.  It's possible that if that account never successfully inserted, then there is no valid id to use in your service group.  Also, given your field names, I'm not sure I understand why an "Account" would be used as a "Sales Rep" or "Service Rep" (which I think of as individuals.  Are you sure that the Sales_Rep__c relationship field on your Service Group is linked to an Account type, and not to some other sort of record type?

 

 Account ac1 = new Account (Name='test test',Email__c = 'test@test.com', RecordTypeId = rtBusinessAcc1.Id, Phone='4081234567', Type = 'SLS');
        try{
        insert ac1;
        }

 

This was selected as the best answer
BenPBenP

Thank you very much for all the information, this will help me a lot.

 

Adding the system.assert (false,e.getmessage()); allowed me to see the error, it was related to a validation rule when creating the work order.  I'm still working on it, but the service team is not in a state where it can be used.  I think I'll have to update a field or two to get a work flow to fire.

BenPBenP

Yep, I added a section to update a couple fields which worked.  We create the work order as one record type, then a work flow updates it to a useable one after certain fields are filled in.  I now create the record with 99% of the data required, then update it with the rest.  That should make the work flow fire, but at least it allows the work order creation.  I encourage any suggestions to improve any of this code.

 

New section added:

SVMXC__Service_Group__c  sg1 = [SELECT Id ,QADCode__c FROM SVMXC__Service_Group__c where Id= :sg.Id ];
        sg1.QADCode__c = 'ZZ000000';
        sg1.DO_NOT_USE__c = false;
        try{
        update sg1;
        }
               catch (Exception e) {
                system.debug('service team update error is '+e.getMessage());
                }

 

Michael_TorchedloMichael_Torchedlo

Are the test failures resolved, or are you still having a problem?

BenPBenP
All resolved