+ Start a Discussion
bca321bca321 

Apex loop Error

Hi

 

I have a text file. I tried to import text file data to salesforce custom object. One file has several pages. each page has same format. I tried to import one page for may object. It's working. Now I want to attach several pages for my text file and import all "RECEIPS" parts for my same object. I want to import only "RECEIPT" data for an object. I took "RECEIPTS" and "TOTAL " words as a flags. I tried search "RECEIPTS" word and if found it insert data. Again if found "TOTAL" word continue. If found "TOTAL" i want to go to loop again find "RECEIPTS" word and again insert data. How can I do that one.


I think we can search like this:  Boolean IsRECEIPTS = false;

Document d = [SELECT id, Body from Document where id = '01580000000Uxou'];
        Blob bodyBlob = d.Body;
        String bodyStr = bodyBlob.toString();
        String[]lines = bodyStr.split('\\n');
        //System.debug('lines' + lines);
        //Boolean matchFound = m.find();
        String searchfor = 'RECEIPTS';
        Boolean matchFound = false;
        String input ='';
        for (integer i = 0; i < lines.size(); i++) {
             input = lines[i];
            if (input.contains(searchfor)) {
                input = input.replace(searchfor,'');
             }
        
            //output += input + '<br />';               
            String regex = '\\s*(\\d\\d\\s\\w\\w\\w\\s\\d\\d)\\s*(\\d+)\\s*(\\d+)\\s*(\\d*\\,*\\d+.\\d*)\\s*(\\d+)\\s*(\\d*\\,*\\d+.\\d*)\\s*';
            Pattern p = Pattern.compile(regex);       
            Matcher m = p.matcher(input);
            matchFound = m.find();
            output += matchFound ;
         
         if(matchFound){       
                    pDueDate = m.group(1);
                    FileNumber = double.valueOf(m.group(2));                   
                    DebitItems = double.valueOf(m.group(3));
                    DebitAmount = decimal.valueOf(m.group(4).replace(',',''));
                    CreditItems = double.valueOf(m.group(5));
                    String aa = m.group(6).replace(',','');
                    CreditAmount = decimal.valueOf(aa.replace('"',''));   
                    output += pDueDate + ' ' + FileNumber + ' ' + DebitItems + ' ' + DebitAmount + ' ' + CreditItems + ' ' + CreditAmount;
                    Receipt__c newReceipt = new Receipt__c();
                    newReceipt.Due_Date_del__c = customDate(pDueDate);
                    newReceipt.File_Number__c = FileNumber;
                    newReceipt.Debit_Items__c = DebitItems;
                    newReceipt.Debit_Amount__c = DebitAmount;
                    newReceipt.Credit_Items__c = CreditItems;
                    newReceipt.Credit_Amount__c = CreditAmount;
                    insert newReceipt;
                     newReceipt = null;                    
                     output += '\n<br>';                       
                }//if
                           
              if(input.contains('TOTAL')){
                    continue;
                }
           
                }
               return output;   
            
                 }              
}