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
Rohan SRohan S 

Error: Compile Error: Variable does not exist: Employees at line 8 column 17

I'm writing a trigger on Account, where on creation, if Employees > 1000, a Contact record should be created. Even though the 'Employees' field is present by default in an Account page, it shows the following error: Error: Compile Error: Variable does not exist: Employees at line 8 column 17. Funny thing is if I change the criteria with any other field (like BillingCountry='ABC'), it throws no error. Below is the Apex class:

public class CreateAccountContactClass {
//Declare Contact Creation function
public static void CreateAccountContact (list<Account> AccountsList)
{
    list<Contact> ContactsList = new list<Contact>();
    for(Account VarA : AccountsList)
    {
        if(VarA.Employees > 1000)
        {
            Contact VarC = New Contact();
            VarC.FirstName = 'Admin';
            VarC.LastName = 'General';
            VarC.AccountId = VarA.id;
            ContactsList.add(VarC);
        }
    }
    insert ContactsList;
}
}
Best Answer chosen by Rohan S
Deepali KulshresthaDeepali Kulshrestha
Hi Rohit,

Try the following code it may be helpful for you:
public class CreateAccountContactClass {
public static void CreateAccountContact (list<Account> AccountsList)
{
    list<Contact> ContactsList = new list<Contact>();
    for(Account VarA : AccountsList)
    {
        if(VarA.NumberOfEmployees > 1000)
        {
            Contact VarC = New Contact();
            VarC.FirstName = 'Admin';
            VarC.LastName = 'General';
            VarC.AccountId = VarA.id;
            ContactsList.add(VarC);
        }
    }
    insert ContactsList;
}
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Rohit,

Try the following code it may be helpful for you:
public class CreateAccountContactClass {
public static void CreateAccountContact (list<Account> AccountsList)
{
    list<Contact> ContactsList = new list<Contact>();
    for(Account VarA : AccountsList)
    {
        if(VarA.NumberOfEmployees > 1000)
        {
            Contact VarC = New Contact();
            VarC.FirstName = 'Admin';
            VarC.LastName = 'General';
            VarC.AccountId = VarA.id;
            ContactsList.add(VarC);
        }
    }
    insert ContactsList;
}
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Rohit,

Try replacing your if condition:  'THIS'  VarA.Employees > 1000  'WITH'  VarA.NumberOfEmployees

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi