Skip to main content

Notifications

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

  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at
    RE: how to create an entity record by using object initializer

    Hi Ravi,

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

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: how to create an entity record by using object initializer

    Hi,

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

    collection.png

    Hope this helps.

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at
    RE: how to create an entity record by using object initializer

    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.

  • Sreevalli Profile Picture
    3,256 on at
    RE: how to create an entity record by using object initializer

    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);

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans