[SalesForce] Cannot Set LoginResult.serverUrl – UNKNOWN_EXCEPTION: Destination URL not reset. The URL returned from login must be set in the SforceService

I've been successfully using integrations to the SFDC using c# solution. So far I added the web reference and used it without any issues. But then I started add the Service Refference, by downloading the enteprise wsdl to local and used by Add Service Reference option. And I was reffering to following article (https://developer.salesforce.com/docs/atlas.en-us.salesforce_developer_environment_dotnet_tipsheet.meta/salesforce_developer_environment_dotnet_tipsheet/salesforce_developer_environment_verify_dotnet_wsdl.htm).
I was able to run the login function, but there I was unable set the serverUrl as I was unable to find the Url property on my SoalpClient. I think Becaues of that when it tries to SOQL query, it retuns following Error message.
"UNKNOWN_EXCEPTION: Destination URL not reset. The URL returned from login must be set in the SforceService"

So when I was using web reference I was successfully able to create the sforcebinding and set the ServerUrl to the property I have in sforcebinding. Really Appriciate Your Help Here

Here is my code,

using SFServices.SFDC_FSb_9_23;

namespace SFServices
{
    class SFServiceClient
    {               
        private static SoapClient SFDC_FSb_9_23Binding;
        private static LoginResult CurrentLoginResult;   

        private bool SFLogin()
        {
            SFDC_FSb_9_23Binding = new SoapClient();  

            string UserName = "usename@domain.com.fsandbox";
            string Password = "passwordwithtoken";        

            try
            {
                CurrentLoginResult = SFDC_FSb_9_23Binding.login(null, UserName, Password);   
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                throw new Exception("Login Error: Please contact Technical Team for more details - "+ex);
                //return false;
            }
            if (CurrentLoginResult.passwordExpired)
            {
                throw new Exception("Password Expired: Please contact Technical Team for more details");
                //return false;
            }
            else
            {    

                //HOW CAN I SET THE ServerUrl. AND I THINK THE ISSUE IS HERE

                return true;                
            }
        }


public bool sampleMethod(string field)
        {
            try
            {           
                if (SFLogin())
                {                    
                    String Query_1 = "SELECT Name, Id, FROM ACCOUNT WHERE custom_field__c='" + field + "'";
                    QueryResult qr = new QueryResult();                    
                    SFDC_FSb_9_23Binding.query(null, null, null, null, Query_1, out qr); // ISSUE COMES FROM HERE
                }
            }
        }  

Thanks,
Mudi

Best Answer

        //LOGIN PART
        sforce.SoapClient sc = new sforce.SoapClient();
        sc.Endpoint.Address = new System.ServiceModel.EndpointAddress("https://test.salesforce.com/services/Soap/u/34.0");
        sforce.LoginResult lr = sc.login(null, null, "someusername@emailformat.com", "test12345");

        //TRANSACTION PART
        sc = new sforce.SoapClient(); //THIS IS IMPORTANT!!! you can't use the existing instance for some reason...
        sc.Endpoint.Address = new System.ServiceModel.EndpointAddress(lr.serverUrl);
        sforce.SessionHeader sh = new sforce.SessionHeader();
        sh.sessionId = lr.sessionId;
        sforce.QueryResult qr;
        sc.queryAll (sh, null, null, "select id, casenumber, subject from Case limit 100", out qr);

Take note that we needed another sc = new sforce.SoapClient(); because utilizing the existing instance used for login() was causing the error you mentioned.

Related Topic