Batch Update SObject in Salesforce.Com (VB.Net)

salesforce dice

I’m sharing this code because I know a lot of new salesforce developers are pulling their hair out looking for concrete samples on how to do API batch update. Yes it took me awhile to be able to solve this problem. APEX API documentation doesn’t give it all and it’s frustrating…

So much for that, here’s how I did it. My sample is updating a custom field in Opportunity Object using VB.Net.
[ad#468×60]

Dim arrUpdateID As New ArrayList  'This ArrayList holds the Opportunity IDs I need to update, I didn't included the query - that will be your task

Dim objOppx(199) As Opportunity

For d = d To arrUpdateID.Count - 1

Dim updateOppx As New Opportunity

updateOppx.Id = arrUpdateID(d) ' You need an ID when you update
updateOppx.Downloaded__c = True ' This is a checkbox in SFDC where I'm setting it to true
updateOppx.Downloaded__cSpecified = True ' This is required when updating a boolean field
objOppx(d) = updateOppx ' Storing opportunity values to  objOppx which is an array of Oppty

Next

Dim sr() As SaveResult = sfdc.update(objOppx) ' This is where the updating happens, this line of code should be outside the loop

For j As Integer = 0 To sr.Length - 1
Dim msgerr As String

If Not sr(j).success Then
msgerr = "Error updating Opportunity ID: " + sr(j).id + ". The error was: " _
+ sr(j).errors(0).message
End If

Next

Note: You can only update 200 records in an Object at a time due to Governor limits, so you need to make some work around with that issue. I have done that through chunking up my array of IDs to update. I’ll leave that up to you! 🙂

I hope this post helps! There will be more to come as I learn Salesforce.com and APEX programming.

Stay tuned!

Join the Conversation

2 Comments

  1. Thank you for your update on the maintenance exam. I hope you also update for future exams. It really helped. What resources have you used to strengthen your Admin skills? Also, to learn Apex and Java? That info would really help.

    Blessings

  2. Hi GG,

    Thanks for dropping a comment. Apologies for replying just now. I will try to post once I’ve finished taking the maintenance exam. 😉 Salesforce.com did a great job in their Help and Training, they have videos and slide decks. Just keep on Googling. You can setup your own developer org to start learning APEX. I don’t really read a lot nowadays, haha! I just research and I get to know new thing in SF when it’s needed for my job. 😀

Leave a comment

Your email address will not be published. Required fields are marked *