Uploading a document or file to Salesforce

I have done a small POC for uploading a file through Salesforce API. 
C#
binding = new SforceService();
 binding.Timeout = 60000;
 LoginResult lr;

            try
            {
                Console.WriteLine("LOGGING IN NOW...");
                lr = binding.login(username, password);
            }
            catch (SoapException e)
            {

            }


 private void uploadFile()
        {

            QueryResult qr = null;
            binding.QueryOptionsValue = new QueryOptions();
            binding.QueryOptionsValue.batchSize = 250;
            binding.QueryOptionsValue.batchSizeSpecified = true;
                        
            Folder rF=null;           
            try
            {

                qr = binding.query("SELECT AccessType, Id, DeveloperName, Name, NamespacePrefix, IsReadonly, Type FROM Folder where DeveloperName='WSDLUpload'");
                if (qr.size > 0)
                {
                    Console.WriteLine("Number of folders fetched::" + qr.records.Length);
                    rF=(Folder)qr.records[0];
                }
                Document d = new Document();
                d.Description = "Test WSDL";
                d.ContentType = ".csv";
                //REQUIRED FIELD
                d.DeveloperName = "DOC" +                                       DateTime.Now.ToString("ddMMMyyyyHHmmss");

                d.FolderId = rF.Id;
                d.IsBodySearchable = true;
                d.IsInternalUseOnly = true;
                d.IsPublic = false;
//REQUIRED
                d.Name = "Doc" + DateTime.Now.ToString("ddMMMyyyyHHmmss");
                Console.WriteLine(d.DeveloperName);
                d.Type = "csv";

                UnicodeEncoding unic = new UnicodeEncoding();

                // content of the file -REQUIRED FIELD
                d.Body = unic.GetBytes("TESTTEST");

                sObject[] docs = new Document[1];
                docs[0] = d;

                SaveResult[] sResults=binding.create(docs);

                for (int i = 0; i < sResults.Length; i++)
                {
                    if (sResults[i].success)
                    {
                        // No errors, so retrieve the Id created for this record 

                        Console.WriteLine("A Document was created with Id: {0}",
                            sResults[i].id);
                    }
                    else
                    {
                        Console.WriteLine("Item {0} had an error updating", i);

                        // Handle the errors 

                        foreach (Error error in sResults[i].errors)
                        {
                            Console.WriteLine("Error code is: {0}",
                                error.statusCode.ToString());
                            Console.WriteLine("Error message: {0}", error.message);
                        }
                    }

                }
            }
            catch (Exception ex)
            {

                Console.WriteLine("\nFailed to execute query succesfully," +

                    "error message was: \n{0}", ex.Message);

            }
            
            Console.WriteLine("\n\nHit enter to continue...");

            Console.ReadLine();
        }

Comments

Popular posts from this blog

Updating SourceData/ Data Source of the Pivot Table

Salesforce.com migration tool - Deploying Weblink and migrating files with special characters

Information Architecture - Setup your term store to scale