You need to sign in to do that
Don't have an account?

uploading a csv file to create new Account, contact & opportunity using apex code
Hi i am trying to upload a csv using apex code, once i click upload button it must create a Account, contact and opportunity. its similar like the lead conversion. But in the contact i want to display the Account name to that contact. In my code i getting for this, kindly any one tell how to resolve this. The CSV may conatin more 10 records.
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;
public Pagereference ReadFile()
{
nameFile=contentFile.toString();
filelines = nameFile.split('\n');
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);
}
try
{
insert accstoupload;
}
catch (Exception e)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
ApexPages.addMessage(errormsg);
}
for (Integer i=1;i<filelines.size();i++)
{
String[] inputconvalues = new String[]{};
inputconvalues = filelines[i].split(',');
Contact con = new Contact();
con.AccountId = accstoupload.Id;
con.Lastname = inputconvalues[1];
con.Lastname_lead__c = inputconvalues[2];
contoupload.add(con);
}
try
{
insert contoupload;
}
catch (Exception e1)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
ApexPages.addMessage(errormsg);
}
// return null;
for (Integer i=1;i<filelines.size();i++)
{
String[] inputvalues = new String[]{};
inputvalues = filelines[i].split(',');
Opportunity opp = new Opportunity();
opp.Name = inputvalues[3];
opp.OPP_DateOfBirth__c = inputvalues[4];
opp.StageName = inputvalues[6];
opp.CloseDate = date.parse(inputvalues[5]);
opptoupload.add(opp);
}
try
{
insert opptoupload;
}
catch (Exception e2)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
ApexPages.addMessage(errormsg);
}
return null;
}
public List<Account> getuploadedAccounts()
{
if (accstoupload!= NULL)
if (accstoupload.size() > 0)
return accstoupload;
else
return null;
else
return null;
}
public List<Contact> getuploadedContact()
{
if (contoupload!= NULL)
if (contoupload.size() > 0)
return contoupload;
else
return null;
else
return null;
}
public List<Opportunity> getuploadedOpportunity()
{
if (opptoupload!= NULL)
if (opptoupload.size() > 0)
return opptoupload;
else
return null;
else
return null;
}
/* public pageReference fileAccess(){
Document lstDoc = [select id,name,Body from Document where name = 'test'];
System.Debug('DOC NAME :: '+lstDoc.name);
System.Debug('DOCBODY :: '+lstDoc.Body);
return null;
} */
public static testMethod void testReadFile1() {
Document lstDoc = [select id,name,Body from Document where name = 'test'];
// System.Debug('DOC NAME :: '+lstDoc.name);
//System.Debug('DOCBODY :: '+lstDoc.Body);
FileUploader file=new FileUploader();
Blob content= lstDoc.Body;
file.nameFile=content.toString();
}
public static testMethod void testReadFile2() {
}
}
Thanks,
Regards,
lavanya
I just saw your earlier thread on this. If you just want to assign Acct 1 to Contact 1 and so on down the line
try this:
con.AccountId = accstoupload[i-1].id;
no need for the Map approach as I suggested before.
All Answers
Hi Lavanya,
I'm assuming that you have an account name for each of your contacts in your input file with contacts. Here's what you need to do:
Create a Map<String, Id> and store your account id's by name when you create the accounts. So just loop thru' your accounts after you do the insert of accounts and do something like this:
Map<String, Id> acctIdMap = new Map<String, Id>();
try
{
insert accstoupload;
}
catch (Exception e)
{
------
}
for (Account acct : accstoupload) {
acctIdMap.put(acct.Name, acct.id);
}
In your loop to process contacts, do this:
for (Integer i=1;i<filelines.size();i++)
{
String[] inputconvalues = new String[]{};
inputconvalues = filelines[i].split(',');
Contact con = new Contact();
//need to read in account name for contact
String acctName = inputconvalues[???];
//get the account id from the Map
con.AccountId = acctIdMap.get(acctName);
//con.AccountId = accstoupload.Id;
con.Lastname = inputconvalues[1];
con.Lastname_lead__c = inputconvalues[2];
contoupload.add(con);}
}
Hope that helps.
Ram
I just saw your earlier thread on this. If you just want to assign Acct 1 to Contact 1 and so on down the line
try this:
con.AccountId = accstoupload[i-1].id;
no need for the Map approach as I suggested before.
Thanks a lot for the solution, it's working. From this a learn a lot thanks.
Ragards,
Lavanya.
Best,
Ram
Hi i need to check whether the acccount already exist or not. how to check this
I have requirment like :
uploading a csv file to create new Account, contact & opportunity using apex code.
Can you plz help me on this as you all ready know about this.
plz past your Vf page and Apex class here.
thanks in Advance..
T