+ Start a Discussion
Deepak Sharma 184Deepak Sharma 184 

Hello, getting error while executing the snippet, error-'Variable does not exist: FirstName'. can anybody tell.

List<contact> conlist= new list<contact>();
conlist.add(FirstName='joe', Department='Finance');
conlist.add(FirstName='kathy', Department='Technology');
conlist.add(FirstName='caroline', Department='Finance');
conlist.add(FirstName='kim', Department='Education');


insert conlist;

list<contact> lstUpdate = new list<contact>();
for(contact con: conlist)
{
    if(Department=='Finance')
    {
        con.Title='Financial Analyst';
    lstUpdate.add(con);
}
}
update lstUpdate;
Best Answer chosen by Deepak Sharma 184
swati_sehrawatswati_sehrawat
if(Department=='Finance')

replace it with if(con.Department=='Finance')

All Answers

swati_sehrawatswati_sehrawat
Try this and let me know if it works.
 
List<contact> conlist= new list<contact>();
contact obj = new contact();
obj.FirstName = 'joe';
obj.LastName = 'provide last name';
obj.Department = 'Finance';
conlist.add(obj);

obj = new contact();
obj.FirstName = 'kathy';
obj.LastName = 'provide last name';
obj.Department = 'Technology';
conlist.add(obj);

obj = new contact();
obj.FirstName = 'caroline';
obj.LastName = 'provide last name';
obj.Department = 'Finance';
conlist.add(obj);

obj = new contact();
obj.FirstName = 'kim';
obj.LastName = 'provide last name';
obj.Department = 'Education';
conlist.add(obj);
insert conlist;

list<contact> lstUpdate = new list<contact>();
for(contact con: conlist)
{
    if(con.Department=='Finance')
    {
        con.Title='Financial Analyst';
    lstUpdate.add(con);
}
}
update lstUpdate;

 
Deepak Sharma 184Deepak Sharma 184
Hi Swati, I tried with this one but now it shows- ' variable does not exist: department'
swati_sehrawatswati_sehrawat
Can you share the code.
Deepak Sharma 184Deepak Sharma 184
List<contact> conlist= new list<contact>();
contact obj = new contact();
obj.FirstName='joe';
obj.LastName='smith';
obj.Department='Finance';

conlist.add(obj);

obj = new contact();
obj.FirstName='kathy';
obj.LastName='smith';
obj.Department='Technology';

conlist.add(obj);

obj = new contact();
obj.FirstName='caroline';
obj.LastName='roth';
obj.Department='Finance';

conlist.add(obj);

obj = new contact();
obj.FirstName='kim';
obj.LastName='shane';
obj.Department='Education';

conlist.add(obj);



insert conlist;

list<contact> lstUpdate = new list<contact>();
for(contact con: conlist)
{
    if(Department=='Finance')
    {
        con.Title='Financial Analyst';
    lstUpdate.add(con);
}
}
update lstUpdate;




This is the same, as you shared.
swati_sehrawatswati_sehrawat
if(Department=='Finance')

replace it with if(con.Department=='Finance')
This was selected as the best answer
Deepak Sharma 184Deepak Sharma 184
working, thanks swati :)