My plugin fire on Pre Create operation on Entity X. When trying to update a field on the Entity X using the following code I am getting error:
trEntity = (Entity)context.InputParameters["Target"];
trGuid = (Guid)trEntity.Id;
tr = (Entity)service.Retrieve("EntityX", trGuid,
new ColumnSet(new string[] { "field_a", "field_b" }));
tr["field_a"] = null;
service.Update(tr);
The error I am getting is:
Entity X with Id = 11505683-2292-b537-e311-143710e56fb7 Does Not Exist
Since you are in Pre-Create, the entity doesn't exist yet in the database.
You don't need to explicitly call Update in a Pre event. You can just update the Target entity (trEntity in your case) and the changes you make will be saved with the Create operation. The Target entity is the actual entity that is about to be created, so feel free to update fields directly on the Target in the Pre event.
trEntity = (Entity)context.InputParameters["Target"];
trEntity["field_a"] = null;
How are you creating your service?
This also happens when you try to update a record outside of the current transaction i.e. using a manually created OrganizationServiceProxy instead of using the one provided by IOrganizationServiceFactory.CreateOrganizationService.
Related
Is it possible to update a record's attribute of picklist type with null by using Sdk.Sync.Update? It's not working for me.
Here's what I do:
var detailsObj = updatedDetailsObj; // I get updatedDetailsObj from previous logic, not shown here
var operation = new Sdk.Entity("kcl_operation");
operation.setId(operationId, false); // I have operationId from previous logic, not shown here
operation.addAttribute(new Sdk.String("op_updatedAccount", detailsObj.UpdatedAccount)); // works, get updated
operation.addAttribute(new Sdk.OptionSet("op_updatedExplanation", null)); // doesn't get updated
Sdk.Sync.update(operation);
After the completion of Sdk.Sync.update, the string field get updated, but the picklist field is left with its previous value, instead of null.
I also took a look inside the XML being sent inside Sdk.Sync.update, and indeed, it lacks the pair of "op_updatedExplanation" and null.
How can make it work?
Added:
I'm not doing it inside a form but inside a grid page, so that the user checks several records and I need to make the update on all of them.
standard CRM SDK code (assuming entity name and field name):
Entity operation = new Entity("kcl_operation");
operation.Id = operationId;
operation["op_updatedexplanation"] = null;
service.Update(operation);
where service is an IOrganizationService instance
Please use this snippet to set value as null.
Xrm.Page.getAttribute("op_updatedexplanation").setValue(null);
This will just set the value in the form. You may have to save the form to see the value getting stored in database.
Xrm.Page.data.entity.save();
If the control is disabled - you have to set the submitmode attribute also.
Xrm.Page.getAttribute("op_updatedexplanation").setSubmitMode("always");
I'm using codeigniter 2.1.4 & datamapper orm. I know how to save relations to an object and I know how to delete them.
In this case I have a many-to-many relation, which I want to update with new values from a form. Now I can use this to save them which works just fine:
$ousergroupright = new Usergroupright;
$usergrouprights = $ousergroupright->where_in('id', $this->input->post('usergrouprights'))->get();
$ousergroup = new Usergroup;
$ousergroup->get_by_id($id);
$ousergroup->save($usergrouprights->all);
But this doesn't delete the records I "unchecked" in my form. I need to delete the objects I don't want related anymore. What would be the best way to do this (without using custom queries)?
A query like above with $ousergroup->where_not_in() before saving seems overkill to me (why query database and build objects just to delete a relation?):
$ousergroupright = new Usergroupright;
$usergrouprights = $ousergroupright->where_not_in('id', $this->input->post('usergrouprights'))->get();
$ousergroup = new Usergroup;
$ousergroup->get_by_id($id);
$ousergroup->delete($usergrouprights->all);
Any ideas?
There is a way to delete the objects you don't want related anymore without the need to query a database and build objects just to delete a relation. You can use the utility function query a run a custom SQL to delete all the unrelated objects WITHOUT needing to perform a database query, something like:
// Create usergroup object
$u = new Usergroup();
// your custom SQL query to delete the unrelated objects
$sql = "delete from usergrouprights where usergroup.id = ? and id usergrouprights.id not in (?)";
// Binding values
$binds = array($idUserGroup , $listOfSelectedItens);
// Run query to populate user object with the results
$u->query($sql);
More about DataMapper query utility function
http://datamapper.wanwizard.eu/pages/utility.html#query
In attempting to merge contacts in Microsoft CRM, I am using the following code -
//c1ID and c2ID are GUIDs of duplicated contacts.
EntityReference target = new EntityReference();
target.LogicalName = Contact.EntityLogicalName;
target.Id = c2ID;
MergeRequest merge = new MergeRequest();
// SubordinateId is the GUID of the account merging.
merge.SubordinateId = c1ID;
merge.Target = target;
merge.PerformParentingChecks = true;
Contact updater = new Contact();
Contact updater2 = new Contact();
updater = (Contact)xrmSvc.ContactSet.Where(c => c.ContactId.Equals(c1ID)).First();
updater2 = (Contact)xrmSvc.ContactSet.Where(c => c.ContactId.Equals(c2ID)).First();
MergeResponse mergedR = (MergeResponse)xrmSvc.Execute(merge);
When I try my Execute call here,I get this error -
Cannot specify child attributes in the columnset for Retrieve. Attribute: owneridname.
Am I not setting something correctly?
Having updatecontent does not change the issue. In fact, I get the error on lookups entered into the updatecontent. I find you have to build new entityreferences:
if (match.Contains("new_mostrecentcampaign"))
master["new_mostrecentcampaign"] =
new EntityReference(match.GetAttributeValue<EntityReference>("new_mostrecentcampaign").LogicalName
, match.GetAttributeValue<EntityReference>("new_mostrecentcampaign").Id);
...
Merge.UpdateContent = master
...
I realize this is a pretty old question, but for those of you who have run into the same issue in 2021 and beyond, here's the reason this error happens.
TL;DR: Ensure the EntityReference values for the attributes does not specify the Name property.
Explanation:
Everything that gets added to the Entity set to UpdateContent will be applied to the Target contact. When programmatically executing a MergeRequest within a plugin/workflow, the attributes of the UpdateContent get applied (as desired).
Where this breaks down is for EntityReference value types (lookups). The internal Microsoft code that performs this operation tries to interpret all properties of the EntityReference object, including Name.
So when the existing values from the SubordinateId contact are pulled using IOrganizationService.Retrieve (to dynamically get the latest version), the Name property is automatically set for those lookup attributes (the child record). This operation is not valid, even though it's not the user code that's directly executing it.
This brings us full circle to explain the original error:
Cannot specify child attributes in the columnset for Retrieve
I wish I had some documentation for this, but although the official documentation notes that the UpdateContent is optional, experience proves that it is in fact necessary. In the MergeRequests I've tested, I always include that property in the request, and there's a post in the MSDN forums for Dynamics 3.0 that suggests the same.
In fact, when I try to merge two contacts in my org without UpdateContent assigned, I actually get a FaultException saying the following:
Required field 'UpdateContent' is missing
Even though the documentation says it's optional!
So I'd suggest populating the UpdateContent property with something as in the below and see if that works:
var merge = new MergeRequest
{
// SubordinateId is the GUID of the account merging.
SubordinateId = c1ID,
Target = target,
PerformParentingChecks = true,
UpdateContent = new Contact()
};
I have created a new query like the following
var pressData = from press in dataContext.Releases
select new
{
Heading = press.Heading,
Description = press.Desc,
DatePublished = press.PublishDate.ToString(),
Body = press.BodyContent,
ID=press.ReleaseID,
CreatedBy=press.CreatedBy
};
Later in the code I want to update the entity from a session variable, but not save any data back to the database. Here is the code I am trying to accomplish this with....
var edit = pressData.Where(a => a.Heading == sectionPreview.HeadingContent && a.ID == sectionPreview.tionID).FirstOrDefault();
if (edit != null)
{
//WONT LET ME UPDATE THE Body VALUE
edit.Body = sectionPreview.SectionContent;
}
The code aboves purpose is to look at pressData and replace the body content with the new body from a session variable(not shown here), but NOT save it to the db. I want pressData to be filtered and updated only in the entity. So when I bind it to the control in this case it binds the data stored in my session.
this.rptSections.DataSource = pressData;
this.rptSections.DataBind();
I am getting a complier error stating
Property or indexer 'AnonymousType#1.Body' cannot be assigned to -- it is read only.
I checked the entity model and nothing is read only not any fields not anything. I must be missing something?
Anonymous Types encapsulate a read only property collection - for more information, read here. The compiler rewrites anonymous types as a constructor injections, ie:
select new
{
Heading = press.Heading,
Description = press.Desc,
DatePublished = press.PublishDate.ToString(),
Body = press.BodyContent,
ID=press.ReleaseID,
CreatedBy=press.CreatedBy
};
Is really rewritten as:
new Anonymous`1(press.Heading, press.Desc, press.PublishDate.ToString(), press.BodyContent, press.ReleaseID, press.CreatedBy)
And the properties are read only (public get, private / protected set, to use an easy comparison). If you want to solve your issue, instead of taking the data and making an anonymous object, create a real type and set properties on it.
I can not update the webform. To bee updated I have to push the save button two times. I´ve tried with DynamicEntity entity = (DynamicEntity)context.PreEntityImages["PreCalculate"]; DynamicEntity updateEntity = (DynamicEntity)context.InputParameters.Properties["Target"];
... updateEntity["new_totalsum"] = new CrmMoney(calculatedvalue); The problem is it will not bee updated the first time I push the save button. I have registred the image PreCalculate as Preimage and Message: Update; Eventing Pipeline Stage of Excecution: Pre Stage; Execution Mode : Syncronous
What´s wrong? Thanks
You need to write the modified record back into the Target when you're done, so your changed values will be used when writing the record to the database. At the end of your plugin's Execute() method, do
context.InputParameters[ParameterName.Target] = updateEntity;