Hot answers tagged

19

You have to set cache="true" and contentType="application/octet-stream instead of text/csv. <apex:page cache="true" contentType="application/octet-stream#{!fileName}.csv" showHeader="false" sidebar="false" standardStylesheets="false"> <apex:outputText value="{!input}" escape="false"/> </apex:page> The key pieces ...


16

Rob's link is interesting and worth considering, though it looks like you have to pull the report data by making HTTPS requests which apart from code involves setting up Remote Site Settings. Assuming the volume of data is reasonably small (so governor limits don't get hit) below is some simple Apex code that will produce a CSV string that can be adapted to ...


10

To see view state you need to enable View State in Development Mode for your user. For enabling View State in Development Mode follow the path given Personal Setup -> My Personal Information -> Personal Information Edit your personal information and check 'Show View State in Development Mode' checkbox is checked or not. If not then check it and save record....


8

You can get the contents of the report by using a PageReference: String reportText = new PageReference('/'+reportId+'?csv=1').getContent().toString(); From there, you can send the data to your third-party server: HttpRequest req = new HttpRequest(); req.setEndpoint(endpoint); req.setBody(reportText); req.setHeader('Content-Type','text/csv'); req....


7

A newline is most commonly denoted by the escape character \n and not /n However, with CSV's, you may also want to check for \r\n as it will depend on the Operating System that is saving the file. Difference between '\n' and '\r\n' string testString = 'Hello\nWorld'; system.debug(testString.substringAfter('\n')); 16:43:16:021 USER_DEBUG [2]|DEBUG|World


7

Creating a CSV file blob in Apex My recommendations: use Batch Apex, passing in the initial Lists as stateful variables using Database.Stateful, and then creating a CSV row out of each SObject during the Batch Apex execute method. In the finish method of your Batch Apex, send an email containing the generated CSV. Using Batch Apex to do the CSV row ...


7

Good question, I personally would not try to handle this in the client, it's to fragile an environment, better to upload this file and handle at the server side. Or consider adjusting your requirements / user expectations to fit Salesforce Data Loader tools, see my summary below. Client Side Client Library and Communications. You can find likely find a ...


7

Looks like you found the solution, but for other folks, I've seen this error occur in three different ways. Calling String.replace() on very large strings (somewhere around 500k from memory, but it varied based on heap usage). This is extra confusing since String.replace() doesn't support regexes. The solution is to chunk up the string into smaller chunks ...


7

Because your code is completely dynamic you need to use the loop and nested loop resulting in N x M executions (N = no. of rows, M = no. of fields) of your inner loop code and N executions of the outer loop code. I don't think that there's much efficiency to be gained, but a lot of your code is geared toward removing commas. Why don't you use a different ...


7

It looks like the CSV file has the dates in mm/dd/yyyy format rather than dd/mm/yyyy. Hence the invalid date exception with the 31st month. This will work: date parsedDate = date.parse('31/5/2013'); This will fail with "System.TypeException: Invalid date: 5/31/2013" date parsedDate = date.parse('5/31/2013'); From the Date.parse methods docs: ...


7

i am trying to create a csv and succeeded in that except with a new empty row has been added on the top of the csv. The following is the visualforce page code for the generation. You need to make sure there's no intervening space between the opening page tag and the next tag: <apex:page ...>{!output}</apex:page> Also, i need to use the ...


6

Best String Methods I've been wondering about your use of String.remove in your above code. This method is described as follows. Thus if you have identical lines in your CSV this will for sure not be what your expecting, which i assume was just to remove the last line read. Removes all occurrences of the specified substring and returns the String result....


6

If the Business_Partner_Identifier_Code__c entry is of type string you can use: String s = String.isBlank((String) record.get('Business_Partner_Identifier_Code__c')) ? '' : String.valueOf(record.get('Business_Partner_Identifier_Code__c')); (One problem with the code you posted is that String.valueOf(null) returns the non-empty string of '...


6

Try \n: myInput.split('\n'); If your input uses \r\n, that is another common option. myInput.split('\r\n');


6

That's not how CSV files work. Comma Separated Values means that the file contains tabular data values in plain text. Values only. No formatting. Note that the below solution is the only way to format cells, but my personal opinion is that it would be massively over-engineering the task at hand. If you really want the data to be formatted, you must ...


6

Using the example text from above, I was able to successfully import into a test data extension by using a delimiter of |. I was also able to successfully import by adding quotations to the field as text qualifiers and selecting the option for text qualifiers on the import.


5

Try this ImportCSVJob class as a reference implementation of Database.Batchable and Schedulable. The class demonstrates how you can use CSVReader to read CSV data, construct new records and then insert those records. Try testing your job with the simple Lead-import.csv data below: FirstName,LastName,Company Marty,Chang,Slalom Consulting Homer,Simpson,...


5

The problem is that you are trying to reference a static variable incorrectly. Change return new CSVIterator(m_csvFile, m_parser.crlf); to return new CSVIterator(m_csvFile, Parser.crlf); When you want to reference a static member you prefix it with the class name and do not use an instance of the class.


5

A common problem would be leading white space, try using trim() site.Estimated_Value__c = decimal.valueOf(inputsitevalues[9].trim());


5

Providing the CSV files are not too big, you can use a Visualforce apex:inputFile to upload the file and then apply this Parse a CSV with Apex sample code to grab the data from the rows. If the CSV files are large you will hit a governor limit such as the the 6M byte heap space or the 10 seconds of CPU or the 10,000 rows of DML.


5

OK, I have a plausible solution for Excel for Mac users. First, the options that I considered but rejected: Windows-1252 encoding - Doesn't support Asian languages UTF-16LE encoding - Creates user issues because if they edit the file I produce and Save, they lose the encoding. Generating an HTML table but declaring the output file as *.xls - Excel for ...


5

You're not going to be able to use XLSX at all. It's based on the Office Open XML standards from Microsoft. Basically, it's a set of XML files contained in a ZIP file, of which we have no native means of creating in Apex Code. Of course, you could build the constituent parts and then ship the data off to Heroku, AWS, etc, and have it finish packing up the ...


5

Basically, you should call a replaceAll beforehand: contents = contents.replaceAll('(\r\n|\r)','\n'); This code normalizes CRLF and CR to use just LF. Note that CSV may be in any of those three formats (CRLF, CR, or LF), depending on the source system (Windows, Mac, and Linux, respectively). Also note that the last line might be empty, depending on the ...


5

The regex too complicated error occurs when you reach 1,000,000 input sequences, which can occur even on incredibly small strings. For example, accord to the knowledge article: Pattern pat = Pattern.compile('(A)?(B)?(C)?(D)?(E)?(F)?(G)?(H)?(I)?(J)?(K)?(L)?(M)?(N)?(O)?(P)?(Q)?(R)?(S)?(T)?(U)?(V)?(W)?(X)?(Y)?(Z)?(AA)?(AB)?(AC)?(AD)?(AE)?(AF)?(AG)?(AH)?(AI)?(...


4

Use a RemoteAction method in Apex and then you can just hand the stringized blob directly to jQuery.


4

I got the below posts which exactly addresses the problem ... it might be useful to others facing the issue.... http://boards.developerforce.com/t5/Apex-Code-Development/Pattern-and-Matcher-Question/m-p/135986 http://boards.developerforce.com/t5/Apex-Code-Development/Regex-too-complicated-error-for-large-volume-of-data-and-a/m-p/178769


4

Here's a rather rough way of achieving what you want, though remember that if you need the fields in a certain order you're going to have trouble with splitting the string representation of the object in this way! String theObject = 'CustomObject__c: {field__c=value, field__c=value}'; theObject = theObject.subString(theObject.indexOf('=') + 1, theObject....


4

The 50k records returned by SOQL is a hard limit. I haven't heard of this particular limit being "black tab increased", but if the customer does not need much more than that it may be worth a try to contact their sales rep and ask. Failing that, if the export is native Apex, you'll have to break it up into 50k row chunks. It's possible that with HTML5/JS ...


4

Your problem is not with Salesforce or CSV, but with Excel. It doesn't handle CSV very well; it doesn't recognize the right encoding or, as far as I could find in a brief search, lets you choose the correct one. If you open your file in any regular text editor and set the encoding to UTF-8, you will see exactly what you expect. For handling UTF-8 CSV files ...


4

Once you save the file in excel in CSV Format, Open it in notepad and save it with encoding as UTF-8, Open it again in excel to see if character are appearing correctly. If Yes then load the file . Assuming that dataloader is used to load this data in, then ensure that utf8 is checked within the settings


Only top voted, non community-wiki answers of a minimum length are eligible