Richard Dewhirst
Richard Dewhirst

Reputation: 229

CRM: plugin error - onCreate (<Message>Expected non-empty Guid.</Message>)

i have an issue: the error...` when creating a new record

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Expected non-empty Guid.Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220989</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>Expected non-empty Guid.</Message>
  <Timestamp>2013-01-31T09:23:30.4406111Z</Timestamp>
  <InnerFault i:nil="true" />
  <TraceText>

The Code...` sets the fields to values. i have also tried

Pline.Id = new Guid();

Pline.Id = a; //(a is a guid ref to parent record)


Entity Pline = new Entity("bc_packlines");
if (b.Attributes.ContainsKey("mins_sum"))
{
    BlockMins = ((Decimal)((AliasedValue)b["mins_sum"]).Value);
}

Pline.Attributes["bc_packlinesid"] = Pline.Id;
Pline.Attributes["bc_pack"] = a;
Pline.Attributes["bc_type"] = "948110004";
Pline.Attributes["bc_minutespurchased"] = BlockMins;
Pline.Id = a;
service.Create(Pline);

Any ideas please? Thanks

Upvotes: 0

Views: 3992

Answers (3)

Michael Philip
Michael Philip

Reputation: 81

you should not set the PK of your record, the CRM will create it so you need to remove this

line

 Pline.Id = a;

and to create a relationship to the parent entity you have its id and you write the following

code

Pline.Attributes["bc_packlinesid"]=new CrmEntityRefrence(Pline.EntityLogicalName,parentEntityID);

Upvotes: 0

Richard Dewhirst
Richard Dewhirst

Reputation: 229

Fixed this, 1st edit fetch statement

<attribute name='productid' groupby='true' alias='productid' />

Which is what each new bc_packlines is based on anyway: then ...

productid = ((Guid)((AliasedValue)b["productid"]).Value);

                        BlockMins = ((Decimal)((AliasedValue)b["mins_sum"]).Value);

                        Entity Pline = new Entity("bc_packlines");
                        Pline.Id = productid;

Thanks :) hope this helps someone

Upvotes: 0

glosrob
glosrob

Reputation: 6715

Some of that is a bit confusing, especially

Pline.Attributes["bc_packlinesid"] = Pline.Id;
// ...
Pline.Id = a;

You should not need to set the Id of your new entity explicitly so remove:

Pline.Id = a;

Edit: Also you say that a is the Guid to a parent record - so how/why would you set as the PK of another record? That won't work fullstop.

Secondly, in this line

Pline.Attributes["bc_packlinesid"] = Pline.Id;

You are setting the value of the field bc_packlinesid to an empty guid, as the Id of the record you are creating is not yet set. This means you are trying to get the entity to refer to itself? It doesn't sound right to me but if it is, you will need to do this as a second step, after

var newId = service.Create(PLine)

Something like

Pline.Attributes["bc_packlinesid"] = newId;
service.Update(PLine);

That is if that is even possible, I'm not sure if CRM will allow an entity to parent-child itself.

Upvotes: 1

Related Questions