Batch Apex and Scheduled Apex

March 29, 2012



This is one of the blog posts I found for interesting with respect to using both Batch Apex and Scheduled apex.


Database.executeBatch (part of apex batch) takes a parameter called scope which specifies the number of
records that should be passed into the execute method.
More info:

“Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000
records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions
of 200 records each. The Apex governor limits are reset for each transaction.”

Also, we can use Database.Stateful in the class definition to make the batch remember the static variables.

“If you specify Database.Stateful in the class definition, you can maintain state across these transactions. This is useful
for counting or summarizing records as they're processed. For example, suppose your job processed opportunity records. You
could define a method in execute to aggregate totals of the opportunity amounts as they were processed.”

I think by using them together, we can achieve what we need without hitting governor limits.

API

Uploading a document or file to Salesforce

March 02, 2012

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();
        }

Popular Posts

Twitter