Below is some part of my code.I had been try to explain.
//data in postdata for add pass in service using ajax
var requestData =
{
"ModuleCode": "incident",
"PostData": [{ "Key": "customerid", "Value": "a624c9dc-cc83-e611-80dc-fc15b428cc94", "Datatype": "lookup", "Entityname": "contact" }, { "Key": "title", "Value": "cq", "Datatype": "String" }, { "Key": "contractid", "Value": "d1f0f5cb-9c84-e611-80dc-fc15b428cc94", "Datatype": "Lookup", "Entityname": "contract" }, { "Key": "prioritycode", "Value": "3", "Datatype": "Picklist" }, { "Key": "description", "Value": "qqqq", "Datatype": "Memo" }, { "Key": "ticketnumber", "Value": "", "Datatype": "String" }, { "Key": "productid", "Value": "072d9a60-e9c2-e411-80e5-c4346bad2660", "Datatype": "Lookup", "Entityname": "product" }],
}
//below is the code that call addorupdate function from other function.
Entity moduledata = new Entity(modulename);
moduledata = addorupdate(moduledata, PostData, modulename);
newmoduledataid = _serviceProxy.Create(moduledata);
public Entity addorupdate(Entity moduledata, List<PostData> PostData, string modulename)
{
for (var i = 0; i < PostData.Count(); i++)
{
bool flag = true;
if (PostData[i].Datatype != null && PostData[i].Datatype != "")
{
if (flag)
{
if (PostData[i].Datatype.ToLower() == "picklist" || PostData[i].Datatype.ToLower() == "optionset" || PostData[i].Datatype.ToLower() == "dropDown")
{
if (PostData[i].Value != null && PostData[i].Value != "")
moduledata[PostData[i].Key] = new OptionSetValue((Convert.ToInt32(PostData[i].Value)));
}
if (PostData[i].Datatype.ToLower() == "string" || PostData[i].Datatype.ToLower() == "memo" || PostData[i].Datatype.ToLower() == "multilinestext")
{
if (PostData[i].Value != null && PostData[i].Value != "")
moduledata[PostData[i].Key] = PostData[i].Value;
}
else if (PostData[i].Datatype.ToLower() == "lookup")
{
if ((PostData[i].Value != null && PostData[i].Value != "") && (PostData[i].Entityname != null && PostData[i].Entityname != ""))
moduledata[PostData[i].Key] = new EntityReference(PostData[i].Entityname, Guid.Parse(PostData[i].Value));
}
//like this for all datatype
}
}
}
return moduledata;
}