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
Hi Ravi,
This is what I thought. Thanks for looking into this.
Hi,
It is not possible to use collection initializer with Entity (late bound). Check the below error message-
Hope this helps.
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.
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);
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
54
Victor Onyebuchi
6