locked
Null Value Exception RRS feed

  • Question

  • I have written to create a account in CRM 2013 Online version. It doesnt give any compile time error but gives a run time exception of null value. Below is the code.

     public void createaccount()
            {
                Uri organisationurl = new Uri("https://disco.crm5.dynamics.com/XRMServices/2011/Discovery.svc");
                ClientCredentials xyz = DeviceIdManager.LoadOrRegisterDevice();
                ClientCredentials cred = new ClientCredentials();
                cred.UserName.UserName = "*******";
                cred.UserName.Password = "*******";
                OrganizationServiceProxy serviceproxy = new OrganizationServiceProxy(organisationurl, null, cred, xyz);
                serviceproxy.Authenticate();
                serviceproxy.EnableProxyTypes();
                service = (IOrganizationService)serviceproxy;



                Guid acctid;
                Entity account = new Entity("account");
                if (account.Attributes.ContainsKey("name"))
                {
                    account.Attributes["name"] = "TCS";
                }

                acctid = service.Create(account);

                Console.Write("{0} {1} created, ", account.LogicalName, account.Attributes["name"]);


            }



            static void Main(string[] args)
            {
                Program obj = new Program();
               
                obj.createaccount();

            }

    Thursday, July 17, 2014 2:56 PM

Answers

All replies

  • It gives a error in Service.create(account) line.
    Thursday, July 17, 2014 3:00 PM
  • Hi,

    You're checking whether the attribute collection contains "name". Since this is a new Entity instance you're using, that won't be present. So your code doesn't add "TCS" as the account name. You need to remove the check condition.

    I have modified the code, this should hopefully work

    public void createaccount()
    {
    	Uri organisationurl = new Uri("https://disco.crm5.dynamics.com/XRMServices/2011/Discovery.svc");
    	ClientCredentials xyz = DeviceIdManager.LoadOrRegisterDevice();
    	ClientCredentials cred = new ClientCredentials();
    	cred.UserName.UserName = "*******";
    	cred.UserName.Password = "*******";
    	OrganizationServiceProxy serviceproxy = new OrganizationServiceProxy(organisationurl, null, cred, xyz);
    	serviceproxy.Authenticate();
    	serviceproxy.EnableProxyTypes();
    	service = (IOrganizationService)serviceproxy;
    
    	Guid acctid;
    	
    	Entity account = new Entity("account");
    	account.Attributes["name"] = "TCS";
    	
    	acctid = service.Create(account);
    
    	Console.Write("{0} {1} created, ", account.LogicalName, account.Attributes["name"]);
    }
    
    static void Main(string[] args)
    {
    	Program obj = new Program();
    	obj.createaccount();
    } 


    Admin QuikView Solution for CRM 2013

    Thursday, July 17, 2014 3:02 PM
  • Hi,

    Its still Giving 

    Value cannot be null.
    Parameter name: detail

    ArgumentNullException was unhandled.

    Thursday, July 17, 2014 3:08 PM
  • Hi,

    I just noticed the Organisation Service Url that you're using is wrong. You're using the Discovery Service Url. I believe you're using CRM Online. The Organisation Service Url will be something like :

    https://{yourorgname}.api.crm5.dynamics.com/XRMServices/2011/Organization.svc

    You can see this from Settings > Customizations > Developer Resources.


    Admin QuikView Solution for CRM 2013

    Thursday, July 17, 2014 3:26 PM
  • Thanks.

    As i am beginner to CRM development. Can you please tell me the difference between Organisation service url and discovery service url and what is the use of both?

    Thursday, July 17, 2014 3:35 PM
  • Hi,

    The Discovery Service, as the name implies, is the one used to discover Organizations. Say for example, when you're connecting to a CRM instance, and you do not know the Organization Name and Url you can use this service to discover it. For On Premise deployments, you can have multiple Organizations for a single CRM deployment, and a single user can have access to multiple Organizations. Using this service you can discover all the Organizations that he/she has access to, they Urls, etc. The "RetrieveOrganisationRequest" is used for this purpose. MSDN Article.

    The Organization Service is however different. You retrieve the Organization Service Url using Discovery Service Url. Once you have the Organization Service Url, you can use this to make calls to Dynamics CRM to fetch data and metadata. Say, when you're fetching an entity record or creating one, or even modifying some metadata or retrieving a list of all attributes of an entity; this is where Organization Service comes into play. MSDN Article.

    Dynamics CRM 2011 introduced another Service other than these 2, called the Organization Data Service which is basically the REST (OData) API. This facilitates querying for data from Dynamics CRM and performing basic CRUD operations from CRM. This API is particularly useful when you want to fetch data using jQuery and this REST API is at your disposal. MSDN Article.


    Admin QuikView Solution for CRM 2013


    Thursday, July 17, 2014 3:56 PM
  • Thanks a Lot.
    Thursday, July 17, 2014 4:15 PM