इसके माध्यम से साझा किया गया


CreateRequest Class

Definition

Contains the data that is needed to create a record.

public ref class CreateRequest sealed : Microsoft::Xrm::Sdk::OrganizationRequest
[System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")]
public sealed class CreateRequest : Microsoft.Xrm.Sdk.OrganizationRequest
[<System.Runtime.Serialization.DataContract(Namespace="http://schemas.microsoft.com/xrm/2011/Contracts")>]
type CreateRequest = class
    inherit OrganizationRequest
Public NotInheritable Class CreateRequest
Inherits OrganizationRequest
Inheritance
CreateRequest
Attributes

Examples

The following example shows how to use this message. For this sample to work correctly, you must be connected to the server to get an IOrganizationService interface instance.

/// <summary>
/// Demonstrates using CreateRequest with optional parameters
/// </summary>
/// <param name="service">The authenticated IOrganizationService instance</param>
static void CreateRequestExample(IOrganizationService service)
{
    //Use late bound Entity class with entity logical name
    Entity account = new("account")
    {
        Attributes = {
            //String primary name
            {"name","Contoso" },
            //Yes/No column
            {"creditonhold", false },
            // DateTime
            {"lastonholdtime", new DateTime(2023, 1, 1) },
            // Double
            {"address1_latitude",47.642311},
            {"address1_longitude",-122.136841},
            // Integer
            {"numberofemployees", 500 },
            // Money
            {"revenue",new Money(new decimal(5000000.00))},
            // Choice column: Preferred Customer
            {"accountcategorycode",new OptionSetValue(1)}
        }
    };

    //Create the account
    CreateRequest request = new()
    {
        Target = account
    };

    //Set optional parameters:

    // Expect error if duplicate record found
    request["SuppressDuplicateDetection"] = false;
    // Set a shared variable that a plug-in can access
    request["tag"] = "RecordCreatedBySampleCode";
    // Bypass plug-in logic if caller has prvBypassCustomPlugins privilege
    request["BypassCustomPluginExecution"] = true;
    // Don't trigger any flows for this operation
    request["SuppressCallbackRegistrationExpanderJob"] = true;

    try
    {
        var response = (CreateResponse)service.Execute(request);

        Console.WriteLine($"Created record ID: {response.id}");
    }
    catch (FaultException<OrganizationServiceFault> ex)
    {
        switch (ex.Detail.ErrorCode)
        {
            case -2147220685: // Duplicate record error
                Console.WriteLine(ex.Detail.Message);
// A record was not created or updated because a duplicate of the current record already exists.
                break;
            default:
                Console.WriteLine(ex.Detail.Message);
                break;
        }
    }
}

Sample code on GitHub

Use duplicate detection when creating and updating records

Remarks

Usage

Pass an instance of this class to the Execute(OrganizationRequest) method, which returns an instance of the CreateResponse class.

Privileges and Access Rights

To perform this action, the caller must have privileges on the table for the Entity set to the Target property. Generally, callers must have Read and Create privileges for the table. If the entity includes related records or lookup values, the caller will need the appropriate privileges for those records as well as Append and AppendTo privileges for the related records. Learn more about how to verify access in code

Notes for Callers

This class enables some operations not possible when using the Create(Entity) method. Some things you can do with this class:

For more information, see Use the CreateRequest class.

For user-owned tables, the caller becomes the owner for the new record by default unless you set the ownerid column for the record to the ID of another user or team.

Supported Tables

See Message support for tables for an example query you can use to get the list of tables you can use with the Create message.

Constructors

CreateRequest()

Initializes a new instance of the CreateRequest class.

Properties

ExtensionData

Gets or sets the structure that contains extra data. Optional.

(Inherited from OrganizationRequest)
Item[String]

Gets or sets the indexer for the Parameters collection.

(Inherited from OrganizationRequest)
Parameters

Gets or sets the collection of parameters for the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
RequestId

Gets or sets the ID of the request. Optional.

(Inherited from OrganizationRequest)
RequestName

Gets or sets the name of the request. Required, but is supplied by derived classes.

(Inherited from OrganizationRequest)
Target

Gets or sets an instance of an entity that you can use to create a new record. Required.

Applies to

See also