Consume CRM O-Data Integration to send and recived the Json response in CRM
public str insertSalesBillingCrm(RecId _recid)
{
System.Exception ex,webException;
CLRObject clrObj;
str errorMsg,bodyData,byteTokenStr,payload,output;
SysInfologEnumerator infoEnumerator;
SysInfologMessageStruct infoMsgStruct;
Exception readException;
System.Net.WebHeaderCollection httpHeader, httpHeaderRes;
System.Net.HttpWebRequest request;
System.IO.StreamWriter streamWriter;
System.IO.Stream requestStream;
System.IO.StreamReader streamRead;
System.Net.HttpWebResponse response;
Newtonsoft.Json.Linq.JObject jObject;
Notes reqBodyStr, resBodyStr;
System.IO.StringWriter stringWriter;
Newtonsoft.Json.Linq.JTokenWriter jsonWriter;
TECAxCrmParameter parameter = TECAxCrmParameter::find();
TECSalesInvoiceBillingDetails salesInvoiceBilling;
str jsonStr;
str token = this.generateToken();//"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImtXYmthYTZxczh3c1RuQndpaU5ZT2hIYm5BdyIsImtpZCI6ImtXYmthYTZxczh3c1RuQndpaU5ZT2hIYm5BdyJ9.eyJhdWQiOiJodHRwczovL3N5bmRyb21lY3JtdWF0LmNybTguZHluYW1pY3MuY29tIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvNWEyMjQzYmEtOTg2ZC00MTYwLTk5NTQtNzI0ZjRjNWMyNTg0LyIsImlhdCI6MTcwODU5MDg0NiwibmJmIjoxNzA4NTkwODQ2LCJleHAiOjE3MDg1OTQ3NDYsImFpbyI6IkUyVmdZSGd1WWJGSUlkRlY4ZmIxSS9NZS9TcllCUUE9IiwiYXBwaWQiOiIyYjcwMzVhZi1lNDIzLTRmMGMtYTI5OS1lMjJiYjBlOWNlYTkiLCJhcHBpZGFjciI6IjEiLCJpZHAiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC81YTIyNDNiYS05ODZkLTQxNjAtOTk1NC03MjRmNGM1YzI1ODQvIiwib2lkIjoiNGVlYTFiYmQtNjc2Ni00ZDk0LWE0NjgtOTg0ZmE3ZDJmNjVlIiwicmgiOiIwLkFYSUF1a01pV20yWVlFR1pWSEpQVEZ3bGhBY0FBQUFBQUFBQXdBQUFBQUFBQUFEREFBQS4iLCJzdWIiOiI0ZWVhMWJiZC02NzY2LTRkOTQtYTQ2OC05ODRmYTdkMmY2NWUiLCJ0ZW5hbnRfcmVnaW9uX3Njb3BlIjoiQVMiLCJ0aWQiOiI1YTIyNDNiYS05ODZkLTQxNjAtOTk1NC03MjRmNGM1YzI1ODQiLCJ1dGkiOiJnR25aSVNCYnRFbUFVcC1UYlhrOUFBIiwidmVyIjoiMS4wIiwieG1zX2NhZSI6IjEifQ.CLFwOgGa4QQ_c51EIXRQPggI3C5RhR4ETe4d83ztG-YucMpefFhfPMDrBgqI-ptuuJ1gtRDQMzt7IVobxGuJnwI74DZ2UlPrw1YqRqVIDuGF-sxP_L__gFb4KT2s5J3Wb-nErPSTY08C4OFfNzDs4zxHAqwVNbjqXTBHubj_JoZiiLJYC0affo70OZM7zgQN89N8Id4DvWHp4mKE1rLYocm6v8u1asXefXAB-RaDWkAWT8M4B1W9EZCgLSvQOfKu2N4lDu_cbOU2UrXN1A2y5-0y2EpRu9m7xYIEM2lTlf6lzWzJtyO6sFfNE_KMUWdCcfCv9nTUJbQqZG3-KVUEgg";
// setting all header here
httpHeader = new System.Net.WebHeaderCollection();
httpHeader.Add("authorization", "Bearer " + token);
httpHeader.Add("prefer","return=representation"); //This is importand line to read a response from CRM other wise the body will blank and code as 204
try
{
new InteropPermission(InteropKind::ClrInterop).assert();
clrObj = System.Net.WebRequest::Create(parameter.CrmUrl);
request = clrObj;
request.set_Headers(httpHeader);
request.set_Method('POST');
request.set_ContentType('application/json; charset=utf-8');
requestStream = request.GetRequestStream();
streamWriter = new System.IO.StreamWriter(requestStream);
select * from salesInvoiceBilling
where salesInvoiceBilling.RecId == _recid;
str jsonString = "";
//Writting jason Start
jsonWriter = new Newtonsoft.Json.Linq.JTokenWriter();//new Newtonsoft.Json.JsonTextWriter(streamWriter);
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("new_recordid"); //AxRECID
jsonWriter.WriteValue(int642Str(_recid));
jsonWriter.WritePropertyName("new_invoicedate"); //AxINVOICEDATE
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.InvoiceDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
jsonWriter.WritePropertyName("new_customerpodate"); //AxCUSTOMERPODATE
if(salesInvoiceBilling.CustomerPODate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.CustomerPODate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_customerpono"); //AxCUSTOMERPONUM
jsonWriter.WriteValue(salesInvoiceBilling.CustomerPO);
// jsonWriter.WritePropertyName("new_dealidservice");
// jsonWriter.WriteValue(salesInvoiceBilling.);
jsonWriter.WritePropertyName("new_saletype"); //AxSALETYPE
jsonWriter.WriteValue(int2Str(this.getSaleType(salesInvoiceBilling.SaleType)));
jsonWriter.WritePropertyName("new_axproductname"); //AxPRODUCTNAME
jsonWriter.WriteValue(salesInvoiceBilling.ProductName);
jsonWriter.WritePropertyName("new_productcode"); //AxPRODUCTCODE
jsonWriter.WriteValue(salesInvoiceBilling.ProductCode);
jsonWriter.WritePropertyName("new_productrefcode"); //AxPRODREFERNO
jsonWriter.WriteValue(salesInvoiceBilling.ProductRefNo);
jsonWriter.WritePropertyName("new_productrefname"); //AxPRODREFERNAME
jsonWriter.WriteValue(salesInvoiceBilling.ProductRefName);
jsonWriter.WritePropertyName("new_customercode"); //AxCUSTCODE
jsonWriter.WriteValue(salesInvoiceBilling.CustomerId);
//jsonWriter.WritePropertyName("new_crmaccountname");
//jsonWriter.WriteValue(salesInvoiceBilling.CustName);
jsonWriter.WritePropertyName("new_contactperson"); //AxCONTACTPERSONNAME
jsonWriter.WriteValue(this.getDescriptionContactInformation(salesInvoiceBilling.CustomerId));
jsonWriter.WritePropertyName("new_contactno"); //AxCONTACTNO
jsonWriter.WriteValue(CustTable::find(salesInvoiceBilling.CustomerId).phone());
jsonWriter.WritePropertyName("new_contractno");// AxSALESID
jsonWriter.WriteValue(salesInvoiceBilling.SalesId);
jsonWriter.WritePropertyName("new_emailid");//AxEMAIL
jsonWriter.WriteValue(CustTable::find(salesInvoiceBilling.CustomerId).email());
jsonWriter.WritePropertyName("new_country"); //AxCOUNTRY
jsonWriter.WriteValue(CustTable::find(salesInvoiceBilling.CustomerId).countryName());
jsonWriter.WritePropertyName("new_legalentity"); //AxLEGALENTITY
jsonWriter.WriteValue(salesInvoiceBilling.DataAreaId);
jsonWriter.WritePropertyName("new_customeraccountmang"); //AxCUSTACCMANAGER
jsonWriter.WriteValue(HcmWorker::find(salesInvoiceBilling.CustAccountManager).name());
jsonWriter.WritePropertyName("new_technologymanager"); //AxTECHNOMANAGER
jsonWriter.WriteValue(HcmWorker::find(salesInvoiceBilling.TechManager).name());
jsonWriter.WritePropertyName("new_installationrequired_new"); //AxINSTALLATIONREQUIRED
jsonWriter.WriteValue(salesInvoiceBilling.InstallationRequired == NoYes::No ? "0" : "1");
jsonWriter.WritePropertyName("new_contracttype_new"); //AxCONTRACTTYPE
jsonWriter.WriteValue(int2Str(this.getContractType(salesInvoiceBilling.ContractType)));
jsonWriter.WritePropertyName("new_servicetype_new"); //AxSERVICEACTIVITYTYPE
jsonWriter.WriteValue(int2Str(this.getServiceActivityType(salesInvoiceBilling.ServiceActivityType)));
jsonWriter.WritePropertyName("new_supporttype_new"); //AxSUPPORTTYPE
jsonWriter.WriteValue(int2Str(this.getSupportType(salesInvoiceBilling.SupportType)));
jsonWriter.WritePropertyName("new_site"); //AxSITE
jsonWriter.WriteValue(salesInvoiceBilling.Site);
jsonWriter.WritePropertyName("new_siteaddress"); //AxSITEADDRESS
jsonWriter.WriteValue(salesInvoiceBilling.SiteAddress);
jsonWriter.WritePropertyName("new_oemcontractno"); //AxOEMCONTRACTNO
jsonWriter.WriteValue(salesInvoiceBilling.OEMContractNo);
jsonWriter.WritePropertyName("new_oemcontractstartdate"); //AxOEMCONTRACTSTARTDATE
if(salesInvoiceBilling.OEMStartDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.OEMStartDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_oemcontractenddate"); //AxOEMCONTRACTENDDATE
if(salesInvoiceBilling.OEMEndDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.OEMEndDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_producttype"); //AxPRODUCTTYPE
jsonWriter.WriteValue(int2Str(this.getProductType(salesInvoiceBilling.ProductType)));
jsonWriter.WritePropertyName("new_serialno"); //PRODREFERSERIALNO
jsonWriter.WriteValue(salesInvoiceBilling.ProductRefSerialNo);
jsonWriter.WritePropertyName("new_sycontractstartdate"); //AxCONTRACTSTARTDATE
if(salesInvoiceBilling.ContractStartDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.ContractStartDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_sycontractenddate"); //AxCONTRACTENDDATE
if(salesInvoiceBilling.ContractEndDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.ContractEndDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_invoiceno"); //AxINVOICEID
jsonWriter.WriteValue(salesInvoiceBilling.InvoiceId);
jsonWriter.WritePropertyName("new_syskillsetstartdate"); //AxSKILLSETSTARTDATE
if(salesInvoiceBilling.SkillsetStartDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.SkillsetStartDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_syskillsetenddate"); //AxSKILLSETENDDATE
if(salesInvoiceBilling.SkillsetEndDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.SkillsetEndDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_amcstartdate"); //AxAMCSTARTDATE
if(salesInvoiceBilling.SubscriptionStartDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.SubscriptionStartDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_amcenddate"); //AxAMCENDDATE
if(salesInvoiceBilling.SubscriptionEndDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.SubscriptionEndDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("syn_crmaccountnametext"); //AxCUSTNAME
jsonWriter.WriteValue(salesInvoiceBilling.CustName);
jsonWriter.WritePropertyName("syn_remarks"); //AXREMARKS
jsonWriter.WriteValue(salesInvoiceBilling.Remarks);
//"new_CRMAccountName@odata.bind": "/accounts(7a34c845-85ab-e811-a969-000d3af06848)"
jsonWriter.WritePropertyName("new_CRMAccountName@odata.bind"); //CRM Customer account lookup values needs to be identified as per GUID
jsonWriter.WriteValue(strFmt("/accounts(%1)", this.getAccountNameGuid(CustTable::find(salesInvoiceBilling.CustomerId).name(), token)));
// End of writting Json
jsonWriter.WriteEndObject();
clrObj = jsonWriter.get_Token();
jObject = clrObj;
reqBodyStr = jObject.ToString();
// Request
streamWriter.WriteAsync(reqBodyStr);
streamWriter.Flush();
streamWriter.Close();
//Response
new InteropPermission(InteropKind::DllInterop).assert();
response = request.GetResponse();
requestStream = response.GetResponseStream();
streamRead = new System.IO.StreamReader(requestStream);
//From Json get the value in Map And read the MAP
jsonStr = streamRead.ReadToEnd();
Map responseData = RetailCommonWebAPI::getMapFromJsonString(jsonStr);
Description billingID = responseData.lookup("new_billingid");
if(billingID)
{
// to update the billing id and crm insert status
ttsbegin;
salesInvoiceBilling.selectForUpdate(true);
salesInvoiceBilling.CRMBillingid = billingID;
salesInvoiceBilling.CRMInsert = NoYes::Yes;
salesInvoiceBilling.doUpdate();
ttscommit;
}
}
catch (Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(ex.ToString());
if (ex != null)
{
ex = ex.get_InnerException();
if ((ex != null) && (ex is System.Net.WebException))
{
webException = ex as System.Net.WebException;
info(webexception.ToString());
}
}
}
catch (Exception::Error)
{
if(strLen(errorMsg)>1)
errorMsg+='\n';
infoEnumerator = SysInfologEnumerator::newData(infolog.cut());
while (infoEnumerator.moveNext())
{
infoMsgStruct = new SysInfologMessageStruct(infoEnumerator.currentMessage());
readException = infoEnumerator.currentException();
if(readException == Exception::Error || readException == Exception::Warning)
{
errorMsg += infoMsgStruct.message() + '\n';
info(webexception.ToString());
}
}
}
return jsonStr;
}
===========================================================================
// <summary>
/// This method is used to Update a record in CRM with the parameter of AX Recid as a primary source.
/// </summary>
/// <returns>
/// Json if the record found and Update In CRM.
/// </returns>
public str updateSalesBillingCrm(RecId _recid, Description _crmBillingID)
{
System.Exception ex,webException;
CLRObject clrObj;
str errorMsg,bodyData,byteTokenStr,payload,output;
SysInfologEnumerator infoEnumerator;
SysInfologMessageStruct infoMsgStruct;
Exception readException;
System.Net.WebHeaderCollection httpHeader, httpHeaderRes;
System.Net.HttpWebRequest request;
System.IO.StreamWriter streamWriter;
System.IO.Stream requestStream;
System.IO.StreamReader streamRead;
System.Net.HttpWebResponse response;
Newtonsoft.Json.Linq.JObject jObject;
Notes reqBodyStr, resBodyStr;
// Newtonsoft.Json.Linq.JTokenWriter writer;
System.IO.StringWriter stringWriter;
Newtonsoft.Json.Linq.JTokenWriter jsonWriter;
TECAxCrmParameter parameter = TECAxCrmParameter::find();
TECSalesInvoiceBillingDetails salesInvoiceBilling;
str jsonStr;
str token = this.generateToken();//"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImtXYmthYTZxczh3c1RuQndpaU5ZT2hIYm5BdyIsImtpZCI6ImtXYmthYTZxczh3c1RuQndpaU5ZT2hIYm5BdyJ9.eyJhdWQiOiJodHRwczovL3N5bmRyb21lY3JtdWF0LmNybTguZHluYW1pY3MuY29tIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvNWEyMjQzYmEtOTg2ZC00MTYwLTk5NTQtNzI0ZjRjNWMyNTg0LyIsImlhdCI6MTcwODU5MDg0NiwibmJmIjoxNzA4NTkwODQ2LCJleHAiOjE3MDg1OTQ3NDYsImFpbyI6IkUyVmdZSGd1WWJGSUlkRlY4ZmIxSS9NZS9TcllCUUE9IiwiYXBwaWQiOiIyYjcwMzVhZi1lNDIzLTRmMGMtYTI5OS1lMjJiYjBlOWNlYTkiLCJhcHBpZGFjciI6IjEiLCJpZHAiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC81YTIyNDNiYS05ODZkLTQxNjAtOTk1NC03MjRmNGM1YzI1ODQvIiwib2lkIjoiNGVlYTFiYmQtNjc2Ni00ZDk0LWE0NjgtOTg0ZmE3ZDJmNjVlIiwicmgiOiIwLkFYSUF1a01pV20yWVlFR1pWSEpQVEZ3bGhBY0FBQUFBQUFBQXdBQUFBQUFBQUFEREFBQS4iLCJzdWIiOiI0ZWVhMWJiZC02NzY2LTRkOTQtYTQ2OC05ODRmYTdkMmY2NWUiLCJ0ZW5hbnRfcmVnaW9uX3Njb3BlIjoiQVMiLCJ0aWQiOiI1YTIyNDNiYS05ODZkLTQxNjAtOTk1NC03MjRmNGM1YzI1ODQiLCJ1dGkiOiJnR25aSVNCYnRFbUFVcC1UYlhrOUFBIiwidmVyIjoiMS4wIiwieG1zX2NhZSI6IjEifQ.CLFwOgGa4QQ_c51EIXRQPggI3C5RhR4ETe4d83ztG-YucMpefFhfPMDrBgqI-ptuuJ1gtRDQMzt7IVobxGuJnwI74DZ2UlPrw1YqRqVIDuGF-sxP_L__gFb4KT2s5J3Wb-nErPSTY08C4OFfNzDs4zxHAqwVNbjqXTBHubj_JoZiiLJYC0affo70OZM7zgQN89N8Id4DvWHp4mKE1rLYocm6v8u1asXefXAB-RaDWkAWT8M4B1W9EZCgLSvQOfKu2N4lDu_cbOU2UrXN1A2y5-0y2EpRu9m7xYIEM2lTlf6lzWzJtyO6sFfNE_KMUWdCcfCv9nTUJbQqZG3-KVUEgg";
// setting all header here
httpHeader = new System.Net.WebHeaderCollection();
httpHeader.Add("authorization", "Bearer " + token);
httpHeader.Add("prefer","return=representation"); //This is importand line to read a response from CRM other wise the body will blank and code as 204
try
{
new InteropPermission(InteropKind::ClrInterop).assert();
clrObj = System.Net.WebRequest::Create(parameter.CrmUrl+'('+_crmBillingID+')');
request = clrObj;
request.set_Headers(httpHeader);
request.set_Method('PATCH');
request.set_ContentType('application/json; charset=utf-8');
requestStream = request.GetRequestStream();
streamWriter = new System.IO.StreamWriter(requestStream);
select * from salesInvoiceBilling
where salesInvoiceBilling.RecId == _recid;
str jsonString = "";
//Writting jason Start
jsonWriter = new Newtonsoft.Json.Linq.JTokenWriter();//new Newtonsoft.Json.JsonTextWriter(streamWriter);
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("new_billingupdated"); //AXBillingUpdated
jsonWriter.WriteValue("True");
jsonWriter.WritePropertyName("new_recordid"); //AxRECID
jsonWriter.WriteValue(int642Str(_recid));
jsonWriter.WritePropertyName("new_invoicedate"); //AxINVOICEDATE
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.InvoiceDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
jsonWriter.WritePropertyName("new_customerpodate"); //AxCUSTOMERPODATE
if(salesInvoiceBilling.CustomerPODate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.CustomerPODate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_customerpono"); //AxCUSTOMERPONUM
jsonWriter.WriteValue(salesInvoiceBilling.CustomerPO);
// jsonWriter.WritePropertyName("new_dealidservice");
// jsonWriter.WriteValue(salesInvoiceBilling.);
jsonWriter.WritePropertyName("new_saletype"); //AxSALETYPE
jsonWriter.WriteValue(int2Str(this.getSaleType(salesInvoiceBilling.SaleType)));
jsonWriter.WritePropertyName("new_axproductname"); //AxPRODUCTNAME
jsonWriter.WriteValue(salesInvoiceBilling.ProductName);
jsonWriter.WritePropertyName("new_productcode"); //AxPRODUCTCODE
jsonWriter.WriteValue(salesInvoiceBilling.ProductCode);
jsonWriter.WritePropertyName("new_productrefcode"); //AxPRODREFERNO
jsonWriter.WriteValue(salesInvoiceBilling.ProductRefNo);
jsonWriter.WritePropertyName("new_productrefname"); //AxPRODREFERNAME
jsonWriter.WriteValue(salesInvoiceBilling.ProductRefName);
jsonWriter.WritePropertyName("new_customercode"); //AxCUSTCODE
jsonWriter.WriteValue(salesInvoiceBilling.CustomerId);
//jsonWriter.WritePropertyName("new_crmaccountname");
//jsonWriter.WriteValue(salesInvoiceBilling.CustName);
jsonWriter.WritePropertyName("new_contactperson"); //AxCONTACTPERSONNAME
jsonWriter.WriteValue(this.getDescriptionContactInformation(salesInvoiceBilling.CustomerId));
jsonWriter.WritePropertyName("new_contactno"); //AxCONTACTNO
jsonWriter.WriteValue(CustTable::find(salesInvoiceBilling.CustomerId).phone());
jsonWriter.WritePropertyName("new_contractno");// AxSALESID
jsonWriter.WriteValue(salesInvoiceBilling.SalesId);
jsonWriter.WritePropertyName("new_emailid");//AxEMAIL
jsonWriter.WriteValue(CustTable::find(salesInvoiceBilling.CustomerId).email());
jsonWriter.WritePropertyName("new_country"); //AxCOUNTRY
jsonWriter.WriteValue(CustTable::find(salesInvoiceBilling.CustomerId).countryName());
jsonWriter.WritePropertyName("new_legalentity"); //AxLEGALENTITY
jsonWriter.WriteValue(salesInvoiceBilling.DataAreaId);
jsonWriter.WritePropertyName("new_customeraccountmang"); //AxCUSTACCMANAGER
jsonWriter.WriteValue(HcmWorker::find(salesInvoiceBilling.CustAccountManager).name());
jsonWriter.WritePropertyName("new_technologymanager"); //AxTECHNOMANAGER
jsonWriter.WriteValue(HcmWorker::find(salesInvoiceBilling.TechManager).name());
jsonWriter.WritePropertyName("new_installationrequired_new"); //AxINSTALLATIONREQUIRED
jsonWriter.WriteValue(salesInvoiceBilling.InstallationRequired == NoYes::No ? "0" : "1");
jsonWriter.WritePropertyName("new_contracttype_new"); //AxCONTRACTTYPE
jsonWriter.WriteValue(int2Str(this.getContractType(salesInvoiceBilling.ContractType)));
jsonWriter.WritePropertyName("new_servicetype_new"); //AxSERVICEACTIVITYTYPE
jsonWriter.WriteValue(int2Str(this.getServiceActivityType(salesInvoiceBilling.ServiceActivityType)));
jsonWriter.WritePropertyName("new_supporttype_new"); //AxSUPPORTTYPE
jsonWriter.WriteValue(int2Str(this.getSupportType(salesInvoiceBilling.SupportType)));
jsonWriter.WritePropertyName("new_site"); //AxSITE
jsonWriter.WriteValue(salesInvoiceBilling.Site);
jsonWriter.WritePropertyName("new_siteaddress"); //AxSITEADDRESS
jsonWriter.WriteValue(salesInvoiceBilling.SiteAddress);
jsonWriter.WritePropertyName("new_oemcontractno"); //AxOEMCONTRACTNO
jsonWriter.WriteValue(salesInvoiceBilling.OEMContractNo);
jsonWriter.WritePropertyName("new_oemcontractstartdate"); //AxOEMCONTRACTSTARTDATE
if(salesInvoiceBilling.OEMStartDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.OEMStartDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_oemcontractenddate"); //AxOEMCONTRACTENDDATE
if(salesInvoiceBilling.OEMEndDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.OEMEndDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_producttype"); //AxPRODUCTTYPE
jsonWriter.WriteValue(int2Str(this.getProductType(salesInvoiceBilling.ProductType)));
jsonWriter.WritePropertyName("new_serialno"); //PRODREFERSERIALNO
jsonWriter.WriteValue(salesInvoiceBilling.ProductRefSerialNo);
jsonWriter.WritePropertyName("new_sycontractstartdate"); //AxCONTRACTSTARTDATE
if(salesInvoiceBilling.ContractStartDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.ContractStartDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_sycontractenddate"); //AxCONTRACTENDDATE
if(salesInvoiceBilling.ContractEndDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.ContractEndDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_invoiceno"); //AxINVOICEID
jsonWriter.WriteValue(salesInvoiceBilling.InvoiceId);
jsonWriter.WritePropertyName("new_syskillsetstartdate"); //AxSKILLSETSTARTDATE
if(salesInvoiceBilling.SkillsetStartDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.SkillsetStartDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_syskillsetenddate"); //AxSKILLSETENDDATE
if(salesInvoiceBilling.SkillsetEndDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.SkillsetEndDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_amcstartdate"); //AxAMCSTARTDATE send as a subscription date in CRM
if(salesInvoiceBilling.SubscriptionStartDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.SubscriptionStartDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("new_amcenddate"); //AxAMCENDDATE AxAMCSTARTDATE send as a subscription date in CRM
if(salesInvoiceBilling.SubscriptionEndDate)
{
jsonWriter.WriteValue(date2Str(salesInvoiceBilling.SubscriptionEndDate,
321,
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
));
}
else
{
jsonWriter.WriteValue('1900-01-01');
}
jsonWriter.WritePropertyName("syn_crmaccountnametext"); //AxCUSTNAME
jsonWriter.WriteValue(salesInvoiceBilling.CustName);
jsonWriter.WritePropertyName("syn_remarks"); //AXREMARKS
jsonWriter.WriteValue(salesInvoiceBilling.Remarks);
//"new_CRMAccountName@odata.bind": "/accounts(7a34c845-85ab-e811-a969-000d3af06848)"
jsonWriter.WritePropertyName("new_CRMAccountName@odata.bind"); //CRM Customer account lookup values needs to be identified as per GUID
jsonWriter.WriteValue(strFmt("/accounts(%1)", this.getAccountNameGuid(CustTable::find(salesInvoiceBilling.CustomerId).name(), token)));
// End of writting Json
jsonWriter.WriteEndObject();
clrObj = jsonWriter.get_Token();
jObject = clrObj;
reqBodyStr = jObject.ToString();
// Request
streamWriter.WriteAsync(reqBodyStr);
streamWriter.Flush();
streamWriter.Close();
//Response
new InteropPermission(InteropKind::DllInterop).assert();
response = request.GetResponse();
requestStream = response.GetResponseStream();
streamRead = new System.IO.StreamReader(requestStream);
jsonStr = streamRead.ReadToEnd();
// Description billingID = subStr(jsonStr,929, 964);
if(_crmBillingID)
{
// to update the billing id and crm insert status
ttsbegin;
// TECSalesInvoiceBillingDetails::findRecId(_recid,true);
salesInvoiceBilling.selectForUpdate(true);
salesInvoiceBilling.CRMUpdated = NoYes::Yes;
salesInvoiceBilling.doUpdate();
ttscommit;
}
// jsonStr = stringWriter.ToString();
}
catch (Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(ex.ToString());
if (ex != null)
{
ex = ex.get_InnerException();
if ((ex != null) && (ex is System.Net.WebException))
{
webException = ex as System.Net.WebException;
info(webexception.ToString());
}
}
}
catch (Exception::Error)
{
if(strLen(errorMsg)>1)
errorMsg+='\n';
infoEnumerator = SysInfologEnumerator::newData(infolog.cut());
while (infoEnumerator.moveNext())
{
infoMsgStruct = new SysInfologMessageStruct(infoEnumerator.currentMessage());
readException = infoEnumerator.currentException();
if(readException == Exception::Error || readException == Exception::Warning)
{
errorMsg += infoMsgStruct.message() + '\n';
info(webexception.ToString());
}
}
}
return jsonStr;
}
==========================================================================
/// <summary>
/// Generate the fresh token for the Bearer type at every request.
/// </summary>
/// <returns>
/// This will return the token generation from the Pass parameter
/// </returns>
public str generateToken()
{
#define.PostFormat('client_id=\%1&grant_type=\%2&resource=\%3&client_secret=\%4')
str rawResponse;
Map responseData;
RetailWebResponse responseLoc;
System.Text.Encoding utf8;
System.Byte[] byteArray;
TECAxCrmParameter parameter = TECAxCrmParameter::find();
str token;
str tokenUri = parameter.TokenGenerationURL;
str secretKey = parameter.passwordEdit(false, '');
str byteStr = parameter.ClientId + ':' + secretKey;
str resource = parameter.TokenResourceURL;//'https://syndromecrmuat.crm8.dynamics.com';
str postdata;
utf8 = System.Text.Encoding::get_UTF8();
byteArray = utf8.GetBytes(byteStr);
byteStr = System.Convert::ToBase64String(byteArray);
RetailCommonWebAPI webApi = RetailCommonWebAPI::construct();
str authHeader = strFmt('Authorization: Bearer %1',byteStr);
//important state to generate the Token start
SysUrlEncoder sysUrlEncoder = new SysUrlEncoder();
postdata = strFmt(#postFormat,
sysUrlEncoder.encode(parameter.ClientId),
sysUrlEncoder.encode('client_credentials'),
sysUrlEncoder.encode(resource),
sysUrlEncoder.encode(secretKey));
//important state to generate the token End
if(tokenUri && authHeader)
{
responseLoc = webApi.makePostRequest(tokenUri,postdata,authHeader);
}
if(responseLoc.parmHttpStatus() != 200)
{
rawResponse = responseLoc.parmData();
throw error("Failed to retrieve the token");
}
rawResponse = responseLoc.parmData();
responseData = RetailCommonWebAPI::getMapFromJsonString(rawResponse);
token = responseData.lookup("access_token");
// Info(token);
return token;
}
===========================================================================
/// <summary>
/// Convert Enum type to Its value to send to CRM.
/// </summary>
/// <returns>
/// Enum value :- TECSalesType
/// </returns>
public int getSaleType(TECSalesType _salestype)
{
int salesTypeValue;
switch(_salestype)
{
case TECSalesType::Direct :
salesTypeValue = Global::enum2int(TECSalesType::Direct);
break;
case TECSalesType::Indirect :
salesTypeValue = Global::enum2int(TECSalesType::Indirect);
break;
}
return salesTypeValue;
}
=====================================================================================
/// <summary>
/// Convert Enum type to Its value to send to CRM.
/// </summary>
/// <returns>
/// Enum value :- TECContractType
/// </returns>
public int getContractType(TECContractType _contractType)
{
int contractTypeValue;
switch(_contractType)
{
case TECContractType::BackToBack :
contractTypeValue = Global::enum2int(TECContractType::BackToBack);
break;
case TECContractType::None :
contractTypeValue = Global::enum2int(TECContractType::None);
break;
case TECContractType::Syndrome :
contractTypeValue = Global::enum2int(TECContractType::Syndrome);
break;
}
return contractTypeValue;
}
===========================================================================
/// <summary>
/// Convert Enum type to Its value to send to CRM.
/// </summary>
/// <returns>
/// Enum value :- TECServiceActivityType
/// </returns>
public int getServiceActivityType(TECServiceActivityType _serviceActivityType)
{
int ServiceActivityTypeValue;
switch(_serviceActivityType)
{
case TECServiceActivityType::Both :
ServiceActivityTypeValue = Global::enum2int(TECServiceActivityType::Both);
break;
case TECServiceActivityType::Contract :
ServiceActivityTypeValue = Global::enum2int(TECServiceActivityType::Contract);
break;
case TECServiceActivityType::None :
ServiceActivityTypeValue = Global::enum2int(TECServiceActivityType::None);
break;
case TECServiceActivityType::Skillenet :
ServiceActivityTypeValue = Global::enum2int(TECServiceActivityType::Skillenet);
break;
case TECServiceActivityType::Subscription :
ServiceActivityTypeValue = Global::enum2int(TECServiceActivityType::Subscription);
break;
}
return ServiceActivityTypeValue;
}
============================================================================
/// <summary>
/// Convert Enum type to Its value to send to CRM.
/// </summary>
/// <returns>
/// Enum value :- TECSupportType
/// </returns>
public int getSupportType(TECSupportType _supportType)
{
int supportTypeValue;
switch(_supportType)
{
case TECSupportType::Gold_8x5x4 :
supportTypeValue = Global::enum2int(TECSupportType::Gold_8x5x4);
break;
case TECSupportType::None :
supportTypeValue = Global::enum2int(TECSupportType::None);
break;
case TECSupportType::Platinum_24x7x4 :
supportTypeValue = Global::enum2int(TECSupportType::Platinum_24x7x4);
break;
case TECSupportType::Silver_8x5_NBD :
supportTypeValue = Global::enum2int(TECSupportType::Silver_8x5_NBD);
break;
case TECSupportType::_8x5xNBD :
supportTypeValue = Global::enum2int(TECSupportType::_8x5xNBD);
break;
}
return supportTypeValue;
}
===============================================================================
/// <summary>
/// Convert Enum type to Its value to send to CRM.
/// </summary>
/// <returns>
/// Enum value :- EcoResProductType
/// </returns>
public int getProductType(EcoResProductType _productType)
{
int productTypeValue;
switch(_productType)
{
case EcoResProductType::Item :
productTypeValue = Global::enum2int(EcoResProductType::Item);
break;
case EcoResProductType::Service :
productTypeValue = Global::enum2int(EcoResProductType::Service);
break;
}
return productTypeValue;
}
===============================================================================
public Description getDescriptionContactInformation(CustAccount _custAccount)
{
LogisticsElectronicAddress electronicAddress;
electronicAddress = DirParty::primaryElectronicAddress(CustTable::find(_custAccount).Party, LogisticsElectronicAddressMethodType::Phone);
if (electronicAddress)
{
Description description = electronicAddress.Description;
electronicAddress.dispose();
return description;
}
return '';
}
=======================================================================================
public str getAccountNameGuid(CustName _custName, str _token)
{
System.Net.HttpWebResponse response;
CLRObject clrObj;
System.Exception ex;
System.IO.Stream requestStream;
System.IO.StreamWriter streamWriter;
System.IO.StreamReader streamRead;
System.Net.WebHeaderCollection httpHeader, httpHeaderRes;
System.Net.HttpWebRequest request;
Notes reqBodyStr, resBodyStr;
str getAccountid;
str rawResponse;
Map responseData;
// str eqValue = _custName;
str token = _token;
// setting all header here
httpHeader = new System.Net.WebHeaderCollection();
httpHeader.Add("authorization", "Bearer " + token);
httpHeader.Add("prefer","return=representation"); // this is use to get the responses from the CRM and consume as an XMl document for the data sharing
try
{
new InteropPermission(InteropKind::ClrInterop).assert();
clrObj = System.Net.WebRequest::Create(strFmt('https://syndromecrmuat.api.crm8.dynamics.com/api/data/v9.2/accounts?$select=name&$filter=name%20eq%20%20%27%1%27', _custName)); //eq//System.Net.WebRequest::Create(parameters.EinvPDF+"?format=PDF&irns="+ewayNumber);
request = clrObj;
request.set_Headers(httpHeader);
request.set_Method('GET');
request.set_ContentType('application/json; charset=utf-8');
//Response
new InteropPermission(InteropKind::DllInterop).assert();
response = request.GetResponse();
streamRead = new System.IO.StreamReader(response.GetResponseStream());
resBodyStr = streamRead.ReadToEnd();
// rawResponse = response;//response.parmData();
responseData = RetailCommonWebAPI::getMapFromJsonString(resBodyStr);
// Get the value from the Map
mapEnumerator mapEnumerator;
Map map1;
List accountList;
ListEnumerator listEnumerator;
container accountID;
mapEnumerator = responseData.getEnumerator();
while ( mapEnumerator.moveNext())
{
switch (mapEnumerator.currentKey())
{
case "value" :
accountID = mapEnumerator.currentValue();
accountList =List::create(accountID);
listEnumerator = accountList.getEnumerator();
while (listEnumerator.moveNext())
{
accountID = listEnumerator.current();
map1 = Map::create(accountID);
//info(map1.lookup('accountid'));
getAccountid = map1.lookup("accountid");
}
break;
}
}
// accountid = responseData.lookup("Value");
// Info(getAccountid);
//info(strFmt('Report download : %1',filepaths));
}
catch
{
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
}
return getAccountid;
}
====================================================================================
Comments
Post a Comment