Please
refer to my previous blog here which list out D365 CRM Web API methods in a
generic format so we can focus on performing business logic.
Here, I am going to showcase how to call the RETREIVE operation and prepare canonical
around it.
Testing the GET Operation
Starting with Crm Lead Entity class (a Crm Data Contract) with the fields declared that is expected as output from D365 WEB Api.
Starting with Crm Lead Entity class (a Crm Data Contract) with the fields declared that is expected as output from D365 WEB Api.
Note:
We need NuGet Packages for Assembly Newtonsoft.Json for JsonProperty
public class SingleCrmLeadGet
{
[JsonProperty("@odata.context")]
public string odata_context { get; set; }
[JsonProperty("@odata.etag")]
public string odata_etag { get; set; }
[JsonProperty("subject")]
public string subject { get; set; }
[JsonProperty("firstname")]
public string firstname { get; set; }
[JsonProperty("lastname")]
public string lastname { get; set; }
[JsonProperty("leadid")]
public string leadid { get; set; }
[JsonProperty("mobilephone")]
public string mobilephone { get; set; }
[JsonProperty("_ccs_accountmanagerid_value")]
public string accountmanagerid_value { get; set; }
[JsonProperty("_ccs_accountmanagerid_value@OData.Community.Display.V1.FormattedValue")]
public string accountmanagerid_name { get; set; }
[JsonProperty("_ccs_contracttypeid_value")]
public string contracttypeid_value { get; set; }
[JsonProperty("_ccs_contracttypeid_value@OData.Community.Display.V1.FormattedValue")]
public string contracttypeid_name { get; set; }
[JsonProperty("ccs_rfpid")]
public string rfpid { get; set; }
[JsonProperty("ccs_rfpissuedate")]
public string rfpissuedate { get; set; }
[JsonProperty("_parentaccountid_value")]
public string parentaccountid_value { get; set; }
[JsonProperty("_parentaccountid_value@OData.Community.Display.V1.FormattedValue")]
public string parentaccountid_name { get; set; }
}
Now,
we need to write a Business Data Contract that other consumer of our Web API
would
get a response and we do not need to scare them with CRM naming
conventions.
[DataContract(Name = "GetSingleLeadResponse")]
public class GetSingleLeadResponse
{
public string LeadId {
get; set; }
public string
FirstName { get; set; }
public string LastName
{ get; set; }
public string
PhoneNumber { get; set; }
public string
ProjectName { get; set; }
public string
AccountManagerId_Value { get; set; }
public string
AccountManagerId_Name { get; set; }
public string
ContractTypeId_Value { get; set; }
public string
ContractTypeId_Name { get; set; }
public string RfpId { get; set; }
public string
RfpIssueDate { get; set; }
public string
ParentAccountId_Value { get; set; }
public string
ParentAccountId_Name { get; set; }
}
Method
to map Dynamic Crm response to business data contract.
protected GetSingleLeadResponse MapLeadResponse(SingleCrmLeadGet crmLead)
{
GetSingleLeadResponse leadRes = null;
if (crmLead != null)
{
leadRes = new GetSingleLeadResponse();
leadRes.LeadId =
crmLead.leadid;
leadRes.FirstName =
crmLead.firstname;
leadRes.LastName =
crmLead.lastname;
leadRes.ProjectName =
crmLead.subject;
leadRes.PhoneNumber =
crmLead.mobilephone;
leadRes.AccountManagerId_Value = crmLead.accountmanagerid_value;
leadRes.AccountManagerId_Name
= crmLead.accountmanagerid_name;
leadRes.ContractTypeId_Value = crmLead.contracttypeid_value;
leadRes.ContractTypeId_Name = crmLead.contracttypeid_name;
leadRes.RfpId =
crmLead.rfpid;
leadRes.RfpIssueDate
= crmLead.rfpissuedate.ToString();
leadRes.ParentAccountId_Value = crmLead.parentaccountid_value;
leadRes.ParentAccountId_Name = crmLead.parentaccountid_name;
}
return leadRes;
}
Finally, here is the calling GetLeadBy - leadId (Guid)
public async Task<GetSingleLeadResponse> GetLeadById(string leadid)
{
GetSingleLeadResponse businessLeadRes = null;
string entityUrl = "leads(" + leadid + ")?";
string selectQuery = "$select=_ccs_accountmanagerid_value,_ccs_contracttypeid_value,ccs_rfpid,
ccs_rfpissuedate,_parentaccountid_value,subject,mobilephone,firstname,lastname";
var result = await GetEntity(entityUrl +
selectQuery);
if (result != null)
{
businessLeadRes = MapLeadResponse(JsonConvert.DeserializeObject(result));
}
return businessLeadRes;
}
Now our custom Web API controller method.
public class LeadController : ApiController
{
public async Task<GetSingleLeadResponse>
Get(string leadId)
{
CrmLeadLogic obj = new
CrmLeadLogic();
var busLeadRes = await
obj.GetLeadById(leadId);
return busLeadRes;
}
}
POST MAN Response
No comments:
Post a Comment