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
Laura GilLaura Gil 

Compiling failing: Variable doesn't exist in test class

Hi all,
I am trying to discover why while compiling a test class method, this is telling me 'Variable doesn't exist' in the row:
System.assertEquals(testEntitlement.Id



I would appreciate if someone could tell me what I am doing wrong?:
 
@isTest
    private static void EntitlementUpdate() {
    //ARRANGE
        User serviceUser = [
        SELECT
            Id
        FROM
            User
        WHERE
            Email =: 'test.serviceuser@google.com'
    ];
	
	 Account testAccount = [
        SELECT 
            Id 
        FROM 
            Account];
			
       Product2 testProduct = [
        SELECT 
            Id 
        FROM 
            Product2];
		

	CaseAsset__c testCaseAsset = new CaseAsset__c(Name='Test Case Asset', Account__c=testAccount.Id, SerialNumber__c='123', Product__c = testProduct.Id, StartDate__c = System.today(), EndDate__c = System.today() + );
    testCaseAsset.OwnerId = serviceUser.ID;
    
    insert testCaseAsset;

    //ACT
    test.startTest();
    System.runAs(serviceUser){
        Entitlement testEntitlement = new Entitlement();
		testEntitlement.AccountID = testCaseAsset.Account__c;
		testEntitlement.Name = testCaseAsset.Name;
        testEntitlement.CasesPerEntitlement = integer.valueOf(testCaseAsset.NoOfCasesPerYear__c);
        testEntitlement.EndDate = testCaseAsset.EndDate__c;
        testEntitlement.StartDate = testCaseAsset.StartDate__c;
        testEntitlement.IsPerIncident = true;
        testEntitlement.CaseAsset__c = testCaseAsset.Id;
        testEntitlement.RemainingCases = integer.valueOf(testCaseAsset.NoOfCasesPerYear__c);
		insert testEntitlement;
    }   
    test.stopTest();
    //ASSERT
    Entitlement testEntitlementAfterProcess = [
        SELECT 
            Id,
            Name
        FROM 
            Entitlement
        ];
        System.assertEquals(testEntitlement.Id,
                        testEntitlementAfterProcess.Name,
                        'Entitlement not created');
   
    }

 
Randi WarnerRandi Warner
You need to declare the Entitlement testEntitlement = new Entitlement(); outside of the Runas block so the lower block has access to it. at the moment it doesnt exist where you are calling it.

just put the Entitlement testEntitlement = new Entitlement(); above the User.runAs() line. 
MichelCRMichelCR
Hi Laura,

As Randi Warner mentioned you should take the Entitlement testEntitlement; declaration outside of the System.runAs(){ ... } It could be like: 
 
....

    //ACT
    Entitlement testEntitlement;
    test.startTest();
    System.runAs(serviceUser){
        testEntitlement = new Entitlement();
		testEntitlement.AccountID = testCaseAsset.Account__c;
		testEntitlement.Name = testCaseAsset.Name;
        testEntitlement.CasesPerEntitlement = integer.valueOf(testCaseAsset.NoOfCasesPerYear__c);
        testEntitlement.EndDate = testCaseAsset.EndDate__c;
        testEntitlement.StartDate = testCaseAsset.StartDate__c;
        testEntitlement.IsPerIncident = true;
        testEntitlement.CaseAsset__c = testCaseAsset.Id;
        testEntitlement.RemainingCases = integer.valueOf(testCaseAsset.NoOfCasesPerYear__c);
		insert testEntitlement;
    }   
    test.stopTest();

....

I hope this helps