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

Notifications

Announcements

No record found.

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,256 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans