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

How can i do validation in apex as shown in the below?
HI,when i am uploading csv file in to custom object first it will check whether the data is existing r not in reseller__c object i.e based on the condition "Select Reseller_Region__c, Name, Id From Reseller__c where Reseller_category__c NOT IN('Distributor(T1)')' "If the condition is satisfied then it will update the records in Sales_Claim__c object.where i have to apply this condition in my code i dont know .kindly let me know the solution asap.
apex:
public with sharing class uploadCSVcontroller {
public String results { get; set; }
public Blob contentFile { get; set; }
public String nameFile { get; set; }
public Integer rowCount { get; set; }
public Integer colCount { get; set; }
public pagereference Results() {
List<List<String>> parsedCSV = new List<List<String>>();
//Reseller__c Res = [Select Reseller_Region__c, Name, Id From Reseller__c where Reseller_category__c NOT IN('Distributor(T1)')];
//System.debug('Res:'+Res);
rowCount = 0;
colCount = 0;
if (contentFile != null){
String fileString = contentFile.toString();
parsedCSV = parseCSV(fileString, false);
rowCount = parsedCSV.size();
System.debug('RowCount:'+rowCount);
List<Sales_Claim__c> recs=new List<Sales_Claim__c>();
//System.debug('RECS:'+recs);
Integer rowCount=0;
for (List<String> row : parsedCSV)
{
Sales_Claim__c rec=new Sales_Claim__c();
recs.add(rec);
System.debug('recs1:'+recs);
for (String col : row)
{
rec.put('Reseller__c', row[0]);
rec.put('Quarter__c', row[1]);
System.debug('QA:'+row[1]);
rec.put('Status__c', row[2]);
}
}
insert recs;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Successfully Uploaded' );
ApexPages.addMessage(myMsg);
//System.debug('Successfull Inserted:'+recs);
}
return null;
}
public static List<List<String>> parseCSV(String contents,Boolean skipHeaders) {
List<List<String>> allFields = new List<List<String>>();
contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');
//System.debug('contents:'+contents);
contents = contents.replaceAll('""','DBLQT');
System.debug('contents:'+contents);
List<String> lines = new List<String>();
try {
lines = contents.split('\r'); // using carriage return accomodates windows, unix, and mac files
} catch (System.ListException e) {
System.debug('Limits exceeded?' + e.getMessage());
}
Integer num = 0;
for(String line: lines) {
// check for blank CSV lines (only commas)
if (line.replaceAll(',','').trim().length() == 0) break;
List<String> fields = line.split(',');
List<String> cleanFields = new List<String>();
String compositeField;
Boolean makeCompositeField = false;
for(String field: fields) {
if (field.startsWith('"') && field.endsWith('"')) {
cleanFields.add(field.replaceAll('DBLQT','"'));
} else if (field.startsWith('"')) {
makeCompositeField = true;
compositeField = field;
} else if (field.endsWith('"')) {
compositeField += ',' + field;
cleanFields.add(compositeField.replaceAll('DBLQT','"'));
makeCompositeField = false;
} else if (makeCompositeField) {
compositeField += ',' + field;
} else {
cleanFields.add(field.replaceAll('DBLQT','"'));
}
}
allFields.add(cleanFields);
}
if (skipHeaders) allFields.remove(0);
System.debug('allFields:'+allFields);
return allFields;
}
}
Vpage:
<apex:page controller="uploadCSVcontroller" >
<apex:pageBlock >
<apex:message />
<apex:form >
<apex:inputFile value="{!contentFile}" filename="{!nameFile}" /><br/>
<apex:commandButton value="Upload" action="{!Results}" id="theButton"/>
</apex:form>
</apex:pageBlock>
<apex:outputPanel id="results">
<p>Filename: {!nameFile}</p>
<p>TotalRecords: {!rowCount}</p>
<table title="CSV Output" border="1" width="100%">
<apex:repeat value="{!results}" var="row">
<tr>
<apex:repeat value="{!row}" var="cell">
<td> {!cell} </td>
</apex:repeat>
</tr>
</apex:repeat>
</table>
</apex:outputPanel>
</apex:page>
I think what you done is correct.
I guess You just need to replace the <apex:Message/> with <apex:pageMessages />