You need to sign in to do that
Don't have an account?
SF Dakshendra
An error occured when uploading csv file?blob is not valid for UTF8 string?
Here my code is,
public class UplaodLead
{
public blob leadcsvfile{get;set;}
public UplaodLead()
{
}
public void save()
{
if(leadcsvfile!=null)
{
try
{
list<lead> leadlist=new list<lead>();
string Csvfilestring=leadcsvfile.tostring();
boolean header=false;
for(string Columns:Csvfilestring.split('\n'))
{
if(header==false)
{
header=true;
continue;
}
integer i=0;
lead lr=new lead();
for(string Fields:Columns.split(','))
{
if(i==0)
lr.firstname=Fields;
else if(i==3)
{
lr.lastname=Fields;
}
else if(i==5)
{
lr.Email=Fields;
}
else if(i==6)
{
lr.Company=Fields;
}
else if(i==7)
{
lr.Department__c=Fields;
}
else if(i==8)
{
lr.Job_Title__c=Fields;
}
else if(i==9)
{
lr.street=Fields;
}
else if(i==10)
{
lr.Business_Street_2__c=Fields;
}
else if(i==12)
{
lr.City=Fields;
}
else if(i==13)
{
lr.State=Fields;
}
else if(i==14)
{
lr.PostalCode=Fields;
}
else if(i==15)
{
lr.Country=Fields;
}
else if(i==16)
{
lr.Home_Street1__c=Fields;
}
else if(i==19)
{
lr.Home_City__c=Fields;
}
else if(i==20)
{
lr.Home_State__c=Fields;
}
else if(i==21)
{
lr.Home_Postal_Code__c=Fields;
}
else if(i==22)
{
lr.Home_Country_Region__c=Fields;
}
else if(i==23)
{
lr.Fax=Fields;
}
else if(i==24)
{
lr.Phone=Fields;
}
else if(i==25)
{
lr.Car_Phone__c=Fields;
}
else if(i==26)
{
lr.HomePhone__c=Fields;
}
else if(i==27)
{
lr.MobilePhone=Fields;
}
else if(i==28)
{
lr.Categories__c=Fields;
}
else if(i==29)
{
lr.E_mail_Type__c=Fields;
}
else if(i==30)
{
lr.E_mail_Display_Name__c=Fields;
}
else if(i==31)
{
lr.Description=Fields;
leadlist.add(lr);
}
i++;
}
}
if(leadlist.size()>0)
insert leadlist;
}
catch(Exception e)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,''+e.getmessage());//Same old drama
ApexPages.addMessage(myMsg);
}
}
}
}
public class UplaodLead
{
public blob leadcsvfile{get;set;}
public UplaodLead()
{
}
public void save()
{
if(leadcsvfile!=null)
{
try
{
list<lead> leadlist=new list<lead>();
string Csvfilestring=leadcsvfile.tostring();
boolean header=false;
for(string Columns:Csvfilestring.split('\n'))
{
if(header==false)
{
header=true;
continue;
}
integer i=0;
lead lr=new lead();
for(string Fields:Columns.split(','))
{
if(i==0)
lr.firstname=Fields;
else if(i==3)
{
lr.lastname=Fields;
}
else if(i==5)
{
lr.Email=Fields;
}
else if(i==6)
{
lr.Company=Fields;
}
else if(i==7)
{
lr.Department__c=Fields;
}
else if(i==8)
{
lr.Job_Title__c=Fields;
}
else if(i==9)
{
lr.street=Fields;
}
else if(i==10)
{
lr.Business_Street_2__c=Fields;
}
else if(i==12)
{
lr.City=Fields;
}
else if(i==13)
{
lr.State=Fields;
}
else if(i==14)
{
lr.PostalCode=Fields;
}
else if(i==15)
{
lr.Country=Fields;
}
else if(i==16)
{
lr.Home_Street1__c=Fields;
}
else if(i==19)
{
lr.Home_City__c=Fields;
}
else if(i==20)
{
lr.Home_State__c=Fields;
}
else if(i==21)
{
lr.Home_Postal_Code__c=Fields;
}
else if(i==22)
{
lr.Home_Country_Region__c=Fields;
}
else if(i==23)
{
lr.Fax=Fields;
}
else if(i==24)
{
lr.Phone=Fields;
}
else if(i==25)
{
lr.Car_Phone__c=Fields;
}
else if(i==26)
{
lr.HomePhone__c=Fields;
}
else if(i==27)
{
lr.MobilePhone=Fields;
}
else if(i==28)
{
lr.Categories__c=Fields;
}
else if(i==29)
{
lr.E_mail_Type__c=Fields;
}
else if(i==30)
{
lr.E_mail_Display_Name__c=Fields;
}
else if(i==31)
{
lr.Description=Fields;
leadlist.add(lr);
}
i++;
}
}
if(leadlist.size()>0)
insert leadlist;
}
catch(Exception e)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,''+e.getmessage());//Same old drama
ApexPages.addMessage(myMsg);
}
}
}
}
Below code work in my org. Please try now.
public class UplaodLead
{
public blob leadcsvfile{get;set;}
public UplaodLead()
{
}
public void save()
{
if(leadcsvfile!=null)
{
try
{
list<lead> leadlist=new list<lead>();
Blob csvBlob = leadcsvfile;
string Csvfilestring = EncodingUtil.base64Encode(csvBlob);
boolean header=false;
for(string Columns:Csvfilestring.split('\n'))
{
if(header==false)
{
header=true;
continue;
}
integer i=0;
lead lr=new lead();
for(string Fields:Columns.split(','))
{
if(i==0)
lr.firstname=Fields;
else if(i==3)
{
lr.lastname=Fields;
}
else if(i==5)
{
lr.Email=Fields;
}
else if(i==6)
{
lr.Company=Fields;
}
else if(i==7)
{
lr.Department__c=Fields;
}
else if(i==8)
{
lr.Job_Title__c=Fields;
}
else if(i==9)
{
lr.street=Fields;
}
else if(i==10)
{
lr.Business_Street_2__c=Fields;
}
else if(i==12)
{
lr.City=Fields;
}
else if(i==13)
{
lr.State=Fields;
}
else if(i==14)
{
lr.PostalCode=Fields;
}
else if(i==15)
{
lr.Country=Fields;
}
else if(i==16)
{
lr.Home_Street1__c=Fields;
}
else if(i==19)
{
lr.Home_City__c=Fields;
}
else if(i==20)
{
lr.Home_State__c=Fields;
}
else if(i==21)
{
lr.Home_Postal_Code__c=Fields;
}
else if(i==22)
{
lr.Home_Country_Region__c=Fields;
}
else if(i==23)
{
lr.Fax=Fields;
}
else if(i==24)
{
lr.Phone=Fields;
}
else if(i==25)
{
lr.Car_Phone__c=Fields;
}
else if(i==26)
{
lr.HomePhone__c=Fields;
}
else if(i==27)
{
lr.MobilePhone=Fields;
}
else if(i==28)
{
lr.Categories__c=Fields;
}
else if(i==29)
{
lr.E_mail_Type__c=Fields;
}
else if(i==30)
{
lr.E_mail_Display_Name__c=Fields;
}
else if(i==31)
{
lr.Description=Fields;
leadlist.add(lr);
}
i++;
}
}
if(leadlist.size()>0)
insert leadlist;
}
catch(Exception e)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,''+e.getmessage());//Same old drama
ApexPages.addMessage(myMsg);
}
}
}
}
Thanks,
Gaurav
All Answers
HI Dakshendra,
Please replace "string Csvfilestring=leadcsvfile.tostring();" with below two lines.
Blob csvBlob = leadcsvfile.Body;
string Csvfilestring = EncodingUtil.base64Encode(leadcsvfile);
Let me know if you still some issue, else mark it as best answer.
Thanks,
Gaurav
skype: gaurav62990
email: gauravgarg.nmims@gmail.com
when i add those two lines i got an error
Initial term of field expression must be a concrete SObject: Blob
Thanks
Please try now.
Blob csvBlob = leadcsvfile.Body;
string Csvfilestring = EncodingUtil.base64Encode(csvBlob );
Let me know if you still some issue, else mark it as best answer.
Thanks,
Gaurav
skype: gaurav62990
email: gauravgarg.nmims@gmail.com
AGAIN I GOT SAME ERROR WHEN I CHANGE CODE
THANKS
Below code work in my org. Please try now.
public class UplaodLead
{
public blob leadcsvfile{get;set;}
public UplaodLead()
{
}
public void save()
{
if(leadcsvfile!=null)
{
try
{
list<lead> leadlist=new list<lead>();
Blob csvBlob = leadcsvfile;
string Csvfilestring = EncodingUtil.base64Encode(csvBlob);
boolean header=false;
for(string Columns:Csvfilestring.split('\n'))
{
if(header==false)
{
header=true;
continue;
}
integer i=0;
lead lr=new lead();
for(string Fields:Columns.split(','))
{
if(i==0)
lr.firstname=Fields;
else if(i==3)
{
lr.lastname=Fields;
}
else if(i==5)
{
lr.Email=Fields;
}
else if(i==6)
{
lr.Company=Fields;
}
else if(i==7)
{
lr.Department__c=Fields;
}
else if(i==8)
{
lr.Job_Title__c=Fields;
}
else if(i==9)
{
lr.street=Fields;
}
else if(i==10)
{
lr.Business_Street_2__c=Fields;
}
else if(i==12)
{
lr.City=Fields;
}
else if(i==13)
{
lr.State=Fields;
}
else if(i==14)
{
lr.PostalCode=Fields;
}
else if(i==15)
{
lr.Country=Fields;
}
else if(i==16)
{
lr.Home_Street1__c=Fields;
}
else if(i==19)
{
lr.Home_City__c=Fields;
}
else if(i==20)
{
lr.Home_State__c=Fields;
}
else if(i==21)
{
lr.Home_Postal_Code__c=Fields;
}
else if(i==22)
{
lr.Home_Country_Region__c=Fields;
}
else if(i==23)
{
lr.Fax=Fields;
}
else if(i==24)
{
lr.Phone=Fields;
}
else if(i==25)
{
lr.Car_Phone__c=Fields;
}
else if(i==26)
{
lr.HomePhone__c=Fields;
}
else if(i==27)
{
lr.MobilePhone=Fields;
}
else if(i==28)
{
lr.Categories__c=Fields;
}
else if(i==29)
{
lr.E_mail_Type__c=Fields;
}
else if(i==30)
{
lr.E_mail_Display_Name__c=Fields;
}
else if(i==31)
{
lr.Description=Fields;
leadlist.add(lr);
}
i++;
}
}
if(leadlist.size()>0)
insert leadlist;
}
catch(Exception e)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,''+e.getmessage());//Same old drama
ApexPages.addMessage(myMsg);
}
}
}
}
Thanks,
Gaurav
Its Working
Thanks