function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bill JonesBill Jones 

Creating Record with Master-Detail and Picklist values

Hello,

I am trying to push data from my C# app into Salesforce. In Salesforce, I have a custom object, called Product, with the following four custom fields:
Field Label     API Name                 Data Type
-----------     ---------                ------------------------
Category        Category__c              Master-Detail (Category)
Description     Product_Description__c   Long Text Area (32768)
Foreign ID      Foreign_ID__c            Text(255)
Status          Product_Status__c        Picklist
I am trying to create a record in my Product object via the REST API. Currently, I have the following:
When this code gets executed,
var product = new Product();
product.Category__c = "C-0000001";
product.Product_Description__c = "This is a test sent at " + DateTime.Now.ToString();
product.Product_Status__c = "Available";

var json = JsonConvert.SerializeObject(product);

var request = (HttpWebRequest)(HttpWebRequest.Create(instanceUrl + "/services/data/v20.0/sobjects/Product__c/"));
request.Method = "POST";
request.ContentType = "application/json";

request.Headers.Add("Authorization: OAuth " + accessToken);
using (var requestWriter = new StreamWriter(request.GetRequestStream()))
{
  requestWriter.Write(json);
  requestWriter.Flush();
  requestWriter.Close();
}

var response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
  var data = reader.ReadToEnd();
}
When this code gets executed, I get a 400 error. The error is:
73
[{"message":"Category: id value of incorrect type: C-0000001","errorCode":"MALFORMED_ID","fields":["Category__c"]}]
0
What am I doing wrong? I suspect this has to deal with the fact that Category is a Master-Detail. I'm guessing I'll have a similar issue with the Picklist value. However, I'm not sure how to define that in C# to pass to Salesforce via the REST API.  Thank you for any help you can provide.



 
Best Answer chosen by Bill Jones
Shashikant SharmaShashikant Sharma
Why this issue is coming : You are trying to put a text value in a Master-Detail reference datatype field. Refrence field only expects a ID value , which should be a Salesforce record id, in this case id of a Category record in SFDC.

Solution 1 : When you are setting

product.Category__c = "C-0000001";

instead of string found the recordId from the Category object in SFDC and put the recordId.

Alternatively you could do
1. Create a text field in SFDC Category_Name ( Text Field ) and set it from your code
2. Develop a before insert , before update trigger in Apex on product to populate Category__c master-detail based on Category_Name value.