locked
Create a new Guid RRS feed

  • Question

  • Hi experts, I want to generate guid for an existing user programatically. how to do it?

    Please  provide some Solution

    Thank you


    saroj

    Tuesday, August 7, 2012 1:23 PM

Answers

  • Hi, you do not set the LogicalName of your EntityReference:

    CrmODataService.EntityReference er = new CrmODataService.EntityReference();
    Guid createdAddressGuid = Guid.NewGuid();
    er.Id = Guid.Parse(_uid);//_uid is the parentId getting from Htmlpage url
    er.LogicalName = "contact"; // or "account" if you use the code for setting account address 
    address.ParentId = er;
    // ...
    Hope this will help.


    • Edited by DPitois Wednesday, August 8, 2012 4:55 AM
    • Marked as answer by Saroj kumar Das Wednesday, August 8, 2012 5:12 AM
    Wednesday, August 8, 2012 4:53 AM

All replies

  • Guid.NewGuid() will give you a new unique Guid. Is that what you meant?

    Gonzalo | gonzaloruizcrm.blogspot.com

    Tuesday, August 7, 2012 1:25 PM
    Moderator
  • Guid.NewGuid();
    Tuesday, August 7, 2012 1:29 PM
  •  Hi Gonzalo, I am able to insert a new record into Contact entity but i am unable to do it in CustomerAddress entity. An exception is getting at Context.EndSaveChanges() method. And also i m not able to get the idea how to insert into customeraddress table as i have contact id as parent id.

    Please Provide some solution.

    below is my code

    ******************************************

           internal void CreateNewAddress(string name, string addline, string city, string state, string zip)

            {         
                // create contact record

                CustomerAddress address = new CustomerAddress();
                CrmODataService.EntityReference er = new CrmODataService.EntityReference();
                Guid createdAddressGuid = Guid.NewGuid(); 
               er.Id = Guid.Parse(_uid);//_uid is the parentId getting from Htmlpage url
                address.ParentId = er;
                address.CustomerAddressId = createdAddressGuid;
                address.ObjectTypeCode = "2";
                address.Name = name;
                address.Line1 = addline;
                address.City = city;
                address.StateOrProvince = state;
                address.PostalCode = zip;
                // add the new contact to the contact set
                MessageBox.Show("The New Address Id is  :'" +createdAddressGuid.ToString()+ "'");
                _crmODataContext.AddToCustomerAddressSet(address);
                // call BeginSaveChanges method and specify the callback method and the contact object
                _crmODataContext.BeginSaveChanges(CreateAddressHandler, address);

            }
            public void CreateAddressHandler(IAsyncResult result)
            {

                try
                {
                    // in the call back method call the EndSaveChanges method

                    // it returns DataServiceResponse object.

                    // we can get the error information from this object in case an operation fails
                    MessageBox.Show("At Try Block");
                    _crmODataContext.EndSaveChanges(result); // Here the Exception is getting
                    
                    if (result.IsCompleted)
                    {
                        // id of the created contact record
                        MessageBox.Show(result.IsCompleted.ToString());
                        Guid createdAddressGuid = ((CustomerAddress)result.AsyncState).CustomerAddressId;
                        MessageBox.Show(createdAddressGuid.ToString());
                        //CustomerAddress createdAddr = (CustomerAddress)result.AsyncState;
                        //MessageBox.Show("Created Address is  :'" + createdAddr.Name + "'");
                    }
                    else
                    {

                        MessageBox.Show("Operation Status is  :" + result.IsCompleted.ToString());
                    }

                }
                catch (Exception ee)
                {
                    MessageBox.Show("Operation Failed  :'" + ee.Message + "','"+"\n\n"+"''" +ee.StackTrace+"'");

                }
            }


    saroj

    Tuesday, August 7, 2012 2:03 PM
  • address.ObjectTypeCode = "2";

    try without this line

    Tuesday, August 7, 2012 2:35 PM
  • Hi Alexander I tried without writting the code "address.ObjectTypeCode="2" ". but it is throwing exception. That is ->

    An error occurred while processing the request at System.Data.Services.Client.SaveResult.HandleBatchResponse()

    at System.Data.Services.Client.SaveResult.EndRequest()

    at System.Data.Services.Client.DataServiceContext.EndSaveChanges(IAsyncResult asyncResult)

    at SilverLightApp.MainPage.CreateAddressHandler(IAsyncResult result)

    Please give some solution for it.

    The message in InnerException is "The CustomerAddress is Parented by niether Account nor Contact"

    Thank you for your response.


    saroj


    Wednesday, August 8, 2012 3:54 AM
  • Hi, you do not set the LogicalName of your EntityReference:

    CrmODataService.EntityReference er = new CrmODataService.EntityReference();
    Guid createdAddressGuid = Guid.NewGuid();
    er.Id = Guid.Parse(_uid);//_uid is the parentId getting from Htmlpage url
    er.LogicalName = "contact"; // or "account" if you use the code for setting account address 
    address.ParentId = er;
    // ...
    Hope this will help.


    • Edited by DPitois Wednesday, August 8, 2012 4:55 AM
    • Marked as answer by Saroj kumar Das Wednesday, August 8, 2012 5:12 AM
    Wednesday, August 8, 2012 4:53 AM
  • Thank you Very much. Its Worked. Thank you again.


    saroj

    Wednesday, August 8, 2012 5:12 AM