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

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.


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)')];                 
            rowCount = 0;
            colCount = 0;
            if (contentFile != null){
                String fileString = contentFile.toString();
                parsedCSV = parseCSV(fileString, false);
                 rowCount = parsedCSV.size();
                List<Sales_Claim__c> recs=new List<Sales_Claim__c>();
                Integer rowCount=0;
                 for (List<String> row : parsedCSV)
                   Sales_Claim__c rec=new  Sales_Claim__c();
                   for (String col : row)
                     rec.put('Reseller__c', row[0]);
                     rec.put('Quarter__c', row[1]);
                     rec.put('Status__c', row[2]);
                     insert recs;
                     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Successfully Uploaded' );
                     //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",');
            contents = contents.replaceAll('""','DBLQT');
            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('"')) {
                    } else if (field.startsWith('"')) {
                        makeCompositeField = true;
                        compositeField = field;
                    } else if (field.endsWith('"')) {
                        compositeField += ',' + field;
                        makeCompositeField = false;
                    } else if (makeCompositeField) {
                        compositeField +=  ',' + field;
                    } else {
            if (skipHeaders) allFields.remove(0);
            return allFields;




<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: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">
                   <apex:repeat value="{!row}" var="cell">
                       <td> {!cell} </td>


I think what you done is correct.


I guess You just need to replace the  <apex:Message/> with <apex:pageMessages />