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

i have created VF uploading CSV file and inserting record, how can i update when we find a matching PO# value in Salesforce
controller#
public class FileUploader {
public string nameFile {
get;
set;
}
public Blob contentFile {
get;
set;
}
public List < String > FieldsInFile {
get;
set;
}
String[] filelines = new String[] {};
List < ccs > ccsupload;
public Pagereference ReadFile() {
if(contentFile==Null)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Invalid template or file while processing your records...!');
ApexPages.addMessage(errormsg);
}
else
{
FieldsInFile = new List < String > ();
nameFile = contentFile.toString();
filelines = nameFile.split('\n');
ccsupload = new List < ccs > ();
//List<String> FirstLine = new List<String>();
//FirstLine = filelines[0].split(,);
Set < String > ObjectFieldList = new Set < String > ();
for (String FName: schema.sobjecttype.ccs.fields.getmap().keyset()) {
ObjectFieldList.add(FName.RemoveEnd('__c'));
}
if (!filelines.isEmpty()) {
FieldsInFile = filelines[0].toLowerCase().trim().split(',');
if (FieldsInFile.isEmpty()) {
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'No fields present in the file...!');
ApexPages.addMessage(errormsg);
//Exception : No fields present in the file
return null;
}
System.debug('**********************************' + ObjectFieldList.containsall(FieldsInFile));
System.debug('**********************************' + schema.sobjecttype.ccs.fields.getmap().keyset().containsall(FieldsInFile));
if (!(ObjectFieldList.containsall(FieldsInFile) || schema.sobjecttype.ccs.fields.getmap().keyset().containsall(FieldsInFile))) {
//Exception : Fields in File not correct - Please check spelling
return null;
}
}
for (Integer i = 1; i < filelines.size(); i++) {
ccs a = new ccs();
List < String > inputvalues = new List < String > ();
inputvalues = filelines[i].split(',');
for (Integer j = 0; j < FieldsInFile.size(); j++) {
if (FieldsInFile.get(j) == 'Service__c')
a.Type_of_Service__c = inputvalues[j];
if (FieldsInFile.get(j) == 'First_Name__c')
a.Member_First_Name__c = inputvalues[j];
if (FieldsInFile.get(j) == 'Street__c')
a.Street__c = inputvalues[j];
if (FieldsInFile.get(j) == 'City__c')
a.City__c = inputvalues[j];
if (FieldsInFile.get(j) == 'PO__c')
a.PO__c = inputvalues[j];
}
ccsupload.add(a);
}
//try{
insert ccsupload;
//}
/*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);
}*/
}
return null;
}
public List < ccs > getuploadedccs() {
if (ccsupload != NULL)
if (ccsupload.size() > 0)
return ccsupload;
else
return null;
else
return null;
}
}
page#
<apex:page sidebar="false" controller="FileUploader">
<style>
.headerRow {
background:#D0D0D0!important ;
}
</style>
<apex:form >
<apex:sectionHeader title="Upload data" />
<apex:pagemessages />
<apex:pageBlock >
<center>
<apex:inputFile value="{!contentFile}" filename="{!nameFile}" />
<apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;" />
<apex:commandButton action="{!refresh}" value="Reset" id="Button" style="width:70px;" />
<br/>
<br/> <font color="red"> <b>Note: Please use the standard CSV template to upload Data. </b> </font>
</center>
<apex:pageblocktable value="{!getuploadedccs}" var="acc" rendered="{!NOT(ISNULL(getuploadedccs))}" style="background-color:#00FFFF;">
<apex:column headerValue="Record id" width="30%" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.Id}" />
<a href="/{!acc.Id}" id="{!acc.Id}" target="_blank" location="top" fullscreen="no" onblur="LookupHoverDetail.getHover('{!acc.Id}').hide();"
onfocus="LookupHoverDetail.getHover('{!Acc.Id}', '/{!Acc.Id}/m?retURL=%2F{!Acc.Id}&isAjaxRequest=1').show();"
onmouseout="LookupHoverDetail.getHover('{!acc.Id}').hide();"
onmouseover="LookupHoverDetail.getHover('{!acc.Id}', '/{!acc.Id}/m?retURL=%2F{!acc.Id}&isAjaxRequest=1').show();">View</a><br/>
<a href="/{!acc.Id}/e?retURL=%2F{!Acc.Id}" id="{!acc.Id}" target="_blank" location="top" fullscreen="no" onblur="LookupHoverDetail.getHover('{!acc.Id}').hide();">Edit</a><br/>
</apex:column>
<apex:column headerValue="Member First Name" width="30%" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.First_Name__c}" />
</apex:column>
<apex:column headerValue="Type of Service" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.Service__c}" />
</apex:column>
<apex:column headerValue="Street" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.Street__c}" />
</apex:column>
<apex:column headerValue="City" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.City__c}" />
</apex:column>
<apex:column headerValue="PO" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.PO__c}" />
</apex:column>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>
public class FileUploader {
public string nameFile {
get;
set;
}
public Blob contentFile {
get;
set;
}
public List < String > FieldsInFile {
get;
set;
}
String[] filelines = new String[] {};
List < ccs > ccsupload;
public Pagereference ReadFile() {
if(contentFile==Null)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Invalid template or file while processing your records...!');
ApexPages.addMessage(errormsg);
}
else
{
FieldsInFile = new List < String > ();
nameFile = contentFile.toString();
filelines = nameFile.split('\n');
ccsupload = new List < ccs > ();
//List<String> FirstLine = new List<String>();
//FirstLine = filelines[0].split(,);
Set < String > ObjectFieldList = new Set < String > ();
for (String FName: schema.sobjecttype.ccs.fields.getmap().keyset()) {
ObjectFieldList.add(FName.RemoveEnd('__c'));
}
if (!filelines.isEmpty()) {
FieldsInFile = filelines[0].toLowerCase().trim().split(',');
if (FieldsInFile.isEmpty()) {
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'No fields present in the file...!');
ApexPages.addMessage(errormsg);
//Exception : No fields present in the file
return null;
}
System.debug('**********************************' + ObjectFieldList.containsall(FieldsInFile));
System.debug('**********************************' + schema.sobjecttype.ccs.fields.getmap().keyset().containsall(FieldsInFile));
if (!(ObjectFieldList.containsall(FieldsInFile) || schema.sobjecttype.ccs.fields.getmap().keyset().containsall(FieldsInFile))) {
//Exception : Fields in File not correct - Please check spelling
return null;
}
}
for (Integer i = 1; i < filelines.size(); i++) {
ccs a = new ccs();
List < String > inputvalues = new List < String > ();
inputvalues = filelines[i].split(',');
for (Integer j = 0; j < FieldsInFile.size(); j++) {
if (FieldsInFile.get(j) == 'Service__c')
a.Type_of_Service__c = inputvalues[j];
if (FieldsInFile.get(j) == 'First_Name__c')
a.Member_First_Name__c = inputvalues[j];
if (FieldsInFile.get(j) == 'Street__c')
a.Street__c = inputvalues[j];
if (FieldsInFile.get(j) == 'City__c')
a.City__c = inputvalues[j];
if (FieldsInFile.get(j) == 'PO__c')
a.PO__c = inputvalues[j];
}
ccsupload.add(a);
}
//try{
insert ccsupload;
//}
/*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);
}*/
}
return null;
}
public List < ccs > getuploadedccs() {
if (ccsupload != NULL)
if (ccsupload.size() > 0)
return ccsupload;
else
return null;
else
return null;
}
}
page#
<apex:page sidebar="false" controller="FileUploader">
<style>
.headerRow {
background:#D0D0D0!important ;
}
</style>
<apex:form >
<apex:sectionHeader title="Upload data" />
<apex:pagemessages />
<apex:pageBlock >
<center>
<apex:inputFile value="{!contentFile}" filename="{!nameFile}" />
<apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;" />
<apex:commandButton action="{!refresh}" value="Reset" id="Button" style="width:70px;" />
<br/>
<br/> <font color="red"> <b>Note: Please use the standard CSV template to upload Data. </b> </font>
</center>
<apex:pageblocktable value="{!getuploadedccs}" var="acc" rendered="{!NOT(ISNULL(getuploadedccs))}" style="background-color:#00FFFF;">
<apex:column headerValue="Record id" width="30%" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.Id}" />
<a href="/{!acc.Id}" id="{!acc.Id}" target="_blank" location="top" fullscreen="no" onblur="LookupHoverDetail.getHover('{!acc.Id}').hide();"
onfocus="LookupHoverDetail.getHover('{!Acc.Id}', '/{!Acc.Id}/m?retURL=%2F{!Acc.Id}&isAjaxRequest=1').show();"
onmouseout="LookupHoverDetail.getHover('{!acc.Id}').hide();"
onmouseover="LookupHoverDetail.getHover('{!acc.Id}', '/{!acc.Id}/m?retURL=%2F{!acc.Id}&isAjaxRequest=1').show();">View</a><br/>
<a href="/{!acc.Id}/e?retURL=%2F{!Acc.Id}" id="{!acc.Id}" target="_blank" location="top" fullscreen="no" onblur="LookupHoverDetail.getHover('{!acc.Id}').hide();">Edit</a><br/>
</apex:column>
<apex:column headerValue="Member First Name" width="30%" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.First_Name__c}" />
</apex:column>
<apex:column headerValue="Type of Service" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.Service__c}" />
</apex:column>
<apex:column headerValue="Street" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.Street__c}" />
</apex:column>
<apex:column headerValue="City" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.City__c}" />
</apex:column>
<apex:column headerValue="PO" style="background-color:#00FFFF;">
<apex:outputField value="{!acc.PO__c}" />
</apex:column>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>