This type of errors occur when we are assigning value of one type to other.
For example.
public class IncompatibleType {
public list <account> accountList {get;set;}
public IncompatibleType () {
for(account acc:[select id,name from account]) {
accountList.add(acc.id);
}
}
}
In the above piece of code, we have declared accountList as list of account (list<account>) , but we are adding " id" to that list.So we will get the following error when we save that class.
Error: Compile Error: Incompatible element type Id for collection of Account at line 5 column 9.
See the image for reference.
To avoid this type of errors,make sure you are assigning value of same type.
Replace above code like this,It will get saved without any error.
public class IncompatibleType {
public list <account> accountList {get;set;}
public IncompatibleType () {
for(account acc:[select id,name from account]) {
accountList.add(acc);
}
}
}
For example.
public class IncompatibleType {
public list <account> accountList {get;set;}
public IncompatibleType () {
for(account acc:[select id,name from account]) {
accountList.add(acc.id);
}
}
}
In the above piece of code, we have declared accountList as list of account (list<account>) , but we are adding " id" to that list.So we will get the following error when we save that class.
Error: Compile Error: Incompatible element type Id for collection of Account at line 5 column 9.
See the image for reference.
To avoid this type of errors,make sure you are assigning value of same type.
Replace above code like this,It will get saved without any error.
public class IncompatibleType {
public list <account> accountList {get;set;}
public IncompatibleType () {
for(account acc:[select id,name from account]) {
accountList.add(acc);
}
}
}