1

I'm trying to write an Apex class that queries data from an sObject and then writes that data to a csv file that is downloaded locally so that it can be uploaded to a big object. I'm adapting the approach from code that I found in another post which generates a csv string and then write it to a file. There is a section of the code that creates a blob and assigns that to the body of an Attachment object and then assigns sample values to the attributes of Name and parentId.

I've looked into where the value for parentId comes from but I haven't been able to find a clear answer. There isn't an attribute by that name of the object being queried so I'm not sure where the value is supposed to come from. What value should be assigned to the parentId?

The code for class is below:

List<Org_Snapshot__c> queryResults = [select id,Organization_Name__c, name from Org_Snapshot__c limit 10];
        //String tempQuery = reportObj.SOQL_Query__c.subString(0,reportObj.SOQL_Query__c.indexOfIgnoreCase('From'));
        List<String> queryFields = /*list of fields used in query exmaple -*/ new  List<String>{'id','name','Organization_Name__c'};
        List<String> csvValues = new List<String>();
        String csvFileString = ''; 
        for(String fieldName : queryFields)
        {
            csvFileString = csvFileString + ',' + fieldName.replace(',','');
        } 
        csvFileString = csvFileString.replaceFirst(',','') + '\n';

        String fileRow = '';
        String fieldValue = '';
        for(SObject obj : queryResults) //**THIS FOR LOOP IS CONSUMING MORE SCIRPT STATMENTS, IS THERE ANY WAY TO OPTIMIZE THIS ?**
        {
            fileRow = '';
            System.debug('===>' + obj); 

            for(String fieldName : queryFields)
            {
                fieldValue = '' + obj.get(fieldName);
                fieldValue = fieldValue.replace(',','');
                fileRow += ',' + fieldValue;
                System.debug('fieldValue: ' + fieldValue);
                csvValues.add(fileRow);
            }
            fileRow = fileRow.replaceFirst(',','');
            csvFileString +=  fileRow + '\n';
            //SObject a = c.getSObject('Account');// for parent objects fields like Account.Name
        }
        Blob csvBlob = Blob.valueOf(csvFileString);
        Attachment AttachmentObj = new Attachment(Body = csvBlob,
                                      Name = 'Test-' +System.now().format('yyyy-MMM-dd-hh-mm-ss') + '.csv',
                                      parentId = 'some id');
        System.debug('blob: '+ csvBlob);
        /*insert AttachmentObj;  */
        return csvValues; 
1

If you aren't saving it to the database via insert, and just using it as a container for some data, it doesn't matter.

If you are saving this record to the database, then the ParentId should be the related record's id. It'll show up under it in the "Notes & Attachments" section, assuming you set the id. Since your Org_Snapshot__c's seem to be custom objects, they probably support attachments, and you can use their id (or really, any id) for the ParentId.

  • I forgot to include the detail that I'm trying to generate and download a csv file so that it can be used to populate a big object. I don't actually need to insert the data back into a standard object but would instead need to find a way to insert the csv data into the big object. – loremIpsum1771 May 1 '18 at 17:09
  • I tried using "any id" (actually I used the id shown in the url for the custom object layout page of the sObject) as you suggested and got the error: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Parent ID: id value of incorrect type. What do you mean my any id? – loremIpsum1771 May 1 '18 at 18:35
  • Any id of an object which supports attachments, which should be all custom objects, unless you've created a custom object, somehow, which does not support attachments. See Attachment Reference – battery.cord May 1 '18 at 19:06
  • If you want to do this a few times a month, you're going to want to make a full fledged process out of this, complete with pages, tests, and lots of code. I don't think anyone else will be able to help you build the entire system, I think you should focus on specific issues. If you make an edit to this question, I'll take another look, but I need to move on to other responsibilities. – battery.cord May 1 '18 at 19:08

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.