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 CSV file,Mapping custom fields to "Address fields" in Account

Hi All,wheni upload a csv it must create account, contact, opportunity.

In my custom lead i am having custom field

company name,

company street,

company city, 

comapny state,

company postal code.

 

when i upload csv file,this fields must be mapped to "Billing Address" field on "Account". How this can be done. i didn't how to map this fileds.

Apex code:

public class FileUploaderAll
{
public FileUploaderAll(ApexPages.StandardController controller) {
}

public PageReference fileAccess() {
return null;
}

public FileUploaderAll(){
}
public string nameFile{get;set;}
public Blob contentFile{get;set;}
String[] filelines = new String[]{};
List<Account> accstoupload;
List<Contact> contoupload;
List<Opportunity> opptoupload;
List<CustomLead__c> custtoupload;
List<String> acctNames;

List<Account> newAccts = new List<Account>();
public Pagereference ReadFile()
{
nameFile=contentFile.toString();
filelines = nameFile.split('\n');
accstoupload = new List<Account>();
contoupload = new List<Contact>();
opptoupload = new List<Opportunity>();
custtoupload = new List<CustomLead__c>();
acctNames = new List<String>();
List<Account> existingAccts =new List<Account>();
for (Integer i=1;i<filelines.size();i++)
{
String[] inputvalues = new String[]{};
inputvalues = filelines[i].split(',');
Account a = new Account();
a.Name = inputvalues[0];
acctNames.add(a.Name);

accstoupload.add(a);
}


existingAccts = [SELECT Id, Name FROM Account where name in :acctNames];
//create a map with names as key
Map<String, Id> acctNamesIdMap = new Map<String, Id>();
// load the map - this will help you find out if an account name exists already
for (Account acct : existingAccts)
{
acctNamesIdMap.put(acct.Name, acct.Id);
System.debug('******Sai******');
}

for (Account acct : accstoupload)
{
//if account name does not exist in map, add it to list of new accounts
if (!acctNamesIdMap.containsKey(acct.Name))
//if (!acctNamesIdMap.containsKey(acct.Id))
{
newAccts.add(acct);

}

}

try
{

insert newAccts;
ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.info,'AccountName with Account Id are the new Account created.Related Contact and Opportunity also created');
ApexPages.Message msg1 = new ApexPages.Message(ApexPages.severity.info,'AccountName without Account Id are the existing Accounts');
ApexPages.addMessage(msg);
ApexPages.addMessage(msg1);

}
catch (Exception e)
{

ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Account Name already exist, change Account Name and try again');
ApexPages.addMessage(errormsg);

}


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

Contact con = new Contact();
for(account a: accstoupload)
{
con.AccountId = accstoupload[i-1].id;
//con.AccountId = a.Id;
}
//con.Account= inputconvalues[0];
if(con.AccountId!= null){

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

contoupload.add(con);
}}
try
{
insert contoupload;

Best Answer chosen by Admin (Salesforce Developers) 
ForcepowerForcepower

Lavanya,

 

Very simple and by the way, these are standard fields. Look at the bolded text - adjust bsed on the exact order in which you have these fields in your file.

 

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

a.BillingStreet  = inputvalues[1];

a.BillingState = inputvalues[2];

a.BillingPostalCode = inputvalues[3];

a.BillingCountry = inputvalues[4];

a.BillingCity = inputvalues[5];
acctNames.add(a.Name);

accstoupload.add(a);
}

Best,

Ram

All Answers

ForcepowerForcepower

Lavanya,

 

Very simple and by the way, these are standard fields. Look at the bolded text - adjust bsed on the exact order in which you have these fields in your file.

 

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

a.BillingStreet  = inputvalues[1];

a.BillingState = inputvalues[2];

a.BillingPostalCode = inputvalues[3];

a.BillingCountry = inputvalues[4];

a.BillingCity = inputvalues[5];
acctNames.add(a.Name);

accstoupload.add(a);
}

Best,

Ram

This was selected as the best answer
LavanyaLavanya

Hi Ram , thanks a lot. I thought this but i had a doubt about this. Now i am clear. Thanks a lot. 

Regards,

Lavanya.