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
fdwebmasterfdwebmaster 

SOQL Query With Limit variable not working

I have an apex page that posts a quantity variable that a user inputs to a class file.  It works fine when the user uses it, but when running the test functions it fails.  If I take the limit variable out on the SOQL statement though the test will pass.

 

If I hard code the quantity variable the test will pass as well, but if I leave the qantity variable as is, the test fails.  This is the only piece I can't get to pass so I can  push to production.  The quantity needs to come from the apex page as it pulls the necessary amount of records based upon the value from that quantity field.

 

 

public Integer quantity { get; set; }
public Integer licenseType {get; set;}

Integer previewCount;
Integer qty = 1;

public List<License_Inventory__c> LicenseInventoryItem {get; set; }  
public List<License_Inventory__c> Licenses {get; set;}

public License_Inventory__c generatePreview()
{
    previewCount = quantity > 15 ? 15 : quantity;
Licenses =  [SELECT Id, License__c, License_Type__c, Status__c FROM License_Inventory__c WHERE Status__c = 'Inventory' AND License_Type__c = :licenseType ORDER BY License__c ASC LIMIT :previewCount];   
		
    return null;
}

public  License_inventory__c getLicenses()
{ 
     sequential = 0;
     string status = 'Inventory';
		
     qty = quantity > 1 ? 1 : quantity;
		
     List<License_Inventory__c> LicenseInventoryItem = [SELECT Id, License__c, License_Type__c, Status__c, Product__c, Product_Code__c, Version__c, Total_Installations_Allowed__c FROM License_Inventory__c WHERE Status__c = :status AND License_Type__c = :licenseType ORDER BY License__c ASC LIMIT :qty];   
		
     inventories = new License_Inventory__c[0];
     for (License_Inventory__c l : LicenseInventoryItem) 
     {
       //additional code
     }
}

 
    @isTest(SeeAllData = True)
    public static void getLicensesTest()
    {
        License_Inventory__c ll = new License_Inventory__c();
        NumberPeelerController controller = new NumberPeelerController();

        integer sequential = 0;
        integer quantity;
        quantity = 10;
        Integer qty;
        qty = quantity;
        string status = 'Inventory';
        
        ll.License__c = '000-000-000-000';
        insert ll;
        
        //Run Licenses Test        
        List<License_Inventory__c> sl = [SELECT Id, License__c    FROM License_Inventory__c WHERE Status__c = :status AND License_Type__c = 'Full' LIMIT :qty]; 
        
        System.assertEquals(10, sl.size());
    }	

@isTest(SeeAllData = True)
	public static void getLicensesTest()
	{
		License_Inventory__c ll = new License_Inventory__c();
		NumberPeelerController controller = new NumberPeelerController();

		integer sequential = 0;
		integer quantity;
		quantity = 10;
		Integer qty;
		qty = quantity;
		string status = 'Inventory';
		
		ll.License__c = '000-000-000-000';
		insert ll;
		
		//Run Licenses Test		
		List<License_Inventory__c> sl = [SELECT Id, License__c	FROM License_Inventory__c WHERE Status__c = :status AND License_Type__c = 'Full' LIMIT :qty]; 
		
		System.assertEquals(10, sl.size());
	}

 

Any help would be appreciated as I'm stuck on this last portion.

 

Thanks

 

fdwebmasterfdwebmaster

Forgot to add this is the error that is being received:

System.NullPointerException: Attempt to de-reference a null object.

souvik9086souvik9086

public List<License_Inventory__c> LicenseInventoryItem = new public List<License_Inventory__c>();
public List<License_Inventory__c> Licenses = new public List<License_Inventory__c>();

 

Just initialize those with new and dont reinitialize the LicenseInventoryItem variable again while querying.

List<License_Inventory__c> LicenseInventoryItem = [SELECT Id, License__c, License_Type__c, Status__c, Product__c, Product_Code__c, Version__c, Total_Installations_Allowed__c FROM License_Inventory__c WHERE Status__c = :status AND License_Type__c = :licenseType ORDER BY License__c ASC LIMIT :qty]; 

Instead of this write this

LicenseInventoryItem = [SELECT Id, License__c, License_Type__c, Status__c, Product__c, Product_Code__c, Version__c, Total_Installations_Allowed__c FROM License_Inventory__c WHERE Status__c = :status AND License_Type__c = :licenseType ORDER BY License__c ASC LIMIT :qty]; 

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

 

 

fdwebmasterfdwebmaster

Thanks for the reply .

 

Adding:

public List<License_Inventory__c> LicenseInventoryItem = new public List<License_Inventory__c>();
public List<License_Inventory__c> Licenses = new public List<License_Inventory__c>();

 

Gives a Error: Compile Error: unexpected token: 'public'

 

I don't have an issue building the list, the issue is with the qty variable not being dynamically set.  I can take that variable out and it runs fine, but that limit is major component to this piece.

 

FAIL:

LicenseInventoryItem = [SELECT Id, License__c, License_Type__c, Status__c, Product__c, Product_Code__c, Version__c, Total_Installations_Allowed__c FROM License_Inventory__c WHERE Status__c = :status AND License_Type__c = :licenseType ORDER BY License__c ASC LIMIT :qty];  

 

DOESN'T FAIL

LicenseInventoryItem = [SELECT Id, License__c, License_Type__c, Status__c, Product__c, Product_Code__c, Version__c, Total_Installations_Allowed__c FROM License_Inventory__c WHERE Status__c = :status AND License_Type__c = :licenseType ORDER BY License__c ASC];  

souvik9086souvik9086

Hi,

 

Sorry for the overlook.

 

Write this

 

public List<License_Inventory__c> LicenseInventoryItem = new List<License_Inventory__c>();
public List<License_Inventory__c> Licenses = new List<License_Inventory__c>();

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

 

souvik9086souvik9086

Another point is that 

 

LicenseInventoryItem = [SELECT Id, License__c, License_Type__c, Status__c, Product__c, Product_Code__c, Version__c, Total_Installations_Allowed__c FROM License_Inventory__c WHERE Status__c = :status AND License_Type__c = :licenseType ORDER BY License__c ASC LIMIT :qty];  

 

Here check what the value is coming for qty as

qty = quantity > 1 ? 1 : quantity;

Here quantity if Null or zero then limit will become 0 or null and the error will come. So make sure that quantity doesn't become null or 0.

 

f this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

 

RockDeveloperRockDeveloper

Put this Conditioon

 

qty = quantity > 1 && quantity != Null  ? 1 : quantity;
souvik9086souvik9086

You can do like this

 

if(quantity != NULL && quantity != 0){

qty = quantity > 1 ? 1 : quantity;

}

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks