IOrganizationService.Update Method

Applies To: Microsoft Dynamics CRM 2013, Microsoft Dynamics CRM Online

Updates an existing record.

Namespace: Microsoft.Xrm.Sdk
Assembly: Microsoft.Xrm.Sdk (in Microsoft.Xrm.Sdk.dll)

Syntax

'Declaration
<FaultContractAttribute(GetType(OrganizationServiceFault))> _
<OperationContractAttribute> _
Sub Update ( _
    entity As Entity _
)
[FaultContractAttribute(typeof(OrganizationServiceFault))] 
[OperationContractAttribute] 
void Update (
    Entity entity
)

Parameters

  • entity
    Type: Entity. An entity instance that has one or more properties set to be updated in the record.

Example

The following example shows how to use the Update method to update an account record (early bound). For this sample to work correctly, you must be connected to the server to instantiate an IOrganizationService interface. You can find the complete sample in the sample code package in the folder SampleCode\CS\GeneralProgramming\EarlyBound\CRUDOperations.cs.

// Connect to the Organization service. 
// The using statement assures that the service proxy will be properly disposed.
using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
{
    // This statement is required to enable early-bound type support.
    _serviceProxy.EnableProxyTypes();

    CreateRequiredRecords();

    // Instantiate an account object.
    // See the Entity Metadata topic in the SDK documentation to determine 
    // which attributes must be set for each entity.
    Account account = new Account { Name = "Fourth Coffee" };

    // Create an account record named Fourth Coffee.
    _accountId = _serviceProxy.Create(account);
    Console.Write("{0} {1} created, ", account.LogicalName, account.Name);

    // Retrieve the account containing several of its attributes.
    ColumnSet cols = new ColumnSet(
        new String[] { "name", "address1_postalcode", "lastusedincampaign", "versionnumber" });

    Account retrievedAccount = (Account)_serviceProxy.Retrieve("account", _accountId, cols);
    Console.Write("retrieved ");

    // Retrieve version number of the account. Shows BigInt attribute usage.
    long? versionNumber = retrievedAccount.VersionNumber;

    if (versionNumber != null)
        Console.WriteLine("version # {0}, ", versionNumber);

    // Update the postal code attribute.
    retrievedAccount.Address1_PostalCode = "98052";

    // The address 2 postal code was set accidentally, so set it to null.
    retrievedAccount.Address2_PostalCode = null;

    // Shows usage of option set (picklist) enumerations defined in OptionSets.cs.
    retrievedAccount.Address1_AddressTypeCode = new OptionSetValue((int)AccountAddress1_AddressTypeCode.Primary);
    retrievedAccount.Address1_ShippingMethodCode = new OptionSetValue((int)AccountAddress1_ShippingMethodCode.DHL);
    retrievedAccount.IndustryCode = new OptionSetValue((int)AccountIndustryCode.AgricultureandNonpetrolNaturalResourceExtraction);

    // Shows use of a Money value.
    retrievedAccount.Revenue = new Money(5000000);

    // Shows use of a Boolean value.
    retrievedAccount.CreditOnHold = false;

    // Shows use of EntityReference.
    retrievedAccount.ParentAccountId = new EntityReference(Account.EntityLogicalName, _parentAccountId);

    // Shows use of Memo attribute.
    retrievedAccount.Description = "Account for Fourth Coffee.";

    // Update the account record.
    _serviceProxy.Update(retrievedAccount);
    Console.WriteLine("and updated.");                    

    DeleteRequiredRecords(promptforDelete);
}

The following example shows how to use the Update method to update an account record (late bound). For this sample to work correctly, you must be connected to the server to instantiate an IOrganizationService interface. You can find the complete sample in the sample code package in the folder SampleCode\CS\GeneralProgramming\LateBound\CRUDOperationsDE.cs.

// Instaniate an account object.
Entity account = new Entity("account");

// Set the required attributes. For account, only the name is required. 
// See the Entity Metadata topic in the SDK documentatio to determine 
// which attributes must be set for each entity.
account["name"] = "Fourth Coffee";

// Create an account record named Fourth Coffee.
_accountId = _service.Create(account);

Console.Write("{0} {1} created, ", account.LogicalName, account.Attributes["name"]);

// Create a column set to define which attributes should be retrieved.
ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid" });

// Retrieve the account and its name and ownerid attributes.
account = _service.Retrieve(account.LogicalName, _accountId, attributes);
Console.Write("retrieved, ");

// Update the postal code attribute.
account["address1_postalcode"] = "98052";

// The address 2 postal code was set accidentally, so set it to null.
account["address2_postalcode"] = null;

// Shows use of Money.
account["revenue"] = new Money(5000000);

// Shows use of boolean.
account["creditonhold"] = false;

// Update the account.
_service.Update(account);
Console.WriteLine("and updated.");

// Delete the account.
bool deleteRecords = true;

if (promptForDelete)
{
    Console.WriteLine("\nDo you want these entity records deleted? (y/n) [y]: ");
    String answer = Console.ReadLine();

    deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y") || answer == String.Empty);
}

if (deleteRecords)
{
    _service.Delete("account", _accountId);

    Console.WriteLine("Entity record(s) have been deleted.");
}

Remarks

Message Availability

This message works regardless whether the caller is connected to the server or offline.

Not all entity types support this message offline. See Supported Entities later in this topic.

Privileges and Access Rights

To perform this action, the caller must have privileges on the entity and access rights on the record specified in the Update parameter.. For a list of the required privileges, see Update Privileges.

Notes for Callers

If the entity instance includes properties that are not valid for update, they are ignored. You can find this information in the metadata for your organization. See the preceding metadata browser information.

You can use this method to update any record of an entity that supports the Update message, including custom entities.

For more information about the exceptions that can be thrown when this method is called, see Handle Exceptions in Your Code.

Supported Entities

The following table shows the default entities that support this message. For the listed entities of this message, the Availability column shows Server if the caller must be connected to the server and shows Both if the caller can be either connected to, or disconnected from, the server.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Windows Server 2008, Windows Server 2012, Windows 7 (All Versions), Windows 8 (All Versions)

Target Platforms

Windows Server 2008, ,Windows Server 2012, ,Windows 7 (All Versions),

Change History

See Also

Reference

IOrganizationService Interface
IOrganizationService Members
Microsoft.Xrm.Sdk Namespace
UpdateRequest
UpdateResponse
Update

Other Resources

Handle Exceptions in Your Code
Troubleshooting and Error Handling

Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.