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 365 | Integration, Dataverse...
Answered

System.NullReferenceException: 'Object reference not set to an instance of an object.'

(0) ShareShare
ReportReport
Posted on by 25

I am pulling a set of records to use the CrmServiceClient to upload them into a custom entity in our instance of CRM.

The query is returning results and seems to be setting the fields in sms properly. 

The error is being thrown at this line:   Guid NewActid = crmService.Create(sms);

Any idea what is causing this?

ERROR Details:

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=UploadLeads_toCRM
StackTrace:
at UpdateCRM.Program.HMtoCRM() in C:\...\UploadLeads_toCRM\Program.cs:line 149
at UpdateCRM.Program.Main(String[] args) in C:\...\UploadLeads_toCRM\Program.cs:line 32   

CODE:

using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader r = command.ExecuteReader();

var p = 0;

while (r.Read())
{
Console.WriteLine(r["contactid"].ToString());
Console.WriteLine(r["date"].ToString());
Console.WriteLine(r["messages"].ToString());


var messages = r["messages"].ToString();

var sms = new ksl_sms
{
ActualStart = DateTime.Parse(r["date"].ToString()),
ActualEnd = DateTime.Parse(r["date"].ToString()),
ksl_Contact = new EntityReference("contact", Guid.Parse(r["contactid"].ToString())),
RegardingObjectId = new EntityReference("contact", Guid.Parse(r["contactid"].ToString())),
OwnerId = new EntityReference("systemuser", Guid.Parse(r["ownerid"].ToString())),
ksl_completeby = new EntityReference("systemuser", Guid.Parse(r["ownerid"].ToString())),
StatusCode = new OptionSetValue(1),
ksl_TextsReceived = Convert.ToInt16(r["totalreceived"]),
ksl_TextsSent = Convert.ToInt16(r["totalsent"].ToString()),
Description = r["messages"].ToString(),
Subject = "Text Conversation"
};

Guid NewActid = crmService.Create(sms);

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Do you have any plugins that handles Create operation of your ksl_sms record?

  • JonathanKisco Profile Picture
    25 on at

    We are using this:

               CrmServiceClient crmConn = new CrmServiceClient("AuthType = Office365; Url = https://****.api.crm.dynamics.com/XRMServices/2011/Organization.svc; Username=****; Password=****");

               IOrganizationService crmService = crmConn.OrganizationServiceProxy;

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I asked another question but since you brought the connection string here - you should stop using Office365 because Microsoft drops the support of it and you should start using OAuth instead of it.

  • JonathanKisco Profile Picture
    25 on at

    I don't believe we have a plugin creating it.  This script was uploading the records fine for the last month or so, and now has stopped.

    Why would it be saying the sms object is null when I can see the various fields are populated?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Jonathan,

    Let's put the plugin aside. Also you SMS record is not null.

    The issue with your code is related to authentication. Microsoft gets rid of Office365 authentication and actually, the code you use to connect to instance doesn't give you the valid connection object and crmConn.OrganizationServiceProxy returns null.

    As I mentioned in my previous reply - change the connection string to use OAuth.

  • JonathanKisco Profile Picture
    25 on at

    Ahh. That makes sense, I didn't understand what you meant in the first post.   Now I having a heck of a time getting the Oauth to connect properly.   Know of any good guides for setting it up for this use case?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    You can check this video - nishantrana.me/.../

  • JonathanKisco Profile Picture
    25 on at

    I tried that, but still no luck.   Here is my connection string, I am pulling the appid from Application (client) ID in Azure.  

    I tried following these directions, but I was unable to create an application user in Dynamics, I think because we currently pull our users from our on prim instance of AD. https://optimallogics.com/connect-to-ms-dynamics-365-crm-using-oauth-in-c-sharp/

    Is there anything I should be doing in the AD set up of the app?   Also do you know any instructions for how to see the Oauth connection attempt response? I couldn't find anything, but it seems that might help give a clue on the issue. 

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

               CrmServiceClient crmConn = new CrmServiceClient("AuthType = OAuth; " +

                                               "Url = https://kis.crm.dynamics.com; " +

                                               "Username=[user]@kis.onmicrosoft.com; Password=*****" +

                                               "AppId=33a****;  " +

                                               "RedirectUri = app://33a****;  " +

                                               "LoginPrompt = Auto ");

               IOrganizationService crmService = crmConn.OrganizationServiceProxy;

    Guid NewActid = crmService.Create(sms);  <--- Fail point


  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Instead of using

    IOrganizationService crmService = crmConn.OrganizationServiceProxy;

    Guid NewActid = crmService.Create(sms);

    try to use

    Guid NewActid = crmConn.Create(sms);

  • JonathanKisco Profile Picture
    25 on at

    No change :/

    I also verified that I could log in to GUI with the user so all the permissions there are correct.

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 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 45 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 36

#3
Satyam Prakash Profile Picture

Satyam Prakash 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans