2

Is there an example that shows how to search a text file attached by the user, I need to extract 2 or three pieces of information from the text file, I would need to check each line for the content. The text file could consist up to 100 lines, each piece of information is on a separate line.

For example, I need to find where an account name is located, unfortunately the account name is not always on a specific line. But the account name is always preceded by "Account name:" so I need to search where the "Account name:" text is, then extract the name that should proceed it. For example: "Account name: Joe Mayers" so I need to extract Joe Mayers and then save or search for that name in a custom object.

Currently the user uploads the text file through VF, Then the following code from in controller,

List<Document> d = [Select body, bodyLength, ContentType, Url from Document where Name ='TextFile']; 
          if(d.size()>0)
          {
          Blob b = d[0].body;
          Content = b.tostring();

Saves the file in the ‘Content’ string, So I need to search the ‘Content’ string, for the information.

8
  • In what context? Is this a file being parsed when uploaded on a VF page, a file already in the org as an Attachment to an sObject, or something else?
    – Mike Chale
    May 7 '13 at 16:26
  • its a text file that could consist up to 100 lines, each piece of information is on a separate line. For example, I need to find where an account name is located, unfortunately the account name is not always on a specific line. But the account name is always preceded by "Account name:" so I need to search where the "Account name:" text is, then extract the name that should proceed it. For example: "Account name: Joe Mayers" so I need to extract Joe Mayers and then save or search for that name in a custom object. May 7 '13 at 16:40
  • I've added this to your initial question, but how is the file being uploaded and searched? Does the user do that from a VF page? Is it a trigger from an attachment being added? Or something else?
    – Mike Chale
    May 7 '13 at 16:59
  • 1
    I would suggest regular expressions as a short answer.
    – Mike Chale
    May 7 '13 at 17:00
  • The user uploads the text file through VF, Then the following code List<Document> d = [Select body, bodyLength, ContentType, Url from Document where Name ='TextFile']; if(d.size()>0) { Blob b = d[0].body; Content = b.tostring(); Saves the file in the ‘Content’ string, So I need to search the ‘Content’ string May 7 '13 at 17:15
3

You'll need to implement you're own parser to find the content you're looking for. You can could do this quickly by splitting the content into lines and then inspecting each for your Account Name: token or whatever else you're using to find the data you need to pull out.

Document d = [select body from Document where name = 'TextFile];
// String.split() takes a regex and you'll need to escape backslash for it to
// be interpreted literally
for(String line : d.body.toString().split('\\n')) { 
  if(line.trim().startsWith('Account Name: ')) {
    // do whatever you want with the account name
  }
}

Another option is to use the Pattern and Matcher classes to extract the values with a regex. See How do I use regular expressions in Apex to extract data from a string?

8
  • I appreciate everyone's feedback, and i'm reading and trying to execute the code, starting with the guidelines above. But I'm having trouble executing each line of the code, to see what each variable is returning [normal troubleshooting]. I don't know how to do that online, so I installed Force.com IDE, but I don't think I can run the VisualForce Markup, so I cant get the text file as input into the code. How do developers normally troubleshoot in this situation. thanks May 9 '13 at 15:50
  • I’m getting the following error Error: newCheck_and_upload_test Compile Error: line breaks not allowed in string literals at line 52 column -1 List<Document> d = [Select body, bodyLength, ContentType from Document where Name = :documentId]; if(d.size()>0) { Blob b = d[0].body; Content = b.tostring(); system.debug(Content); Line 52 for(String line : Content.split('\\n)) { // if(line.trim().startsWith('AcountName') { // } } } not sure what’s wrong? May 9 '13 at 18:13
  • you've got a line break in your string :) May 9 '13 at 23:36
  • look at Content.split() you're not closing the string May 9 '13 at 23:36
  • Ok Ralph I got it, but also I need to close the bracket for the if statement, May 13 '13 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.