function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mandycmandyc 

Update Self-Service User Record

I would like to update a Self-Service User record via an OM when the related contact record is updated. The below code is generating the following error: Object reference not set to an instance of an object.

 

I appreciate the help!

 

 

for (int i = 0; i < contacts.Length; i++)
        {
            EditContactNotification.ContactNotification notification = contacts[i];
            EditContactNotification.Contact contact = (EditContactNotification.Contact)notification.sObject;
            
            EnterpriseWebReference.QueryResult qr = null;
            try
            {
                qr = binding.query("SELECT Id FROM SelfServiceUser WHERE ContactId = " + contact.Id);
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Query didn't work " + e.Message);
            }

            if (qr.size > 0)
            {
                EnterpriseWebReference.sObject[] records = qr.records;
                for (int x = 0; x < records.Length; x++)
                {
                    EnterpriseWebReference.SelfServiceUser var = ((EnterpriseWebReference.SelfServiceUser)records[x]);
                    EnterpriseWebReference.SelfServiceUser ssu = new EnterpriseWebReference.SelfServiceUser();
                    ssu.Id = var.Id;
                    ssu.ContactId = contact.Id;
                    ssu.FirstName = contact.FirstName;
                    ssu.LastName = contact.LastName;
                    ssu.Username = contact.Email;
                    ssu.Email = contact.Email;
                    EnterpriseWebReference.SaveResult[] saveResults = binding.update(new EnterpriseWebReference.sObject[] { ssu });
                }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mandycmandyc

I still don't know how to debug my code but I figured out the issue. I was setting the contact Id in my original update code and once I took that out everything started working. Here is my functioning code for anyone trying to update a Self Service User account in the future.

 

        EditContactNotification.ContactNotification[] contacts = notifications1.Notification;
        for (int i = 0; i < contacts.Length; i++)
        {
            EditContactNotification.ContactNotification notification = contacts[i];
            EditContactNotification.Contact contact = (EditContactNotification.Contact)notification.sObject;
            
            try
            {
                EnterpriseWebReference.QueryResult qr = null;
                qr = binding.query("SELECT Id FROM SelfServiceUser WHERE ContactId = '" + contact.Id + "'");
                if (qr.size > 0)
                {
                    EnterpriseWebReference.sObject[] records = qr.records;
                    for (int x = 0; x < records.Length; x++)
                    {
                        EnterpriseWebReference.sObject ssu2Update = qr.records[x];
                        EnterpriseWebReference.SelfServiceUser ssu = new EnterpriseWebReference.SelfServiceUser();
                        ssu.Id = ssu2Update.Id;
                        ssu.FirstName = contact.FirstName;
                        ssu.LastName = contact.LastName;
                        ssu.Username = contact.Email;
                        ssu.Email = contact.Email;
                        try
                        {
                            EnterpriseWebReference.SaveResult[] saveResults = binding.update(new EnterpriseWebReference.sObject[] { ssu });
                        }
                        catch (System.Exception e)
                        {
                            Console.WriteLine("Update didn't work " + e.Message);
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Query didn't work " + e.Message);
            }
        }
        EditContactNotification.notificationsResponse response = new EditContactNotification.notificationsResponse();
        response.Ack = true;
        return response;

 

All Answers

SuperfellSuperfell

On which line?

mandycmandyc

I've refactored my code a bit and I am no longer receiving an error; however, my SSU is not updating either.

 

Here is my current code. This is my first C# web service so I'm not sure how best to debug the code. I'm not sure if I'm getting a result from the query or not. Any suggestions you have are appreciated!

 

EditContactNotification.ContactNotification[] contacts = notifications1.Notification;
        for (int i = 0; i < contacts.Length; i++)
        {
            EditContactNotification.ContactNotification notification = contacts[i];
            EditContactNotification.Contact contact = (EditContactNotification.Contact)notification.sObject;
            
            try
            {
                EnterpriseWebReference.QueryResult qr = null;
                qr = binding.query("SELECT Id FROM SelfServiceUser WHERE ContactId = " + contact.Id);
                if (qr.size > 0)
                {
                    EnterpriseWebReference.sObject[] records = qr.records;
                    for (int x = 0; x < records.Length; x++)
                    {
                        EnterpriseWebReference.SelfServiceUser var = ((EnterpriseWebReference.SelfServiceUser)records[x]);
                        EnterpriseWebReference.SelfServiceUser ssu = new EnterpriseWebReference.SelfServiceUser();
                        ssu.Id = var.Id;
                        ssu.ContactId = contact.Id;
                        ssu.FirstName = contact.FirstName;
                        ssu.LastName = contact.LastName;
                        ssu.Username = contact.Email;
                        ssu.Email = contact.Email;
                        try
                        {
                            EnterpriseWebReference.SaveResult[] saveResults = binding.update(new EnterpriseWebReference.sObject[] { ssu });
                        }
                        catch (System.Exception e)
                        {
                            Console.WriteLine("Update didn't work " + e.Message);
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Query didn't work " + e.Message);
            }
        }
        EditContactNotification.notificationsResponse response = new EditContactNotification.notificationsResponse();
        response.Ack = true;
        return response;
    }

 

SuperfellSuperfell

You call binding.update, but don't check the returned saveResult, if that chunk of code is actually getting called,then that'll tell you why the update failed. You should probably add more logging for the query response to make it easier to debug.

mandycmandyc

Can you send me an example of how I might add the additional logging? Will I see the log information on the OM Delivery Status screen?

 

Thank you

mandycmandyc

I still don't know how to debug my code but I figured out the issue. I was setting the contact Id in my original update code and once I took that out everything started working. Here is my functioning code for anyone trying to update a Self Service User account in the future.

 

        EditContactNotification.ContactNotification[] contacts = notifications1.Notification;
        for (int i = 0; i < contacts.Length; i++)
        {
            EditContactNotification.ContactNotification notification = contacts[i];
            EditContactNotification.Contact contact = (EditContactNotification.Contact)notification.sObject;
            
            try
            {
                EnterpriseWebReference.QueryResult qr = null;
                qr = binding.query("SELECT Id FROM SelfServiceUser WHERE ContactId = '" + contact.Id + "'");
                if (qr.size > 0)
                {
                    EnterpriseWebReference.sObject[] records = qr.records;
                    for (int x = 0; x < records.Length; x++)
                    {
                        EnterpriseWebReference.sObject ssu2Update = qr.records[x];
                        EnterpriseWebReference.SelfServiceUser ssu = new EnterpriseWebReference.SelfServiceUser();
                        ssu.Id = ssu2Update.Id;
                        ssu.FirstName = contact.FirstName;
                        ssu.LastName = contact.LastName;
                        ssu.Username = contact.Email;
                        ssu.Email = contact.Email;
                        try
                        {
                            EnterpriseWebReference.SaveResult[] saveResults = binding.update(new EnterpriseWebReference.sObject[] { ssu });
                        }
                        catch (System.Exception e)
                        {
                            Console.WriteLine("Update didn't work " + e.Message);
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Query didn't work " + e.Message);
            }
        }
        EditContactNotification.notificationsResponse response = new EditContactNotification.notificationsResponse();
        response.Ack = true;
        return response;

 

This was selected as the best answer