update()

Updates one or more existing objects in your organization’s data.

Syntax

SaveResult[] = sfdc.update(sObject[] sObjects);

Usage

Use this call to update one or more existing objects, such as individual accounts or contacts, in your organization’s data. The update() call is analogous to the UPDATE statement in SQL.

Permissions

Your client application must be logged in with sufficient access rights to update() individual objects (as well as individual fields inside that object) within the specified object. For more information, see Factors that Affect Data Access.

Special Handling

Certain objects—and certain fields within those objects—require special handling or permissions. For example, you might also need permissions to access this object’s parent object. Before you attempt to update a particular object, be sure to read its description in the Standard Objects and in the Salesforce online help.

Updateable Objects

Certain objects cannot be updated via the API. To update an object via the update() call, its object must be configured as updateable (updateable is true). To determine whether an object can be updated, your client application can invoke the describeSObjects() call on the object and inspect its updateable property.

Required Fields

When updating required fields, you must supply a value—you cannot set the value to null. For more information, see Required Fields.

ID Fields

Fields whose names contain “Id” are either that object’s primary key (see ID Field Type ) or a foreign key (see Reference Field Type ). Client applications cannot update primary keys, but they can update foreign keys. For example, a client application can update the OwnerId of an Account, because OwnerID is a foreign key that refers to the user who owns the account record. Use describeSObjects() to confirm whether the field can be updated.

This call checks a batch for duplicate Id values, and if there are duplicates, the first five are processed. For the sixth and all additional duplicate Ids, the SaveResult for those entries is marked with an error similar to the following:
Maximum number of duplicate updates in one batch (5 allowed). 
Attempt to update Id more than once in this API call: number_of_attempts.

Automatically Updated Fields

The API updates certain fields automatically, such as LastModifiedDate, LastModifiedById, and SystemModstamp. You cannot explicitly specify these values in your update() call.

Resetting Values to null

To reset a field value to null, you add the field name to the fieldsToNull array in the sObject. You cannot set required fields (nillable is false) to null.

Valid Field Values

You must supply values that are valid for the field’s data type, such as integers (not alphabetic characters) for integer fields. In your client application, follow the data formatting rules specified for your programming language and development tool (your development tool will handle the appropriate mapping of data types in SOAP messages).

String Values

When storing values in string fields, the API trims any leading and trailing white space. For example, if the value of a name field is entered as "   ABC Company ", then the value is stored in the database as "ABC Company".

Assignment Rules

When updating Case or Lead objects, your client application can set AssignmentRuleHeader options to have the case or lead automatically assigned to one or more users based on assignment rules configured in the Salesforce user interface. For more information, see Case or Lead.

Maximum Number of Objects Created

Your client application can change up to 200 individual objects in a single update() call. If an update request exceeds 200 objects, then the entire operation fails.

update() and Foreign Keys

You can use external ID fields as a foreign key, allowing you to create, update, or upsert records in a single step instead of querying a record to get the ID first. To do this, specify the foreign key name and the external ID field value. For example:

Opportunity updateOpportunity = new Opportunity();
updateOpportunity.setStageName("Prospecting");
updateOpportunity.setId("006300000023YFu");
// Standard object ref
Account updateParentAccountRef = new Account();
updateParentAccountRef.setExternal_SAP1_ACCTID__c("SAP111111"); 
updateOpportunity.setAccount(updateParentAccount);
// call update
UpdateResult[] updateResults = binding.update(new SObject[] {updateOpportunity});

Basic Steps for Updating Objects

Use this process to update objects:
  1. Determine the ID of each object that you want to update(). For example, you might call query() to retrieve a set of objects (with their IDs), based on specific criteria, that you would want to update. If you know the ID of the object that you want to update, you can call retrieve() instead. For information on IDs, see ID Field Type.
  2. For each object, populate its fields with the data that you want to update.
  3. Construct an sObject[] array and populate that array with the objects that you want to update. All objects must be of the same object.
  4. Call update(), passing in the sObject[] array.
  5. Process the results in the SaveResult[] object to verify whether the objects have been successfully updated.

Sample Code—Java

public void updateAccountSample() {
  // Create an array of SObjects to send to the update method
  Account[] updates = new Account[2];
  // This account could also be from the results of a retrieve or query call
  Account updateAccount = new Account();
  updateAccount.setId("001x0000002kuk2AAA");
  updateAccount.setShippingPostalCode("94105");
  updates[0] = updateAccount;
  Account updateAccount2 = new Account();
  updateAccount2.setId("001x0000002kuk1AAA");
  updateAccount2.setWebsite("www.website.com");
  updates[1] = updateAccount2;
  // Invoke the update call and save the results
  try {
    SaveResult[] saveResults = binding.update(updates);
  }
  catch (Exception ex) {
    System.out.println("An unexpected error has occurred." + ex.getMessage());
    getUserInput ("\nUpdate failed. Press return to continue...");
    return;
  }
  getUserInput ("\nUpdate done.  Press return to continue...");
}

Sample Code—C#

using System;
using System.Collections.Generic;
using System.Text;
using SFDCWsdl.Salesforce_WSDL;

namespace SFDCWsdl
{
    public class SFDCWsdlTest
    {
        private SforceService binding;

        private static readonly string Username = "USERNAME";
        private static readonly string Password = "PASSWORD";

        /// <summary>
        /// Create the binding and login
        /// </summary>
        private SFDCWsdlTest()
        {
            this.binding = new SforceService();
            binding.login(SFDCWsdlTest.Username, SFDCWsdlTest.Password);
        }
        private void UpdateAccountSample()
        {
            //create the account object to hold our changes
            sObject updateAccount = new sObject();
            //need to have the id so that API knows which account to update

            //set a new value for the name property
            updateAccount.Id = "00130000001dmJT";
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            System.Xml.XmlElement nameElement = doc.GetElementById("Name");
            nameElement.InnerText = "New Account Name from Update Sample";

            updateAccount.Any = new System.Xml.XmlElement[] { nameElement };
            updateAccount.type = "Account";

            //call the update passing an array of object
            SaveResult[] saveResults = binding.update(new sObject[] { updateAccount });

            //loop through the results, checking for errors
            for (int j = 0; j < saveResults.Length; j++)
            {
                Console.WriteLine("Item: " + j);
                if (saveResults[j].success)
                    Console.WriteLine("An account with an id of: " + saveResults[j].id +
                          " was updated.\n");
                else
                {
                    Console.WriteLine("Item " + j.ToString() + " had an error updating.");
                    Console.WriteLine("    The error reported was: " +
                             saveResults[j].errors[0].message + "\n");
                }
            }
        }
    }
}

Arguments

Name Type Description
sObjects sObject[] Array of one or more objects (maximum of 200) to update.

Response

SaveResult[]

Faults

InvalidSObjectFault

UnexpectedErrorFault

See Also:
API Call Basics
https://wiki.apexdevnet.com/index.php/Sample_SOAP_Messages
© Copyright 2000-2008 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.