2

I'm using ForceClient in Force.com Toolkit for .NET to save a custom object with a link to a Lead record.

    private class Lead
    {
        public const string SObjectTypeName = "Lead";

        public string Id { get; set; }
        public string LastName { get; set; }
    }

    private class ResourceView
    {
        public const string SObjectTypeName = "ResourceView__c";

        public string Id { get; set; }
        public Lead Lead__c { get; set; }
        public string Name { get; set; }
    }

    ResourceView rv = new ResourceView()
    {
            Name = "Important PDF",
            Lead__c = lead
    };

    var resourceView = await client.CreateAsync(ResourceView.SObjectTypeName, rv);

Here is the field list configured in SF:

Field list

This call fails with:

Cannot deserialize instance of reference from START_OBJECT value { or request may be missing a required field at [line:1, column:2]

I also tried changing Lead__c to be a string and pass the lead.Id from Salesforce. This fails with:

Lead: id value of incorrect type: 0035800000AQqGfAAL

If I remove setting the Lead property, the object saves successfully.

2 Answers 2

2

The following console application ran for me and created the ResourceView__c record associated with the Lead.

Note that:

  • The Lead Id was assigned as a string.
  • I needed to add my namespace prefix of "DFB__" to the types.

Full console app:

class Program
{
    static void Main(string[] args)
    {
        Task.Run(async () =>
        {
            CreateRecord();
        }).Wait();

        Console.ReadLine();
    }

    private static readonly string ConsumerKey = "3MVG9V_NOTmyRealKey_jGiqAUqY4lvuZc_xS0UZ5FN_";
    private static readonly string ConsumerSecret = "70012345678901234567";
    private static readonly string Username = "[email protected]";
    private static readonly string Password = "wololo";

    static async Task CreateRecord()
    {
        var auth = new AuthenticationClient();

        string IsSandboxUser = "false";

        // Authenticate with Salesforce
        Console.WriteLine("Authenticating with Salesforce");
        var url = "https://login.salesforce.com/services/oauth2/token";

        await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url);
        Console.WriteLine("Connected to Salesforce");

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

        ResourceView rv = new ResourceView()
        {
            Name = "Important PDF",
            DFB__Lead__c = "00Q7000000zB6c4"
        };

        try
        {
            var resourceView = await client.CreateAsync(ResourceView.SObjectTypeName, rv);
            Console.Out.WriteLine(resourceView);
        }
        catch (Exception ex)
        {
            Console.Out.WriteLine(ex);
            System.Diagnostics.Debug.WriteLine(ex);
        }
    }

    private class ResourceView
    {
        public const string SObjectTypeName = "DFB__ResourceView__c";

        public string Id { get; set; }
        public string DFB__Lead__c { get; set; }
        public string Name { get; set; }
    }
}
0
1

After reading the other answers and reviewing my code again I found the answer staring me in the face. The second error message was the key

Lead: id value of incorrect type: 0035800000AQqGfAAL

Turns out this ID was a Contact ID, not a Lead ID. I had assumed this error meant string was the wrong type but no, it meant I was trying to pass a Contact into a Lead object.

1
  • 1
    Ah yes, I should have noticed that. The 003 keyprefix is for Contacts, and 00Q for leads. Commented Jun 23, 2016 at 8:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .