web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

how to create an entity record by using object initializer

(0) ShareShare
ReportReport
Posted on by 13,758

Hi Experts,

I would like know if its possible to create an entity record in late bound using object initializer. I know its possible in early bound but cant seem to find a way in late bound.
just like a query expression I can do this:

var queryExp = new QueryExpression("entityname")
{
ColumnSet = new ColumnSet("fieldname"),
Criteria =
{
Filters =
{
new FilterExpression
{
FilterOperator = LogicalOperator.And, Conditions =
{
new ConditionExpression("new_name", ConditionOperator.Equal, "OPP")
}

}
}
}

};

var result = service.RetrieveMultiple(queryExp);

rather than :

var queryExp = new QueryExpression("entityname");
queryExp.ColumnSet = new ColumnSet("fieldname");
queryExp.Criteria.AddCondition("new_name", ConditionOperator.Equal, "OPP");
var result = service.RetrieveMultiple(queryExp);

 I want to do same thing when creating an entity like :

var entity = new entity("entityname)

{
//not sure how to assign values to entity attributes here

};

service.Update(entity);

*This post is locked for comments

I have the same question (0)
  • Sreevalli Profile Picture
    3,264 on at

    Hi,

    you should use service.create not update. see the below sample code for different data types.

     Entity accObj = new Entity("account");
                accObj["name"] = name; //string
                accObj["fax"] = "+46 012332155";
                accObj["telephone1"] = "+46 (0) 75673456"; //phone number
                accObj["revenue"] = new Money(Convert.ToDecimal(18000.50)); //decimal
                accObj["numberofemployees"] = 25; //whole number
                accObj["websiteurl"] = "http://xxx.se/"; //url
                accObj["customertypecode"] = new OptionSetValue(3); //optionset
                if (conId != Guid.Empty)
                    accObj["primarycontactid"] = new EntityReference("contact", conId); // lookup - contact reference
                if (accId != Guid.Empty)
                    accObj["parentaccountid"] = new EntityReference("account", accId); // lookup - account reference
                Guid accerecId = service.Create(accObj);
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Thanks for your response.

    I can very well update the entity too if i assign the ID of an existing record to this new record.

    My question is slightly different. I have showed two ways of initializing a query expression. same way I wanted to know for entity creation or updation.

    The method you show is what we and everyone uses.

    i think i am essentially talking about a "collection initializer" rather than traditional object initializer. If thats possible here with latebound because with early bound i can easily do something like.

    var entity = new Entity(entityname)

    {

    new_name = "name";

    new_field1 = 1;

    etc etc

    };

    but cant seem to find a way doing it using late bound. Hope this makes sense now.

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    It is not possible to use collection initializer with Entity (late bound). Check the below error message-

    collection.png

    Hope this helps.

  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Ravi,

    This is what I thought. Thanks for looking into this.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans