How to Create a new Child document in any Doctype of ERPNext using API

Hello! I have a Desktop Application that is responsible for pushing data from the local database to the ERPNext server. There is a document of ERPNext Timesheet. The question is that I want to put a new record of the child table of Timesheet like time_logs. But if I use the POST request its response will be {} just blank and no new row is created. But if I use a PUT request, it changes the previous child row. I want to create a new row since the parent document is in draft.

Below is my code.

{
  "activity_type": "Communication",
  "from_time": "2024-01-15 11:50:20",
  "description": "Administrator",
  "project": "PROJ-0001",
  "to_time": "2024-01-15 11:50:36"
}




string timesheet = @"{";
string str = @"
                  ""activity_type"": ""vactivity_type"",
                  ""from_time"": ""vfrom_time"",
                  ""description"": ""vdescription"",
                  ""project"": ""vproject"",
                  ""to_time"": ""vto_time""
                ";
RestRequest restRequest = new RestRequest($"/api/resource/Timesheet/TS-2024-00001/time_logs", Method.POST);
str = str.Replace("vactivity_type", "Communication"/*row["activity_type"].ToString()*/);
str = str.Replace("vfrom_time", Convert.ToDateTime(row["from_time"]).ToString("yyyy-MM-dd HH:mm:ss"));
str = str.Replace("vdescription", row["description"].ToString());
str = str.Replace("vproject", row["project"].ToString());
str = str.Replace("vto_time", Convert.ToDateTime(row["to_time"]).ToString("yyyy-MM-dd HH:mm:ss"));
timesheet += string.Concat(str, "}");
restRequest.AddParameter("data", timesheet);
IRestResponse restResponse = restClient.Execute(restRequest);

Please guide me on this. Thanks in advance.

1 Like

Yeah!!! I just figured it out myself. POST new record to the Child DocType and be sure to include these first 3 elements along with the new data.

POST to {{baseUrl}}/api/resource/AuditEntryActionCollaborator
{
“parent”: “38”,
“parentfield”: “collaborators”,
“parenttype”: “AuditEntryAction”,
“role”: “test3”,
“ext_user”: 0
}

As another note… I still see issues with multiple posts to the same endpoint… duplication. So I am using constraints in my MySQL database to setup unique requirements.

Well, could you please explain AuditEntryActionCollaborator is a Child Doctype?

Not sure what you mean by ‘explain’? It is a child table of AuditEntryAction… which is many-to-one linked to AuditEntry… which is many-to-one linked to Audit.