locked
Expected non-empty Guid RRS feed

  • Question

  • I am using WCF to communicate with an internal .Net program on one side and CRM 2011 on the other. The .Net program was originally passing individual fields as input paramters but we changed it to pass the Xrm classes. The intent is to create new contacts and other entity records programmatically.

    When the parms were individual fields the service worked when tested from the IDE.
    But now, when I try to create a new Contact, it is failing with: "Expected non-empty Guid". The InnerException is null.

    The only references I can find to this error online refer to workflows but I am not using workflows.

    Does anyone know why this error is coming up now or what I can do to track it down?

    My code looks like:

    try
                {
                    //Create a new Contact record:
                    gContactGuid = _service.Create(contact);
                }
                catch (Exception ex)
                {
                    clsHelperMethods.WriteToEventLog("Create Failed: " + ex.Message + " " + ex.InnerException, string.Empty, String.Empty);
                }

    Thank you.

    Friday, October 5, 2012 3:10 PM

Answers

All replies

  • You might try catching the Microsoft.Xrm.Sdk.OrganizationServiceFault

    SDK reference: Handle Exceptions in Your Code

    catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
    {
    }

    Jason Lattimer

    Saturday, October 6, 2012 3:01 AM
    Moderator
  • I ran into this recently, how are you creating the contact object?  I believe my issue was that I was using an empty guid as the Contact ID.  The Contact ID should either be null or set to Guid.NewGuid() if you need to explicitly set a guid.

    Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/ - Follow @bscarlav
    CRM 2011 JavaScript Model Generator - CRM 2011 Appender for log4net

    Monday, October 8, 2012 8:28 PM
  • Thanks for responding, Blake.  I have:

    Guid gNewContactGUID = new Guid();
    The contact's fields are being set at the source and are being passed to my web service as an entity record in a parameter. I am not explicitly settiing the Contact Id; that's generated by the CRM system upon Create.

    Please see my comments above, under Jason's reply.

    Thursday, October 11, 2012 9:34 PM
  • Thanks, Jason,

    I added the catch as you recommended.  The code did break with a Fault Exception but the error.message was just "Expected non-empty Guid" and there was no InnerException.

    A Watch on contact showed the contact.id = {00000000-0000-0000-0000-000000000000} so I tried to explicitly set contact.Id = new Guid(); right before the call to Create but the outcome was the same.


    Thursday, October 11, 2012 9:55 PM
  • That code is your problem:

    Guid gNewContactGUID = new Guid();


    You either need to do Guid.NewGuid() or just not set the GUID


    Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/ - Follow @bscarlav
    CRM 2011 JavaScript Model Generator - CRM 2011 Appender for log4net

    Thursday, October 11, 2012 10:29 PM
  • As Blake mentioned - when creating a new record, you really shouldn't have to worry about setting the ID unless you really need to have it a certain value (like you are mapping it up to another system).

    If you look at this sample from the SDK here - just to get an Account record created, only the absolute minimum values are provided that correspond to the default required fields for the entity.

    Account account = new Account { Name = "Fourth Coffee" };
    
    _accountId = _serviceProxy.Create(account);


    Jason Lattimer

    Friday, October 12, 2012 3:32 AM
    Moderator
  • Ok, I now have the following (simplified, somewhat) but I am getting a compilation error: "Use of unassigned local variable gNewContactGuid' on the" return".

    public Guid AddNewContact(Xrm.Contact contact) { Guid gNewContactGuid; try { //Create a new Contact record: gNewContactGuid = _service.Create(contact); } catch { //handle errors } return gNewContactGuid; }

    Friday, October 12, 2012 1:25 PM
  • Since you're not using the gNewContactGuid to assign to the Contact ID anymore, now it is fine to set it to an empty GUID.  Or you can return a nullable GUID and return null if the Contact doesn't get created due to an error.

    public Guid AddNewContact(Xrm.Contact contact) {
    
    Guid gNewContactGuid = new Guid();
    
    try
    {
        //Create a new Contact record:
        gNewContactGuid = _service.Create(contact);
    
    }
    catch
    {
         //handle errors
    }
    
    
    return gNewContactGuid;
    
    }

    OR this should work too:

    public Guid? AddNewContact(Xrm.Contact contact) {
    
    Guid gNewContactGuid = null;
    
    try
    {
        //Create a new Contact record:
        gNewContactGuid = _service.Create(contact);
    
    }
    catch
    {
         //handle errors
    }
    
    
    return gNewContactGuid;
    
    }



    Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/ - Follow @bscarlav
    CRM 2011 JavaScript Model Generator - CRM 2011 Appender for log4net

    Friday, October 12, 2012 1:40 PM
  • I reworked the code and got rid of the compilation error. Now it looks like this but when I invoke the service I am still throwing an error on the Create: "Expected non-empty Guid".

    public void AddNewContact(Xrm.Contact contact)
    {
        Guid gNewContactGuid = Guid.NewGuid();
    
        try
        {
             //Create a new Contact record:
             gNewContactGuid = _service.Create(contact);
    
             oDictionary.Add("gNewContactGuid", Convert.ToString(gNewContactGuid));
    
         }
         catch   (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
         {
             //handle error
         }
    
    
    
    }

    Can anyone help?

    Thanks.

    Friday, October 12, 2012 2:12 PM
  • So the contact ID must still be getting set to an empty GUID somewhere.  

    Try setting the contact ID to null before passing it to the Create method.

    Something like this (some letters might need capitalized):

    contact.contactid = null;


    Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/ - Follow @bscarlav
    CRM 2011 JavaScript Model Generator - CRM 2011 Appender for log4net

    Friday, October 12, 2012 2:17 PM
  • Blake and Jason, thank you for helping.

    Blake, I tried your first suggestion (although I had reworked the method so it's no longer returning the Guid) and the result was the same "Expected non-empty Guid" error.

    When I tried your 2nd suggestion I was unable to set:
    Guid gNewContactGuid = null;    The error was that a Guid is not nullable.  I am fairly new to C# so maybe I am missing something here.

    Jason, I wasn't trying to set the Id. I was just trying to instantiate a Guid to receive the result of the Create.  The contact record is being passed in with most of its attributes already set, so I can't do that very minimalist technique you showed me (and thank you for that link).

    Friday, October 12, 2012 2:25 PM
  • Yeah I think the problem is that the contact is being passed in with an empty GUID as the ID which will cause the error, therefore you would need to null out the ID or give it Guid.NewGuid()

    Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/ - Follow @bscarlav
    CRM 2011 JavaScript Model Generator - CRM 2011 Appender for log4net

    Friday, October 12, 2012 2:28 PM
  • I just added:

    contact.ContactId = null;

    right beofre the Create but this resulted in "Invalid type for entity id value" being thrown on the Create.

    Friday, October 12, 2012 2:31 PM
  • Success!

    I changed it to:

    contact.ContactId = Guid.NewGuid();

    and it all came together.  Thank you for helping!

    So, why do I need to set the ContactId at all?


    Friday, October 12, 2012 2:39 PM
  • You shouldn't need to, I'm not sure why it doesn't like the null ID.  I just know that it doesn't like an empty ID being explicitly set.

    Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/ - Follow @bscarlav
    CRM 2011 JavaScript Model Generator - CRM 2011 Appender for log4net

    Friday, October 12, 2012 2:56 PM
  • The interesting thing is I have another set of code that does something similar: a contact record is passed in to my method but then I unpack the fields and use them to populate a new contact object, which I use for the Create. I did this, an admittedly waste-of-time-process, because I couldn't get the method we;ve been working on to work.  And I never set the ContactId in that code, but the process works just fne.
    • Proposed as answer by AmyVomisa Tuesday, June 27, 2017 1:11 PM
    • Unproposed as answer by AmyVomisa Tuesday, June 27, 2017 1:11 PM
    Friday, October 12, 2012 3:07 PM
  • I had the same error with the incident creation in crm2015. While debugging, I found that was an attribute in the incident that have a empty guid (owner). Be certain to set all requiered attributes before creation.
    Tuesday, June 27, 2017 1:38 PM