public Entity CreateContact(FreshdeskContactDto freshdeskContactDto) { Entity contact = new Entity("contact"); contact["isw_freshdeskid"] = freshdeskContactDto.Id.ToString(); if (freshdeskContactDto.CompanyId != null) { string[] columns = { "accountid" }; ConditionExpression ce = new ConditionExpression("isw_freshdeskid", ConditionOperator.Equal, freshdeskContactDto.CompanyId.ToString()); FilterExpression fe = new FilterExpression(); fe.AddCondition(ce); var ec = CrmServiceFactory.RetrieveEntities("account", columns, fe, 1); if (ec.Entities.Count > 0) { contact.Attributes["parentcustomerid"] = new EntityReference(ec.Entities[0].LogicalName, ec.Entities[0].Id); } } if (!string.IsNullOrEmpty(freshdeskContactDto.Email)) { contact["emailaddress1"] = freshdeskContactDto.Email; } if (!string.IsNullOrEmpty(freshdeskContactDto.Name)) { string[] nameSplit = freshdeskContactDto.Name.Split(' '); contact["firstname"] = nameSplit[0]; if (nameSplit.Length > 1) { contact["lastname"] = nameSplit[1]; } } contact["telephone1"] = freshdeskContactDto.Phone; if (!string.IsNullOrEmpty(freshdeskContactDto.Description)) { contact["description"] = freshdeskContactDto.Description; } if (!string.IsNullOrEmpty(freshdeskContactDto.Address)) { contact["address1_line1"] = freshdeskContactDto.Address; } if (!string.IsNullOrEmpty(freshdeskContactDto.JobTitle)) { contact["jobtitle"] = freshdeskContactDto.JobTitle; } if (!string.IsNullOrEmpty(freshdeskContactDto.Mobile)) { contact["telephone2"] = freshdeskContactDto.Mobile; } if (!string.IsNullOrEmpty(freshdeskContactDto.Name)) { contact["fullname"] = freshdeskContactDto.Name; } return(contact); }
public Entity CreateTicket(FreshdeskTicket ticket) { Entity ticketEntity = new Entity("incident"); //requesterId => customerid try { string[] columns = { "contactid" }; ConditionExpression ce = new ConditionExpression("isw_freshdeskid", ConditionOperator.Equal, ticket.RequesterId.ToString()); FilterExpression fe = new FilterExpression(); fe.AddCondition(ce); var ec = CrmServiceFactory.RetrieveEntities("contact", columns, fe, 1); if (ec.Entities.Count > 0) { ticketEntity.Attributes["customerid"] = new EntityReference(ec.Entities[0].LogicalName, ec.Entities[0].Id); } else { var r = _crmRepository.CreateContact(ticket.RequesterId); if (r != null) { ticketEntity.Attributes["customerid"] = new EntityReference("contact", r.Value); } else { return(null); } } } catch (Exception) { return(null); } //responder => ownerid try { string[] columns = { "systemuserid" }; ConditionExpression ce = new ConditionExpression("isw_freshdeskid", ConditionOperator.Equal, ticket.ResponderId.ToString()); FilterExpression fe = new FilterExpression(); fe.AddCondition(ce); var ec = CrmServiceFactory.RetrieveEntities("systemuser", columns, fe, 1); if (ec.Entities.Count > 0) { ticketEntity.Attributes["ownerid"] = new EntityReference(ec.Entities[0].LogicalName, ec.Entities[0].Id); ticketEntity.Attributes["isw_resolvedby"] = new EntityReference(ec.Entities[0].LogicalName, ec.Entities[0].Id); } } catch (Exception) {} if (!string.IsNullOrEmpty(ticket.Type)) { try { string[] columns = { "isw_issueid" }; ConditionExpression ce = new ConditionExpression("isw_name", ConditionOperator.Equal, ticket.Type); FilterExpression fe = new FilterExpression(); fe.AddCondition(ce); var ec = CrmServiceFactory.RetrieveEntities("isw_issue", columns, fe, 1); if (ec.Entities.Count > 0) { ticketEntity.Attributes["isw_issue"] = new EntityReference(ec.Entities[0].LogicalName, ec.Entities[0].Id); } } catch (Exception) { } } if (!string.IsNullOrEmpty(ticket.CustomFields.CfIssueSubCategory)) { try { string[] columns = { "isw_issuesubcategoryid" }; ConditionExpression ce = new ConditionExpression("isw_name", ConditionOperator.Equal, ticket.CustomFields.CfIssueSubCategory); FilterExpression fe = new FilterExpression(); fe.AddCondition(ce); var ec = CrmServiceFactory.RetrieveEntities("isw_issuesubcategory", columns, fe, 1); if (ec.Entities.Count > 0) { ticketEntity.Attributes["isw_issuesubcategory"] = new EntityReference(ec.Entities[0].LogicalName, ec.Entities[0].Id); } } catch (Exception) { } } if (!string.IsNullOrEmpty(ticket.CustomFields.CfIssueCategory)) { try { string[] columns = { "isw_issuecategoryid" }; ConditionExpression ce = new ConditionExpression("isw_name", ConditionOperator.Equal, ticket.CustomFields.CfIssueCategory); FilterExpression fe = new FilterExpression(); fe.AddCondition(ce); var ec = CrmServiceFactory.RetrieveEntities("isw_issuecategory", columns, fe, 1); if (ec.Entities.Count > 0) { ticketEntity.Attributes["isw_issuecategory"] = new EntityReference(ec.Entities[0].LogicalName, ec.Entities[0].Id); } } catch (Exception) { } } try { ticketEntity["prioritycode"] = new OptionSetValue(TicketPriority[ticket.Priority]); } catch (Exception) {} try { ticketEntity["caseorigincode"] = new OptionSetValue(TicketSource[ticket.Source]); } catch (Exception) { } if (!string.IsNullOrEmpty(ticket.Type)) { ticketEntity["title"] = ticket.Type; } ticketEntity["isw_freshdeskid"] = ticket.Id.ToString(); if (!string.IsNullOrEmpty(ticket.DescriptionText)) { ticketEntity["description"] = ticket.Subject; } if (ticket.Stats.FirstRespondedAt != null) { ticketEntity["isw_firstresponseforautocreatedcases"] = ticket.Stats.FirstRespondedAt.Value.DateTime; } if (ticket.Stats.FirstRespondedAt != null) { ticketEntity["isw_firstresponseforautocreatedcases"] = ticket.Stats.FirstRespondedAt.Value.DateTime; } if (ticket.Stats.ResolvedAt != null) { ticketEntity["isw_actualresolvedate"] = ticket.Stats.ResolvedAt.Value.DateTime; } if (ticket.Stats.ReopenedAt != null) { ticketEntity["isw_reactivateddate"] = ticket.Stats.ReopenedAt.Value.DateTime; } return(ticketEntity); }