Friday, 24 March 2017

How upload large file in salesforce

Arpit Sharma


In Salesforce when we upload file using remote action then we are not able upload file more then 4.3 MB because above 4,3 MB we getting input too long error.so I found one solution in which we are able to upload file upto 25 MB In this approach we are using sforce api on visual force page to upload file on salesforce.below is code of uploading file upto 25 MB.

  1. <apex:page>
  2. <script type="text/javascript">
  3. var __sfdcSessionId = '{!GETSESSIONID()}';
  4. </script>
  5. <script src="/soap/ajax/34.0/connection.js" type="text/javascript"></script>
  6. <script>
  7. function uploadAttachmentFromVisualForcePage() {
  8. var reader = new FileReader();
  9. var attachFile = document.getElementById('idOfUploadInputFileElement').files[0];
  10. if (attachFile == undefined) {
  11. alert('Please select a file');
  12. return;
  13. }
  14. if (attachFile.size > 26214400) { //Where 26214400 is byte equivalent of 25MB
  15. alert('Attachment size not supported');
  16. }
  17. reader.onload = function(e) {
  18. var attachment = new sforce.SObject('Attachment');
  19. attachment.Name = attachFile.name;
  20. attachment.IsPrivate = false;
  21. attachment.ContentType = attachFile.type;
  22. attachment.Body = (new sforce.Base64Binary(e.target.result)).toString();;
  23. attachment.Description = attachFile.name;
  24. attachment.ParentId = 'recordID'; //Where recordID is the ID of record to which you want to add your attachment
  25. var result = sforce.connection.create([attachment]);
  26. if (result[0].getBoolean("success")) {
  27. alert('file uploaded successfully');
  28. } else {
  29. alert('something went wrong.');
  30. }
  31. };
  32. reader.readAsBinaryString(attachFile);
  33. }
  34. </script>
  35. <input type="file" id="idOfUploadInputFileElement" /><input type="button" value="Upload" onClick="uploadAttachmentFromVisualForcePage();" />
  36. </apex:page>

How to get list view id in apex class

Arpit Sharma
salesforce
Get list view id  using apex class in salesforce first we need to describe organization sobject by using below function

Step 1 - 

Schema.getGlobalDescribe() - This map contains organization object. map populate during run time based on permission. Key of map is sobject name and value is Sobject tokens.token is serializeable reference to sobject. 

Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();

Step 2-

Suppose if we want to describe Standard object account then we need to retrieve account token from map,If your org have namespace then you need to add namespace to custom object name because map contains object name with namespace as key.

Schema.SObjectType accInfo =  Schema.getGlobalDescribe().get( 'Account' );

Step 3-

Now we will describe Account object using getDescribe(), Return result is not serializeble, result contains all describe property of object or fields 

DescribeSObjectResult accDesc = accInfo.getDescribe();

Step 4-

Now we need to reterive object List view id  from descrbie sobject result.

String listViewId = accDesc.getKeyPrefix();