web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Object reference not set to an instance of an object.

(0) ShareShare
ReportReport
Posted on by

Dear All,

I'm trying to insert couple of records for Entity person 

here is the code I wrote:

Entity account  = new Entity("contact");

            account.Attributes["firstname"] = "Test";

            account.Attributes["lastname"] = "Test";

            account.Attributes["datatel_prospectsourceid"] = "Beacardinal Website";

            account.Attributes["preferredcontactmethodcode"] = new OptionSetValue(2);

            account.Attributes["address1_telephone1"] = "3333333333";

But I keep getting this error message: "Object reference not set to an instance of an object." .

Any suggestions?

Regards

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    maybe there is a plugin on the create of the contact that is checking some values (like a lookup) that is missing

  • Community Member Profile Picture
    on at

    Thx for the answer . What is plugin? And where can I find it and what t do to change it ?

  • tw0sh3ds Profile Picture
    5,600 on at

    Hi,

    Paste here stacktrace fothe exception, error message like that can be caused by anything. Also paste the full code not some snippet. This is console application? How are you connecting to CRM?

  • Community Member Profile Picture
    on at

    Here is the code. I'm conecting successfully to CRM but when it comes to create new entry

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.ServiceModel.Description;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Crm.Sdk.Messages;

    using Microsoft.Xrm.Sdk.Query;

    namespace InfoRequest.Services

    {

       public class CRMInfoRequestService

       {

           private IOrganizationService CRMService() {

               System.Configuration.Configuration rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);

               string strOrganizationUri = System.Configuration.ConfigurationManager.AppSettings["strOrganizationUri"];

               string strUserName = System.Configuration.ConfigurationManager.AppSettings["username"];

               string strPassword = System.Configuration.ConfigurationManager.AppSettings["password"];

               var organizationUriIFD = new Uri(strOrganizationUri);

               var credentials = new ClientCredentials();

               credentials.UserName.UserName = strUserName;

               credentials.UserName.Password = strPassword;

               IServiceConfiguration<IOrganizationService> config = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(organizationUriIFD);

               IOrganizationService service;

               using (var serviceProxy = new OrganizationServiceProxy(config, credentials))

               {

                   // This statement is required to enable early-bound type support.

                   serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                   service = serviceProxy;

                   var who = new WhoAmIRequest();

                   //retreive user data from the server.

                   var whoResponse = (WhoAmIResponse)service.Execute(who);

                   System.Diagnostics.Debug.WriteLine(whoResponse.UserId);

                   return service;

               }

               // Get the ID's of the current user and business unit.

               //in case of failure

               return null;

           }

           public bool submitInoRequestFormDataToCRM() {

               var service = CRMService();

               if (service == null) return false;

               Entity account  = new Entity("contact");

               account.Attributes["firstname"] = this.first;

               account.Attributes["lastname"] = "Test3";

               account.Attributes["datatel_prospectsourceid"] = "Beacardinal Website";

               account.Attributes["preferredcontactmethodcode"] = new OptionSetValue(2);

       //        account.Attributes["address1_telephone1"] = "3333333333";

               try

               {

                   service.Create(account);

               }

               catch (Exception ex)

               {

                   System.Diagnostics.Debug.WriteLine(ex.Message);

                   return false;

               }

               Console.WriteLine("Done");

               return true;

           }

       }

    }

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at

    Hi mohammed,

    As Pawel mentioned, please provide the StackTrace.

    I noticed the first line is a bit strange.

    Entity account  = new Entity("contact");

    Which entity are you using? Account or Contact?  If it's Account record you are creating, change the code as below.

    Entity account  = new Entity("account");

    Also double check the attribute names you are using.  Maybe you have a typo.  Comment out the below three lines temporarily and just leave the mandatory fields and see if it works.

    account.Attributes["datatel_prospectsourceid"] = "Beacardinal Website";

    account.Attributes["preferredcontactmethodcode"] = new OptionSetValue(2);

    account.Attributes["address1_telephone1"] = "3333333333";

  • Community Member Profile Picture
    on at

    Thx for the answer, It's contact and I found the issue which is , we have afield called origin source whcih has this setup:

    Data type:lookup

    target record type:Source

    So hwo can I add valeu for such field?

  • Verified answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at

    Hi mohammed,

    It should be something like below.

    account.Attributes["originsource"] = new EntityReference("source", sourceId);

    Where:

    originsource = Name of the lookup

    source = Entity name of the Source entity

    sourceId = GUID of the Source record you are associating with contact.

  • Suggested answer
    Pramod M Profile Picture
    1,445 on at

    Hi Mohammed,

    In order to set the lookup you need to use EntityReference. Below please find the sample for lookup to contact. The field is primarycontactid.

    EntityReference primaryContactId = new EntityReference("contact", contactId);

    account["primarycontactid"] = primaryContactId;

  • Suggested answer
    tw0sh3ds Profile Picture
    5,600 on at

    Hi,

    The problem is not CRM related, it's C# related. This code:

    using (var serviceProxy = new OrganizationServiceProxy(config, credentials))
               {
                   // This statement is required to enable early-bound type support.
                   serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                   service = serviceProxy;
                   var who = new WhoAmIRequest();
                   //retreive user data from the server.
                   var whoResponse = (WhoAmIResponse)service.Execute(who);
                   System.Diagnostics.Debug.WriteLine(whoResponse.UserId);
                   return service;
               }


    makes no sense. You are creating serviceProxy in using {} block so it will be disposed when you exit using block. So returning this service makes no sense, you will be then trying to call a disposed object.

    If you want to use Using statement, you should wrap it around code that is using your service, so remove Using from CRMService method and do the Using statement inside submitInoRequestFormDataToCRM method.

    Also I'm not sure if this line is correct:

     account.Attributes["datatel_prospectsourceid"] = "Beacardinal Website";

    usually when the field is called like that it is EntityReference and you are assigning String to it. Assign EntityReference instead.

  • Community Member Profile Picture
    on at

    Thx fo the answer,

    There is a field for states and it's look up , so is there better way to just provide state value and it get guid instead of providing guid from code ?

    Thx alot

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans