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

writing a csv file and attach to a custom object
hi ,
I am trying to do a custom solution for a scenario at our work place. I wrote a batch class to compare two objects and find the synchronization is working properly or not.
In this scenario in the batch class i am comparing the objects , if any object id is not equal to id of other object i need to capture that object information and write that into a csv file and save it as an attachment to a custom object .
can anybody guide me how to write to a csv file and attach it to a custom object. how can i write to a csv fiel and how can i format the data in the file as i want like the rows and columns.
cheers,
So assuming you have the information in a list of sobjects, you'd write to the string something like the following:
Is this the sort of thing you are after, or have I misunderstood?
All Answers
You'll need to create an Attachment in order to do this:
Write the contents of the file to a string (use \n for line seperator etc).
Create an Attachment sobject and set the body of the attachment to be the contents of the string.
Set the parent id of the attachment to the id of your object.
E.g., assuming your object is myObj and your string containing the file contents is 'myString', something like the following should do it.
thanks bob i understood the part of attachement my concern is about the results that i need to write to a file.
like for example i want to dispaly the results in this form
S.no Territoryname userid
1. abcd xyz
2. def 1234
I want the results in this format
how can i represent this in a string territoryname and userid are the fields in this case and i am fetching the results of search.
So assuming you have the information in a list of sobjects, you'd write to the string something like the following:
Is this the sort of thing you are after, or have I misunderstood?
hi bob,
thnaks for the reply i used your code and made some changes according to my org and when i am trying ti save i am geting an error saying
variable doesnot exist: myString
the code i used i am posting below.
hi solved the my string error now i am facing with the object error.
Illegal assignment from Schema.SObjectField to Id at line 81 column 11
Attachment att=new Attachment(Name='myfile.csv');
att.Body=Blob.valueOf(myString);
att.ParentId = errorlog__c.id;
insert att;
got it i forgot to instantiate the custom object.
errorlog__c myobj = new errorlog__c(); Attachment att=new Attachment(Name='myfile.csv'); att.Body=Blob.valueOf(myString); att.ParentId = myobj.id; insert att;