1

I an building an API that is essentially the middleware between a portal site and Salesforce. When trying to test and create a contact I am consistently getting a 400 bad request. I have tested that it is indeed logging in successfully to Salesforce.

Code I am using:

        public async Task<SFDCContactsCreate> AddUser([FromBody] SFDCContactsCreate sfdcC)
    {
        var auth = new AuthenticationClient();
        var url = Properties.Settings.Default.url1;
        await auth.UsernamePasswordAsync(Properties.Settings.Default.consumerKey, Properties.Settings.Default.consumerSecret, Properties.Settings.Default.acctName, Properties.Settings.Default.acctPw + Properties.Settings.Default.token, url);
        Console.Write(sfdcC);

        var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);
        var cnt = new SFDCContactsCreate();
        cnt.AccountId = sfdcC.AccountId;
        cnt.FirstName = sfdcC.FirstName;
        cnt.LastName = sfdcC.LastName;
        cnt.Email = sfdcC.Email;
        cnt.Title = sfdcC.Title;
        cnt.Phone = sfdcC.Phone;
        cnt.IsAdmin = sfdcC.IsAdmin;

            var body = JsonConvert.SerializeObject(cnt);

            var req = (HttpWebRequest)WebRequest.Create(Properties.Settings.Default.url2);
            req.ContentType = "application/json";
            req.Method = "post";

            byte[] data = System.Text.Encoding.ASCII.GetBytes(body);
            req.ContentLength = body.Length;
            var os = req.GetRequestStream();
            os.Write(data, 0, data.Length);
            os.Close();

            WebResponse resp;

            try
            {
                resp = req.GetResponse();
            }
            catch (WebException ex)
            {
                resp = ex.Response;
            }

            if (resp == null) return null;
            return sfdcC;
        }

The Fiddler Test I am sending:

POST http://localhost:18178/UserCreate/ HTTP/1.1 Host: localhost:18178 Content-Length: 154 Content-Type: application/json

{ "AccountId" : "001c000000TT4VU", "FirstName" : "John", "LastName" : "Smith", "Email" : "johnsmith@nowhere.com", "Title" : "CEO", "Phone" : "555-555-5555", "IsAdmin" : false }

I am not getting a specific line error and wonder if it is the test message I am sending. Any input would be quite helpful.

  • can you change your phone value as phone: "5555555555" – Manjot Singh Feb 20 '17 at 14:21
  • The fact that you are successfully logging in is great... that's usually the most difficult bit. Have you tried setting up a debug log for the API User? That might show you what's going on in Salesforce. – John Thompson Feb 20 '17 at 16:23
  • I tried the phone number and to no avail, same issue. thank you John, I was so deep I had not thought of that. Doing it now. – JP Blonshine Feb 20 '17 at 18:43
  • I turned on debug and even though I am getting successful logins, I am not getting any logs – JP Blonshine Feb 20 '17 at 18:53
0

It looks like you are using the Force.com Toolkit for .NET. I can see you are creating an instance of the ForceClient to handle the authentication, but then appear to ignore it and use a raw HttpWebRequest to POST the JSON payload in.

It's likely the Properties.Settings.Default.url2 doesn't have the correct URL. Also, you aren't passing the AccessToken in the Authentication Bearer header.

You need to use the ForceClient to perform the calls to Salesforce. It will have the active AccessToken (a.k.a. Session ID) and instance URL details that came back from the login operation.


Example code to insert a Contact Record from C# using the toolkit.

    static string ConsumerKey = "3MVG9VmVOCGHKYBRsgrNwitZ7FRpAHsO_NotARealConsumerKey_xS0UZ3FN_";
    static string ConsumerSecret = "1234567890123456789";
    static string Username = "login.user@example.com";
    static string Password = "password";

    static void Main(string[] args)
    {
        Task.Run(async () =>
        {
            await MainAsync(args);
        }).GetAwaiter().GetResult();
    }



    static async Task MainAsync(string[] args)
    {

        var auth = new AuthenticationClient();
        await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password);

        ForceClient client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);

        Contact cnt = new Contact();
        cnt.FirstName = "John";
        cnt.LastName = "Doe";
        cnt.Email = "john.doe@example.com";
        cnt.Title = "Person";

        SuccessResponse res = await client.CreateAsync("Contact", cnt);

    }

    public class Contact
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Title { get; set; }
    }
  • Thank you for the input. I will post the solution as it was actually the SuccessResponse line that made it work. Without that line I would have kept my wheels spinning. – JP Blonshine Feb 21 '17 at 13:59
0
public async Task<SFDCContactsCreate> AddUser(SFDCContactsCreate sfdcC)
    {
        var auth = new AuthenticationClient();
        var url = Properties.Settings.Default.url1;
        await auth.UsernamePasswordAsync(Properties.Settings.Default.consumerKey, Properties.Settings.Default.consumerSecret, Properties.Settings.Default.acctName, Properties.Settings.Default.acctPw + Properties.Settings.Default.token, url);
        Console.Write(sfdcC);

        var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);
        var cnt = new SFDCContactsCreate();
        cnt.AccountId = sfdcC.AccountId;
        cnt.FirstName = sfdcC.FirstName;
        cnt.LastName = sfdcC.LastName;
        cnt.Email = sfdcC.Email;
        cnt.Title = sfdcC.Title;
        cnt.Phone = sfdcC.Phone;
        cnt.IsAdmin__c = sfdcC.IsAdmin__c;

        SuccessResponse resp = await client.CreateAsync("Contact", cnt);

        if (resp == null) return null;
        return sfdcC;
    }

This is the code that worked for me with the posted JSON payload.

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.