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
nagasnagas 

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, 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

So assuming you have the information in a list of sobjects, you'd write to the string something like the following:

 

 

String csvStr='S.No, TerritoryName, UserId';
for (MySObject__c sobj : mySobjList)
{
   csvStr+='\n' + sobj.no + ',' + sobj.Territory_Name__c + ',' + sobj.User_Id__c;
}

 Is this the sort of thing you are after, or have I misunderstood?

 

All Answers

bob_buzzardbob_buzzard

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.

 

 

Attachment att=new Attachment(Name='myfile.csv');
att.Body=Blob.valueOf(myString);
att.ParentId=myObj.id;
insert att;

 

 

nagasnagas

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.

 

bob_buzzardbob_buzzard

So assuming you have the information in a list of sobjects, you'd write to the string something like the following:

 

 

String csvStr='S.No, TerritoryName, UserId';
for (MySObject__c sobj : mySobjList)
{
   csvStr+='\n' + sobj.no + ',' + sobj.Territory_Name__c + ',' + sobj.User_Id__c;
}

 Is this the sort of thing you are after, or have I misunderstood?

 

This was selected as the best answer
nagasnagas

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.

 

for(Territory terr :stdTerritory.values())
        {
            if(ustr.containskey(terr.id))
            {
                if(!ushadow.containskey(terr.id))
                {
                    String myString='TerritoryName';
                  
                    myString+='\n'  + terr.name;
                      
                }
            }
            
        }
        
          Attachment att=new Attachment(Name='myfile.csv');
          att.Body=Blob.valueOf(myString);
          att.ParentId = errorlog__c.id;
          insert att; 

 

 

nagasnagas

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; 

 

 

 Attachment att=new Attachment(Name='myfile.csv');          

att.Body=Blob.valueOf(myString);          

att.ParentId = errorlog__c.id;        

 insert att; 

nagasnagas

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; 

errorlog__c myobj = new errorlog__c();          Attachment att=new Attachment(Name='myfile.csv');          att.Body=Blob.valueOf(myString);          att.ParentId = myobj.id;          insert att;