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
EzmoEzmo 

Trigger & Test deployment issue

Morning,

 

I've been having a bit of an issue when trying to deploy a trigger and it's test from a sandbox to production. I've pasted the error and code below, any help or ideas would be much appreciated as this has got me stumped.

 

Failure Message: "System.QueryException: List has no rows for assignment to SObject", Failure Stack Trace: "Class.TestTriggers.myUnitTest: line 13, column 13 External entry point"

 

trigger stockControl on Transfer__c (after insert) {
    
    for (Transfer__c t : Trigger.new){
        if (t.Transfer_Type__c == 'inbound'){
            
            Decimal existingQuantity = 0;
            
            Stock_Level__c s = new Stock_Level__c();
            try{
            s = [SELECT Quantity__c 
                                FROM Stock_Level__c 
                                WHERE   Storage_Area__c = :t.Storage_Area__c AND 
                                        Resource__c = :t.Resource__c];
                                        
            existingQuantity = s.Quantity__c;                           
                      
            }
            catch(exception e){           
            }
            s.Resource__c = t.Resource__c;
            s.Storage_Area__c = t.Storage_Area__c;  
            s.Quantity__c = t.Quantity__c + existingQuantity;
            upsert s;
        }
    }
}

 

 

@isTest
private class TestTriggers {

    static testMethod void myUnitTest() {
        
        Transfer__c t = new Transfer__c();
        t.Quantity__c = 100000;
        t.Transfer_type__c = 'inbound'; 

        insert t; 
        
        Stock_Level__c s = new Stock_Level__c();
        s = [SELECT id FROM Stock_Level__c WHERE Quantity__c = 100000];
        
        id i = s.id;
        
        insert t;
        
        s = [SELECT id FROM Stock_Level__c WHERE id = :i];
        
        
        System.assert(s.Quantity__c == 200000);
        
    }
}

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

Hi,

 

There are few mistakes in your trigger and test class. I will explain them below.

 

  1. Don't write SOQL queries in for loops. I change your trigger for that issue. It will explain for this issue.
trigger stockControl on Transfer__c (after insert) {
	
	Set<Id> resourceId = new Set<Id>();
	Set<Id> storageAreaId = new Set<Id>();
	
	Stock_Level__c stockLevel = new Stock_Level__c();
	for (Transfer__c t : Trigger.new)
	{
		resourceId.add(t.Resource__c);
		storageAreaId.add(t.Storage_Area__c);
	}
    
	stockLevel = [SELECT Quantity__c FROM Stock_Level__c WHERE   Storage_Area__c in: storageAreaId AND Resource__c in: resourceId];
    
	for (Transfer__c t : Trigger.new)
	{	
		if (t.Transfer_Type__c == 'inbound')
		{
			for (Stock_Level__c tempSL : stockLevel)
			{   
				if(tempSL.Resource__c == t.Resource__c and tempSL.Storage_Area__c == t.Storage_Area__c)
				{
					Decimal existingQuantity = 0;
				            
					try{
                                                    
						existingQuantity = tempSL.Quantity__c;   

						tempSL.Resource__c = t.Resource__c;
						tempSL.Storage_Area__c = t.Storage_Area__c;  
						tempSL.Quantity__c = t.Quantity__c + existingQuantity;
						upsert tempSL;
                      
					}catch(exception e)
					{    

					}
				}				
            	
					
				
			}
		}
    }
}

 2.  Don't use existing data records in test class. You have fetch some data from Stick_Level__c object. That is wrong.

 

Here is the guidelines for write your test class

 

 

  1. Insert Resource__c record
  2. insert Storage_Area__c record
  3. insert Stock_Level__c records with Resource__c id and Storage_Area__c id which insert step2 and step3 (with other fields of Stock_Level__c)
  4. Then insert aTransfer__c object with Resource__c id and Storage_Area__c id which insert step2 and step3 (with other fields of Transfer__c)
  5. Add assert statement according to your requirment

 

@isTest
private class TestTriggers {

    static testMethod void myUnitTest() {
        
        Transfer__c t = new Transfer__c();
		Stock_Level__c s = new Stock_Level__c();
		Storage_Area__c sa = new Storage_Area__c();
		Resource__c r= new Resource__c();
		
        //insert Storage_Area__c record
		
		//insert Resource__c record
		
		s.Quantity__c = 100000;
		s.Resource__c = r.Id;
		s.Storage_Area__c = sa.Id;
		insert s; 
        
        t.Quantity__c = 100000;
        t.Transfer_type__c = 'inbound'; 
		t.Resource__c = r.Id;
		t.Storage_Area__c = sa.Id;
        insert t; 
		
        System.assert(s.Quantity__c == 200000);
        
    }
}

 NOTE : These codes for get the idea about your issues. Sometime it will provide errors becuase I'm not aware about your domain

 

Hope this will help you to resolve your issue.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog