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
ZoomVZoomV 

Variable does not exist in trigger test

I've put together the following test for a trigger. 

@isTest
public with sharing class AutoCreateSubsTriggerTest {

    static testMethod void testAutoCreateSubs() {
        // First, set up your test User.
        User testUser = generateTestUser();

        // Generate your position but don't perform DML yet
        Contract_Overview__c contover = new Contract(Subsidiaries_On_Contract__c = testUser.Id);

        // Start your test and insert your contract overview
        Test.startTest();
        insert contover;
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<Subs_Serviced_On_Contract__c> subs = [SELECT
                                                 Id
                                             FROM
                                                 Subs_Serviced_On_Contract__c
                                             WHERE
                                                 Contract_Overview__c = :contover.Id
        ];

        // There should be 1 interviewer because only one user ID was provided
        System.assert(1, subs.size(), 'One sub object is expected');
    }

    private User generateTestUser(){
        // We use the System Admin profile because it will always be there
        Profile sysAdmin = [SELECT 
                               Id 
                            FROM 
                               Profile 
                            WHERE 
                               Name = 'System Administrator'
                            LIMIT 1
        ];

        // Generate the user
        User newUser = new User(
            UserName = 'unit.tester@example.com',
            LastName = 'Test',
            FirstName = 'Jane',
            Email = 'unit.tester@example.com',
            phone = '555-555-5555',
            MobilePhone = '555-555-5554',
            Street = '123 Fake Street',
            City = 'Los Angeles',
            State = 'CA',
            PostalCode = '12345',
            CommunityNickName = 'unit.test',
            Alias = 'abcd',
            ProfileId = sysAdmin.Id,
            emailencodingkey = 'UTF-8',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles'
        );

        // Insert that user
        insert user;

        return user;
    }
}

 I am getting the error "Variable does not exist : user". I don't understand how that's happening. Anybody got an idea ? 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
@isTest
public with sharing class AutoCreateSubsTriggerTest {

    static testMethod void testAutoCreateSubs() {
        // First, set up your test User.
        User testUser = generateTestUser();

        // Generate your position but don't perform DML yet
        Contract_Overview__c contover = new Contract(Subsidiaries_On_Contract__c = testUser.Id);

        // Start your test and insert your contract overview
        Test.startTest();
        insert contover;
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<Subs_Serviced_On_Contract__c> subs = [SELECT
                                                 Id
                                             FROM
                                                 Subs_Serviced_On_Contract__c
                                             WHERE
                                                 Contract_Overview__c = :contover.Id
        ];

        // There should be 1 interviewer because only one user ID was provided
        System.assert(1, subs.size(), 'One sub object is expected');
    }

    private User generateTestUser(){
        // We use the System Admin profile because it will always be there
        Profile sysAdmin = [SELECT 
                               Id 
                            FROM 
                               Profile 
                            WHERE 
                               Name = 'System Administrator'
                            LIMIT 1
        ];

        // Generate the user
        User newUser = new User(
            UserName = 'unit.tester@example.com',
            LastName = 'Test',
            FirstName = 'Jane',
            Email = 'unit.tester@example.com',
            phone = '555-555-5555',
            MobilePhone = '555-555-5554',
            Street = '123 Fake Street',
            City = 'Los Angeles',
            State = 'CA',
            PostalCode = '12345',
            CommunityNickName = 'unit.test',
            Alias = 'abcd',
            ProfileId = sysAdmin.Id,
            emailencodingkey = 'UTF-8',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles'
        );

        // Insert that user
        insert newUser;

        return newUser;
    }
}

 you are trying to insert user instead of newUser

All Answers

Naidu PothiniNaidu Pothini
@isTest
public with sharing class AutoCreateSubsTriggerTest {

    static testMethod void testAutoCreateSubs() {
        // First, set up your test User.
        User testUser = generateTestUser();

        // Generate your position but don't perform DML yet
        Contract_Overview__c contover = new Contract(Subsidiaries_On_Contract__c = testUser.Id);

        // Start your test and insert your contract overview
        Test.startTest();
        insert contover;
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<Subs_Serviced_On_Contract__c> subs = [SELECT
                                                 Id
                                             FROM
                                                 Subs_Serviced_On_Contract__c
                                             WHERE
                                                 Contract_Overview__c = :contover.Id
        ];

        // There should be 1 interviewer because only one user ID was provided
        System.assert(1, subs.size(), 'One sub object is expected');
    }

    private User generateTestUser(){
        // We use the System Admin profile because it will always be there
        Profile sysAdmin = [SELECT 
                               Id 
                            FROM 
                               Profile 
                            WHERE 
                               Name = 'System Administrator'
                            LIMIT 1
        ];

        // Generate the user
        User newUser = new User(
            UserName = 'unit.tester@example.com',
            LastName = 'Test',
            FirstName = 'Jane',
            Email = 'unit.tester@example.com',
            phone = '555-555-5555',
            MobilePhone = '555-555-5554',
            Street = '123 Fake Street',
            City = 'Los Angeles',
            State = 'CA',
            PostalCode = '12345',
            CommunityNickName = 'unit.test',
            Alias = 'abcd',
            ProfileId = sysAdmin.Id,
            emailencodingkey = 'UTF-8',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles'
        );

        // Insert that user
        insert newUser;

        return newUser;
    }
}

 you are trying to insert user instead of newUser

This was selected as the best answer
ZoomVZoomV

Thanks Naidu - totally missed that.

ZoomVZoomV

Naidu,

I am now getting a "Save Error : Method does not exist or incorrect signature : generateTestUser()" for this line : 

User testUser = generateTestUser();


I need to make it static, but I'm not sure how I would do that in this particular code ... ? 

Thank you for any help.

 

 

Naidu PothiniNaidu Pothini
static testMethod void testAutoCreateSubs()
    {
        Test.startTest();

        // First, set up your test User.
        User testUser = generateTestUser();

        // Generate your position but don't perform DML yet
        Contract_Overview__c contover = new Contract(Subsidiaries_On_Contract__c = testUser.Id);

        // Start your test and insert your contract overview
        
        insert contover;
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<Subs_Serviced_On_Contract__c> subs = [SELECT Id FROM Subs_Serviced_On_Contract__c WHERE Contract_Overview__c = :contover.Id];

        // There should be 1 interviewer because only one user ID was provided
        System.assert(1, subs.size(), 'One sub object is expected');
    }

can you change this method like the one above and try. Let me know whats happening.

ZoomVZoomV

Naidu - I'm not sure what you're saying to do here. Is this what you are asking me to change the code to ? : 

@isTest
public with sharing class AutoCreateSubsTriggerTest {

   static testMethod void testAutoCreateSubs()
    {
        Test.startTest();

        // First, set up your test User.
        User testUser = generateTestUser();

        // Generate your position but don't perform DML yet
        Contract_Overview__c contover = new Contract_Overview__c(Subsidiaries_On_Contract__c = testUser.Id);

        // Start your test and insert your contract overview
        
        insert contover;
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<Subs_Serviced_On_Contract__c> subs = [SELECT Id FROM Subs_Serviced_On_Contract__c WHERE Contract_Overview__c = :contover.Id];

        // There should be 1 interviewer because only one user ID was provided
        System.assert(1, subs.size(), 'One sub object is expected');
    }

    private User generateTestUser(){
        // We use the System Admin profile because it will always be there
        Profile sysAdmin = [SELECT 
                               Id 
                            FROM 
                               Profile 
                            WHERE 
                               Name = 'System Administrator'
                            LIMIT 1
        ];

        // Generate the user
        User newUser = new User(
            UserName = 'unit.tester@example.com',
            LastName = 'Test',
            FirstName = 'Jane',
            Email = 'unit.tester@example.com',
            phone = '555-555-5555',
            MobilePhone = '555-555-5554',
            Street = '123 Fake Street',
            City = 'Los Angeles',
            State = 'CA',
            PostalCode = '12345',
            CommunityNickName = 'unit.test',
            Alias = 'abcd',
            ProfileId = sysAdmin.Id,
            emailencodingkey = 'UTF-8',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles'
        );

        // Insert that user
        insert newuser;

        return newuser;
    }
}

 
I'm getting a "Save Error : Method does not exist or incorrect signature: generateTestUser()" error 

along with : 

"Save Error : Only top-level class variables can be declared static"

Thank you for your help.

 

 

Naidu PothiniNaidu Pothini
@isTest
public with sharing class AutoCreateSubsTriggerTest
{
	public Static profile sysAdmin = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1]

	public Static User newUser = new User(UserName = 'unit.tester@example.com',
								          LastName = 'Test',
								          FirstName = 'Jane',
								          Email = 'unit.tester@example.com',
								          phone = '555-555-5555',
								          MobilePhone = '555-555-5554',
								          Street = '123 Fake Street',
								          City = 'Los Angeles',
								          State = 'CA',
								          PostalCode = '12345',
								          CommunityNickName = 'unit.test',
								          Alias = 'abcd',
								          ProfileId = sysAdmin.Id,
								          emailencodingkey = 'UTF-8',
								          languagelocalekey = 'en_US',
								          localesidkey = 'en_US',
								          timezonesidkey = 'America/Los_Angeles');

    static testMethod void testAutoCreateSubs()
    {
        Test.startTest();

        insert testUser;

        Contract_Overview__c contover = new Contract(Subsidiaries_On_Contract__c = testUser.Id);

        insert contover;
        
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<Subs_Serviced_On_Contract__c> subs = [SELECT Id FROM Subs_Serviced_On_Contract__c WHERE Contract_Overview__c = :contover.Id];

        // There should be 1 interviewer because only one user ID was provided
        System.assert(1, subs.size(), 'One sub object is expected');
    }
}

 try this,.

ZoomVZoomV

Thank you Naidu,

I am now getting this error : "Save error : unexpected token 'Static'" for this line : 

public Static User newUser = new User(UserName = 'unit.tester@example.com',

 
Thank you very much for your time & effort.




Naidu PothiniNaidu Pothini
@isTest
public with sharing class AutoCreateSubsTriggerTest
{
    public Static profile sysAdmin = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1]

    static testMethod void testAutoCreateSubs()
    {
        Test.startTest();

User newUser = new User(UserName = 'unit.tester@example.com', LastName = 'Test', FirstName = 'Jane', Email = 'unit.tester@example.com',
phone = '555-555-5555', MobilePhone = '555-555-5554', Street = '123 Fake Street', City = 'Los Angeles', State = 'CA',
PostalCode = '12345', CommunityNickName = 'unit.test', Alias = 'abcd', ProfileId = sysAdmin.Id, emailencodingkey = 'UTF-8',
languagelocalekey = 'en_US', localesidkey = 'en_US', timezonesidkey = 'America/Los_Angeles');

insert testUser;

Contract_Overview__c contover = new Contract(Subsidiaries_On_Contract__c = testUser.Id);
insert contover;

Test.stopTest();

// Get the subs from the DB to ensure they were created
List<Subs_Serviced_On_Contract__c> subs = [SELECT Id FROM Subs_Serviced_On_Contract__c WHERE Contract_Overview__c = :contover.Id];
// There should be 1 interviewer because only one user ID was provided
System.assert(1, subs.size(), 'One sub object is expected');
}
}

 try this... It should not thrown the error for static key word.. but try this let me know if it doesnt work.

ZoomVZoomV

Naidu,

I'm still getting the same error. ( Save error : unexpected token 'static').

 

Now it's for this line : 

static testMethod void testAutoCreateSubs()

 Thank you for your help.

Naidu PothiniNaidu Pothini
@isTest
public with sharing class AutoCreateSubsTriggerTest
{
	public Static profile sysAdmin = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1]; // check this.

	public Static User newUser = new User(UserName = 'unit.tester@example.com',
								          LastName = 'Test',
								          FirstName = 'Jane',
								          Email = 'unit.tester@example.com',
								          phone = '555-555-5555',
								          MobilePhone = '555-555-5554',
								          Street = '123 Fake Street',
								          City = 'Los Angeles',
								          State = 'CA',
								          PostalCode = '12345',
								          CommunityNickName = 'unit.test',
								          Alias = 'abcd',
								          ProfileId = sysAdmin.Id,
								          emailencodingkey = 'UTF-8',
								          languagelocalekey = 'en_US',
								          localesidkey = 'en_US',
								          timezonesidkey = 'America/Los_Angeles');

    static testMethod void testAutoCreateSubs()
    {
        Test.startTest();

        insert testUser;

        Contract_Overview__c contover = new Contract(Subsidiaries_On_Contract__c = testUser.Id);

        insert contover;
        
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<Subs_Serviced_On_Contract__c> subs = [SELECT Id FROM Subs_Serviced_On_Contract__c WHERE Contract_Overview__c = :contover.Id];

        // There should be 1 interviewer because only one user ID was provided
        System.assert(1, subs.size(), 'One sub object is expected');
    }
}

 

ZoomVZoomV

Naidu,

just fyi : I got it working with this code : 

public with sharing class AutoCreateSubsTriggerTest {

    static testMethod void testAutoCreateSubs() {
        // First, set up your test User.
        User testUser = generateTestUser();

        // Generate your position but don't perform DML yet
        Contract_Overview__c contover = new Contract_Overview__c(Subsidiaries_On_Contract__c = testUser.Id);
 
        // Start your test and insert your contract overview
        Test.startTest();
        insert contover;
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<Subs_Serviced_On_Contract__c> subs = [SELECT
                                                 Id
                                             FROM
                                                 Subs_Serviced_On_Contract__c
                                             WHERE
                                                 Contract_Overview__c = :contover.Id
        ];

        // There should be 1 interviewer because only one user ID was provided
        //System.assert(1, subs.size(), 'One sub object is expected');
        System.assert(1 == subs.size(), 'One sub object is expected');
    }

    private static User generateTestUser(){
        // We use the System Admin profile because it will always be there
        Profile sysAdmin = [SELECT 
                               Id 
                            FROM 
                               Profile 
                            WHERE 
                               Name = 'System Administrator'
                            LIMIT 1
        ];
 
        // Generate the user
        User newUser = new User(
            UserName = 'unit.tester@example.com',
            LastName = 'Test',
            FirstName = 'Jane',
            CompanyName = 'TestCo',
            Title = 'Developer',
            Email = 'unit.tester@example.com',
            phone = '555-555-5555',
            MobilePhone = '555-555-5554',
            Street = '123 Fake Street',
            City = 'Los Angeles',
            State = 'CA',
            PostalCode = '12345',
            CommunityNickName = 'unit.test',
            Alias = 'abcd',
            ProfileId = sysAdmin.Id,
            emailencodingkey = 'UTF-8',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles'
        );

        // Insert that user
        insert newuser;

        return newuser;
    }
}

 Thank you very much for your help & effort.