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
Anuj.ShuklaAnuj.Shukla 

Help required in reading a text file

Hi,

 

I am writing an apex class where I have to refer certain pre-known values (I will run validation against these pre-known values). Initially I had thought to use a custom object with just one field and store as many object values as I can. Then I was planning to get those values using simple query against custom object.

 

But my customer insists that I should not be using a custom object. Rather, I should be using a static resource (some sort of text file with pre-known values) and I should read that text file using apex code. I have not done this thing before.

 

I know how to upload a text file as static resource but I have absolutely no idea of how would I read that text file in my apex code.

 

I have to read that file, get all the values that have been mentioned in text file (each individual entry  in new line) in a LIST.

I can take it from there. 

 

Just help in 'how to read static resource of text file' would be appreciated.

 

 

Thanks,

Anuj

sfdcfoxsfdcfox

Actually, a CustomSetting would be even better than a custom object. But since the client is always right, here's the solution:

 

 

StaticResource sr = [select id,body from StaticResource Where Name = 'myTextResource'];
String contents = sr.body.toString();
for(String line:contents.split('\n')) {
  // Do something with this config
}

Of course, if it is an XML file, you could instead use XML streams to read the file.

 

Anuj.ShuklaAnuj.Shukla

Thanks a ton.

That was very quick response and worked like a charm.

 

am sure, newbie in salesforce like me will get enormous help here.

 

I welcome myself to this community :-)

 

 

Thanks,

Anuj

sfdcfoxsfdcfox

We are a very mature community with many knowledgeable and helpful members. Welcome to the community, and I do hope that you learn a lot about the force.com platform and use it to your fullest advantage.

b-Forceb-Force

Anuj,

Please mark above solution as accepted , and help to community.

 

Thanks,

Bala

SudhiSudhi

Hi,

 

I have a similar requirement but little more complex. I have a static resource and within that I have number of xml files. I want to read a specific xml file from the static resource into an xmlDom object in apex. How can I do that?