2

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

  • why not use web reference instead of service reference? using service reference has different implementation and is not aligned with documentation. – pnoytechie Sep 23 '15 at 9:22
  • Hey thanks for the update. Simply because I encountered an issue when I add the web reference. So after I check with salesforce support, they suggest me to use the service reference. This is the issue i had previously, salesforce.stackexchange.com/questions/92781/… Thanks, Mudi – LahiruS Sep 23 '15 at 9:25
  • i can add web service without problem to my c# project without problem – pnoytechie Sep 23 '15 at 9:40
  • How would you set the serverurl? can you share the code snippet? – LahiruS Sep 23 '15 at 9:58
  • i've added it as web service so the good 'ol, documentation aligned approach works, i.e. sforce.SforceService binding=new sforce.SforceService(); binding.Url="https://test.salesforce.com"; sforce.LoginResult lr=binding.login("username@emailformat.com", "passwrd+token"); binding.Url=lr.serverUrl; binding.SessionHeaderValue=new sforce.SessionHeader(); binding.SessionHeaderValue.sessionId = lr.sessionId; – pnoytechie Sep 23 '15 at 10:15
3
        //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.

  • Thank you so much for the Answer. I tried it out and worked pretty well. I just wanted to try this out with Enterprise wsdl, and hence had to change the EndpointAddress to, "test.salesforce.com/services/Soap/c/34.0") Thank you so much for your help once agian – LahiruS Oct 7 '15 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.