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
Suresh RaghuramSuresh Raghuram 

Variable does not exist

Hi Community

 

I am writing a trigger on contact and trying to get all AccountId s related to the contact  in the following code but i am comming across an error 

Variable does not exist: AccountId.

 

set<Account> ids = new set<Account>();
for(Contact cnt : Trigger.New){
ids.add(cnt.AccountId);
}

 

Do any body let me know what mistake i did.

 

Thanks in advance for your time and help.

bob_buzzardbob_buzzard

Can you post the rest of your code?  That snippet looks fine to me and the contact sobject definitely has a field named 'AccountId'.

Suresh RaghuramSuresh Raghuram

when i look for the account look up field on the contact its api name is Account ,

 

I also check force.com Ide schema I noticed the AccountIde over there.

I  did not find where iam making the mistake.

 

trigger MobilePhone on Contact(before Insert){

set<Account> ids = new set<Account>();
for(Contact cnt : Trigger.New){

ids.add(cnt.AccountId);
}
Map<Id, Account> accMapIds = new Map<Id, Account>([Select Id,Phone from Account where Id
IN: ids]);
List<Account> act = new List<Account>();
for(Contact ct : trigger.New){
if(ct.Primary__c == True){
Account acc = accMapIds.get(ct.AccountId);
acc.Phone = ct.Phone;
act.add(acc);
}
}
Update act;
}

gaisergaiser

 

You declared a set of Accounts

set<Account> ids = new set<Account>();

But trying to add Id into this set
ids.add(cnt.AccountId);

Judging by the rest of you code the "ids" set should be declared like this

set<Id> ids = new set<Id>();

 

 

 

Suresh RaghuramSuresh Raghuram

Even after changing the same error

gaisergaiser

>>Even after changing the same error

 

Unless you have come across some weird bug in apex compiler this most likely means that what you pasted in this discussion does not exactly represent the code you are trying to save.

 

The error message like you mentioned "Variable does not exist: AccountId." would occur if we changed this

for(Contact cnt : Trigger.New){

ids.add(cnt.AccountId);
}

 

to this:

for(Contact cnt : Trigger.New){

ids.add(AccountId);
}

 

Notice that I removed "cnt." in front of AccountId.

 

Also, "Variable does not exist" compiler error usually includes line number. What is the line number in your case?

Suresh RaghuramSuresh Raghuram

Still no change

Error: Compile Error: Variable does not exist: AccountId at line 4 column 17

trigger primaryPhone on Contact(before Insert){

set ids = new set();

for(Contact cnt : Trigger.New){

ids.add(AccountId);

}

Map accMapIds = new Map([Select Id,Phone from Account where Id IN: ids]);

List act = new List();

for(Contact ct : trigger.New){ if(ct.Primary__c == True){

Account acc = accMapIds.get(AccountId);

acc.Phone = ct.Phone;

act.add(acc);

}

} Update act;

}

a!a!

for(Contact ct : trigger.New){ if(ct.Primary__c == True)

 

Please see the above piece of code. wheather it is cnt Or ct. check it once 

gaisergaiser

Looks like my last post was not clear enough.

When I suggested to change 

ids.add(cnt.AccountId);

to 

ids.add(AccountId);

I meant to show you what the code which produces comile error you mentioned may look like.

 

Here is the version of your trigger which compiles without problems

trigger MobilePhone on Contact(before Insert){

  set<Id> ids = new set<Id>();
  for(Contact cnt : Trigger.New){
    ids.add(cnt.AccountId);
  }
  Map<Id, Account> accMapIds = new Map<Id, Account>([Select Id,Phone from Account where Id IN: ids]);
  List<Account> act = new List<Account>();
  for(Contact ct : trigger.New){
    if(ct.Primary__c == True){
      Account acc = accMapIds.get(ct.AccountId);
      acc.Phone = ct.Phone;
      act.add(acc);
    }
  }
  Update act;
}

 

 

 

 

Suresh RaghuramSuresh Raghuram

By default there is a Account look up field on the contact and to access the related record  on account we use AccountId  up to this i am clear , And i believe my code is fine , Other than above two reason is there any other reasons for this issue