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
LavanyaLavanya 

Uploading from csv file to create Account and Contact.

Hi when i am upload a csv to create Account and contact using Apex code. In the contact i need to give the acoount name which is a list. How to get accoutn id to contact.In the code i have bold the line contac.Kindly anyone tell how to resolve this.

 

My code:

 

accstoupload = new List<Account>();
contoupload = new List<Contact>();
opptoupload = new List<Opportunity>();
for (Integer i=1;i<filelines.size();i++)
{
String[] inputvalues = new String[]{};
inputvalues = filelines[i].split(',');

Account a = new Account();
a.Name = inputvalues[0];

accstoupload.add(a);
}

for (Integer i=1;i<filelines.size();i++)
{
String[] inputconvalues = new String[]{};
inputconvalues = filelines[i].split(',');

Contact con = new Contact();
 con.AccountId = a.Id;
con.Lastname = inputconvalues[1];
con.Lastname_lead__c = inputconvalues[2];


contoupload.add(con);
}

Best Answer chosen by Admin (Salesforce Developers) 
Yoganand GadekarYoganand Gadekar

In your context, you are refreing to a.id, but you are not iterating over the account list hence you will get error for unknown variable.

If you want to put a.id then iterate over account lis while puting the accouint id.

for eg as below:

---------------------------------------------------------

Contact con = new Contact();

for(account a: accstoupload){

 con.AccountId = a.Id;

}
con.Lastname = inputconvalues[1];
con.Lastname_lead__c = inputconvalues[2];

-------------------------------------------------------------

 

in above eg, the last account record in list id will be assigned. So it is upto you which account id you want, you can use map to save the id. Or else camapre any field with If condituion and use the account id in for loop.

 

Mark this as answer if it helps you.

 

Thanks,

Yoganand

All Answers

Yoganand GadekarYoganand Gadekar

In your context, you are refreing to a.id, but you are not iterating over the account list hence you will get error for unknown variable.

If you want to put a.id then iterate over account lis while puting the accouint id.

for eg as below:

---------------------------------------------------------

Contact con = new Contact();

for(account a: accstoupload){

 con.AccountId = a.Id;

}
con.Lastname = inputconvalues[1];
con.Lastname_lead__c = inputconvalues[2];

-------------------------------------------------------------

 

in above eg, the last account record in list id will be assigned. So it is upto you which account id you want, you can use map to save the id. Or else camapre any field with If condituion and use the account id in for loop.

 

Mark this as answer if it helps you.

 

Thanks,

Yoganand

This was selected as the best answer
LavanyaLavanya

Thanks for the reply, can you please explain me more on this, i new to apex code. 

 

LavanyaLavanya
Hi Yoganand, its working as you said it is assigning the last record account id. but i want for accoutn no.1 i need to add contact no.1 & Account no.2 i need to add conatct no.2 likewise. Can you pls tell me how to do this. waiting for your reply
Yoganand GadekarYoganand Gadekar

You can use map to do this but Since you have not posted your code fully its bit difficult to get the full insight of your requirement.

 

From the code what i can understand is that you are putting zeroth item in array as name for account you can use that parameter.  

You can try as below,

---------------------------------------------------------------------------

accstoupload = new List<Account>();
contoupload = new List<Contact>();
opptoupload = new List<Opportunity>();

for (Integer i=1;i<filelines.size();i++)
{
String[] inputvalues = new String[]{};
inputvalues = filelines[i].split(',');

Account a = new Account();
a.Name = inputvalues[0];

accstoupload.add(a);
}

for (Integer i=1;i<filelines.size();i++)
{
String[] inputconvalues = new String[]{};
inputconvalues = filelines[i].split(',');

Contact con = new Contact();

for(account a: accstoupload){

if(a.name == inputconvalues[0]) 

con.AccountId = a.Id;

}


con.Lastname = inputconvalues[1];
con.Lastname_lead__c = inputconvalues[2];


contoupload.add(con);
}

 

-----------------------------------------------------------------------------------------