Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Object reference not set to an instance of an object Error while Attempting to retrieve record in crm

$
0
0

Hi, 
On a plugin i attempt to retrieve record in other CRM instance. 
Here is the code:

namespace TES_CRM_PLUGIN
{
    public class Class1 : IPlugin
    {
        string connstring = string.Empty;

        IOrganizationService _service;
        Guid fabercastel = Guid.Empty;
        string x;
        public void Execute(IServiceProvider ServiceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity ent = (Entity)context.InputParameters["Target"];

                if (ent.LogicalName != "opportunity")
                    return;
                //Getting id from new field during create process in CRM On-Premise
                string idnum = ent.GetAttributeValue<String>("new_id");

                if (ent.Attributes.Contains("name"))
                {

                    //Attempting to connect CRM Online
                    connstring = @"Url=office.crm5.dynamics.com; Username=admin@office.onmicrosoft.com; Password=crmoffice; authtype=Office365";
                    CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connstring);
                    _service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

                        if (!string.IsNullOrEmpty(idnum))
                        {
                            try{

                                //Getting record from CRM Online by matching the idnum
                                QueryExpression query = new QueryExpression("opportunity");
                                string[] cols2 = { "new_presalesid", "name" };
                                query.Criteria = new FilterExpression();
                                query.Criteria.AddCondition("new_presalesid", ConditionOperator.Equal, idnum);
                                query.ColumnSet = new ColumnSet(cols2);
                                EntityCollection preid = _service.RetrieveMultiple(query);

                                foreach (Entity cloudent in preid.Entities)
                                {
                                    if (cloudent.Attributes.Contains("new_presalesid"))
                                    {
                                        x = cloudent.GetAttributeValue<String>("new_presalesid");
                                    }
                                }
                            }
                            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
                            {
                                throw new InvalidPluginExecutionException(ex.Message);
                            }
                        }

                       // Fill the description field in the CRM On-premise, by value we were took from CRM Online
                        if (!string.IsNullOrEmpty(x))
                        {
                            ent["description"] = x;
                        }
                        else
                        {
                            ent["description"] = "FAK";
                        }

                }

            }
        }
    }
}

The plugin throw an error like this:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Error: Object reference not set to an instance of an object.Detail:<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts"><ActivityId>48b026df-da81-4eba-a075-d7f3b3b1bbc3</ActivityId><ErrorCode>-2147220891</ErrorCode><ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic"><KeyValuePairOfstringanyType><d2p1:key>OperationStatus</d2p1:key><d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:string">0</d2p1:value></KeyValuePairOfstringanyType><KeyValuePairOfstringanyType><d2p1:key>SubErrorCode</d2p1:key><d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:string">-2146233088</d2p1:value></KeyValuePairOfstringanyType></ErrorDetails><Message>Error: Object reference not set to an instance of an object.</Message><Timestamp>2017-02-22T03:44:48.8456384Z</Timestamp><ExceptionSource i:nil="true" /><InnerFault i:nil="true" /><OriginalException i:nil="true" /><TraceText>

[TES CRM PLUGIN: TES_CRM_PLUGIN.Class1]
[4b76457a-05f8-e611-80cb-000c2901c43a: TES_CRM_PLUGIN.Class1: Create of opportunity]
Attempting to connect CRM ONline..
Trying to get account name

</TraceText></OrganizationServiceFault>


The error is coming from this line:

 QueryExpression query = new QueryExpression("opportunity");
                                string[] cols2 = { "new_presalesid", "name" };
                                query.Criteria = new FilterExpression();
                                query.Criteria.AddCondition("new_presalesid", ConditionOperator.Equal, idnum);
                                query.ColumnSet = new ColumnSet(cols2);
                                EntityCollection preid = _service.RetrieveMultiple(query);


There, i used _service to retrieve queryexpression. The _service is declare in this line:

                   _service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

It's seem that _service is null. So it can't be used to retrieving data.

I have been searching on the internet how to solve this, but i am unable to find a way to solve it yet.


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>