/// <summary> /// Logs the request or response /// </summary> /// <param name="buffer"></param> /// <param name="message"></param> private void LogRequestResponse(MessageBuffer buffer, string message) { Message msg = buffer.CreateMessage(); StringBuilder sb = new StringBuilder(); using (System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(sb)) { msg.WriteMessage(xw); xw.Close(); } _logger.Debug(message, sb.ToString()); }
/// <summary> /// Get the OwnerPartyId /// </summary> /// <param name="accountModel">AccountModel</param> /// <returns></returns> public AccountModel GetOwnerPartyId(AccountModel accountModel) { AccountModel resultModel = null; if (accountModel != null) { try { DataObjectResult dataObjResult = _accountService._accountClient.getAccount(accountModel.PartyId); if (null == dataObjResult) { _logger.Debug("No Account found matching external reference " + accountModel.PartyId); resultModel = new AccountModel(); resultModel.OwnerPartyId = null; return(resultModel); } object[] dataObjResultValue = dataObjResult.Value; foreach (object value in dataObjResultValue) { resultModel = new AccountModel(); Account account = (Account)value; resultModel.OwnerPartyId = (long)account.OwnerPartyId; } } catch (Exception e) { _logger.Debug("Error occured while creating lead. Lead Not Created in Sales Cloud. Exception: " + e.StackTrace); _logger.Debug("Setting OwnerPartyId for Account to null"); resultModel = new AccountModel(); resultModel.OwnerPartyId = null; } } return(resultModel); }
/// <summary> /// Get the OwnerPartyId /// </summary> /// <param name="contactModel">ContactModel</param> /// <returns></returns> public ContactModel GetOwnerPartyId(ContactModel contactModel) { ContactModel resultModel = null; if (contactModel != null) { try { DataObjectResult dataObjResult = _contactService._contactClient.getContact(contactModel.PartyId); if (null == dataObjResult) { _logger.Debug("No Contact found matching external reference " + contactModel.PartyId); resultModel = new ContactModel(); resultModel.OwnerPartyId = null; return(resultModel); } object[] dataObjResultValue = dataObjResult.Value; foreach (object value in dataObjResultValue) { resultModel = new ContactModel(); Contact contact = (Contact)value; resultModel.OwnerPartyId = (long)contact.OwnerPartyId; } } catch (Exception e) { _logger.Debug("Error occured while creating lead. Lead Not Created in Sales Cloud. Exception: " + e.StackTrace); _logger.Debug("Setting OwnerPartyId for Contact to null"); resultModel = new ContactModel(); resultModel.OwnerPartyId = null; } } return(resultModel); }
/// <summary> /// Create an Opportunity in OSC /// </summary> /// <param name="opportunityModel">OpportunityModel</param> /// <returns></returns> public OpportunityModel CreateOpportunity(OpportunityModel opportunityModel) { OpportunityModel resultModel = null; try { if (opportunityModel != null) { Opportunity opportunity = new Opportunity(); opportunity.Name = opportunityModel.Name; opportunity.TargetPartyId = opportunityModel.TargetPartyId; opportunity.TargetPartyIdSpecified = opportunityModel.TargetPartyIdSpecified; opportunity.OwnerResourcePartyId = opportunityModel.OwnerResourcePartyId; opportunity.OwnerResourcePartyIdSpecified = opportunityModel.OwnerResourcePartyIdSpecified; opportunity.KeyContactId = opportunityModel.KeyContactId; opportunity.KeyContactIdSpecified = opportunityModel.KeyContactIdSpecified; OpportunityResource resource = new OpportunityResource(); resource.ResourceId = opportunityModel.OpportunityResourceModel.ResourceId; resource.ResourceIdSpecified = opportunityModel.OpportunityResourceModel.ResourceIdSpecified; resource.OwnerFlag = opportunityModel.OpportunityResourceModel.OwnerFlag; resource.OwnerFlagSpecified = opportunityModel.OpportunityResourceModel.OwnerFlagSpecified; OpportunityResource[] resources = new OpportunityResource[] { resource }; opportunity.OpportunityResource = resources; if (!OSCCommonUtil.ValidateCurrentSiteName()) { resultModel = new OpportunityModel(); resultModel.OpportunityId = OSCOpportunitiesCommon.DefaultOpportunitySalesLeadID; return(resultModel); } Opportunity result = _opportunityService._opportunityClient.createOpportunity(opportunity); resultModel = new OpportunityModel(); resultModel.OpportunityId = result.OptyId; } } catch (Exception exception) { _logger.Debug("Error occured while creating opportunity. Opportunity Not Created in Sales Cloud.", exception.StackTrace); MessageBox.Show(OSCExceptionMessages.LeadOpportunityCannotBeCreated, OSCExceptionMessages.LeadNotCreatedTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } return(resultModel); }
/// <summary> /// Create a ServiceLead in OSC /// </summary> /// <param name="leadModel">LeadModel</param> /// <returns></returns> public LeadModel CreateServiceLead(LeadModel leadModel) { LeadModel resultModel = null; try { if (leadModel != null) { SalesLead lead = new SalesLead(); lead.Name = leadModel.Name; lead.CustomerId = leadModel.CustomerId; lead.CustomerIdSpecified = leadModel.CustomerIdSpecified; lead.PrimaryContactId = leadModel.PrimaryContactId; lead.PrimaryContactIdSpecified = leadModel.PrimaryContactIdSpecified; lead.OwnerId = leadModel.OwnerId; lead.OwnerIdSpecified = leadModel.OwnerIdSpecified; if (!OSCCommonUtil.ValidateCurrentSiteName()) { resultModel = new LeadModel(); resultModel.LeadId = OSCOpportunitiesCommon.DefaultOpportunitySalesLeadID; return(resultModel); } SalesLeadResult leadResult = _leadService._leadClient.createSalesLead(lead); var result = leadResult.Value.FirstOrDefault(); if (result != null) { resultModel = new LeadModel(); resultModel.LeadId = result.LeadId; } } } catch (Exception exception) { _logger.Debug("Error occured while creating lead. Lead Not Created in Sales Cloud. Exception: " + exception.StackTrace); MessageBox.Show(OSCExceptionMessages.LeadOpportunityCannotBeCreated, OSCExceptionMessages.LeadNotCreatedTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } return(resultModel); }
/// <summary> /// Method to fetch Opportunity and return respective models. /// </summary> /// <param name="context"></param> /// <returns>List of Opportunity Models</returns> private List <OpportunityModel> getOpportunitesList(IRecordContext context) { var opportunitiesList = new List <OpportunityModel>(); if (context != null) { var tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; Boolean timeout = false; string timeoutValue = RightNowConfigService.GetConfigValue(RightNowConfigKeyNames.SalesTimeout); var orgRecord = context.GetWorkspaceRecord(WorkspaceRecordType.Organization) as IOrganization; if (orgRecord != null) { string ext_ref = RightNowConnectService.GetService().GetContactOrgExternalReference(orgRecord.ID, false, OracleRightNowOSCAddInNames.OpportunityReportTableAddIn); //If the contact is a non sales contact if (String.IsNullOrEmpty(ext_ref)) { _logger.Debug("External reference is empty or null for the org. Returning empty opportunity list."); return(opportunitiesList); } if (!String.IsNullOrEmpty(timeoutValue)) { var task = Task.Factory.StartNew(() => OpportunityService.GetService().FindOpenOpportunities(OSCOpportunitiesTableMetadata.OrgFilterColumn, ext_ref), token); if (!task.Wait(Convert.ToInt32(timeoutValue))) { timeout = true; tokenSource.Cancel(); } else { opportunitiesList = task.Result; task.Dispose(); } } else { _logger.Debug("Sales Timesout value is either empty or null"); opportunitiesList = OpportunityService.GetService().FindOpenOpportunities(OSCOpportunitiesTableMetadata.OrgFilterColumn, ext_ref); } _logger.Debug("No. of opportunities fetched for Org #" + ext_ref + " : " + ((opportunitiesList != null) ? opportunitiesList.Count.ToString() : "null")); } else { var contactRecord = context.GetWorkspaceRecord(WorkspaceRecordType.Contact) as IContact; if (contactRecord != null) { string ext_ref = RightNowConnectService.GetService().GetContactOrgExternalReference(contactRecord.ID, true, OracleRightNowOSCAddInNames.OpportunityReportTableAddIn); //If the contact is a non sales contact if (String.IsNullOrEmpty(ext_ref)) { _logger.Debug("External reference is empty or null for contact. Returning empty opportunity list."); return(opportunitiesList); } if (!String.IsNullOrEmpty(timeoutValue)) { var task = Task.Factory.StartNew(() => OpportunityService.GetService().FindOpenOpportunities(OSCOpportunitiesTableMetadata.ContactFilterColumn, ext_ref), token); if (!task.Wait(Convert.ToInt32(timeoutValue))) { timeout = true; tokenSource.Cancel(); } else { opportunitiesList = task.Result; task.Dispose(); } } else { _logger.Debug("Sales Timesout value is either empty or null"); opportunitiesList = OpportunityService.GetService().FindOpenOpportunities(OSCOpportunitiesTableMetadata.ContactFilterColumn, ext_ref); } _logger.Debug("No. of opportunities fetched for Contact #" + ext_ref + " : " + ((opportunitiesList != null) ? opportunitiesList.Count.ToString() : "null")); } } if (timeout) { _logger.Debug("FindOpportunity request timed out!"); MessageBox.Show(OSCExceptionMessages.FindOppTimedOut, OSCOpportunitiesCommon.FindOppTimedOutTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } return(opportunitiesList); }
/// <summary> /// /// </summary> /// <param name="recordContext"></param> /// <param name="orgExternalRef"></param> /// <param name="contactExternalRef"></param> /// <param name="contactID"></param> /// <param name="orgID"></param> /// <param name="e"></param> /// <returns></returns> public bool validateRequest(IRecordContext recordContext, long?orgExternalRef, long?contactExternalRef, int?contactID, int?orgID, System.ComponentModel.CancelEventArgs e) { logger.Debug("ValidateRequest Start"); bool isSuccess = true; logger.Debug("Validate Create or Update"); //Validate Record Status isSuccess = validateCreateOrUpdate(recordContext); logger.Debug("Validate Create or Update done. Validation status=" + isSuccess); if (!isSuccess) { //validation failed, cancel save operation e.Cancel = true; return(isSuccess); } logger.Debug("Validate opportunity type"); //Validate lead_oppty_type isSuccess = validateLeadOpportunityType(recordContext); logger.Debug("validate opportunity type done. validation status=" + isSuccess); if (!isSuccess) { //Set submit_status to failed //ICustomObject leadOpportunityObject = recordContext.GetWorkspaceRecord(recordContext.WorkspaceTypeName) as ICustomObject; //setFieldLeadOpportunity(leadOpportunityObject, OSCOpportunitiesCommon.LeadOpportunityStatusFieldName, (object)OSCOpportunitiesCommon.LeadOpportunityFailedStatus); //validation failed, cancel save operation e.Cancel = true; return(isSuccess); } logger.Debug("Validate contact is mandatory or not"); //Validate Contact mandatory isSuccess = validateMandatoryContact(contactID); logger.Debug("Validation mandatory contact is done. Validation status=" + isSuccess); if (!isSuccess) { //validation failed, cancel save operation e.Cancel = true; return(isSuccess); } logger.Debug("Validate external reference"); //Validate External Reference isSuccess = validateExternalReference(recordContext, orgExternalRef, contactExternalRef, orgID); if (!isSuccess) { //Set submit_status to failed ICustomObject leadOpportunityObject = recordContext.GetWorkspaceRecord(recordContext.WorkspaceTypeName) as ICustomObject; setFieldLeadOpportunity(leadOpportunityObject, OSCOpportunitiesCommon.LeadOpportunityStatusFieldName, (object)OSCOpportunitiesCommon.LeadOpportunityFailedStatus); } logger.Debug("Validate external reference done. validation status=" + isSuccess); return(isSuccess); }
public void Handler() { LeadOpportunityViewModel leadOpportunityViewModel = new LeadOpportunityViewModel(); IContact contactRecord = _recordContext.GetWorkspaceRecord(WorkspaceRecordType.Contact) as IContact; IOrganization orgRecord = _recordContext.GetWorkspaceRecord(WorkspaceRecordType.Organization) as IOrganization; long?orgExternalRef = null, contactExternalRef = null; int? contactId = null, orgID = null; if (orgRecord != null) { orgExternalRef = leadOpportunityViewModel.GetContactOrgExternalReference(orgRecord.ID, false, OracleRightNowOSCAddInNames.LeadOpportunityAddIn); orgID = orgRecord.ID; } if (contactRecord != null) { contactExternalRef = leadOpportunityViewModel.GetContactOrgExternalReference(contactRecord.ID, true, OracleRightNowOSCAddInNames.LeadOpportunityAddIn); contactId = contactRecord.ID; } //validate external refId logger.Debug("Validating request"); if (!leadOpportunityViewModel.validateRequest(_recordContext, orgExternalRef, contactExternalRef, contactId, orgID, cancelSaveOperation)) { logger.Debug("Request validation failed, see logs for more details"); return; } logger.Debug("Request validation passed"); long?ownerPartyId = null; if (orgExternalRef != null) { ownerPartyId = leadOpportunityViewModel.GetOwnerPartyId(orgExternalRef); } else if (contactExternalRef != null) { ownerPartyId = leadOpportunityViewModel.GetOwnerPartyIdForContact(contactExternalRef); } String opportunityName = (String)leadOpportunityViewModel.getFieldFromLeadOpportunity(_leadOpportunityRecord, OSCOpportunitiesCommon.LeadOpportunityLeadName); if (opportunityName != null) { opportunityName = opportunityName + " " + System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); } String leadType = (String)leadOpportunityViewModel.getFieldFromLeadOpportunity(_leadOpportunityRecord, OSCOpportunitiesCommon.LeadTypeFieldName); if (ownerPartyId == null) { logger.Debug("Invalid ownerPartyId. Lead Not Created in Sales Cloud"); leadOpportunityViewModel.setFieldLeadOpportunity(_leadOpportunityRecord, OSCOpportunitiesCommon.LeadOpportunityStatusFieldName, OSCOpportunitiesCommon.LeadOpportunityFailedStatus); MessageBox.Show(OSCExceptionMessages.SalesAccountNotExistMessage, OSCExceptionMessages.LeadNotCreatedTitle, MessageBoxButton.OK, MessageBoxImage.Error); return; } if (leadType.Equals(OSCOpportunitiesCommon.OpportunityRecordType)) { long?opportunityId = null; if (ownerPartyId != null) { opportunityId = leadOpportunityViewModel.CreateOpportunity(ownerPartyId, orgExternalRef, contactExternalRef, opportunityName); logger.Debug("Got the opportunity Id " + opportunityId); } leadOpportunityViewModel.setFieldLeadOpportunity(_leadOpportunityRecord, OSCOpportunitiesCommon.LeadOpportunityStatusFieldName, opportunityId != null && opportunityId > 0 ? OSCOpportunitiesCommon.LeadOpportunitySuccessStatus : OSCOpportunitiesCommon.LeadOpportunityFailedStatus); if (opportunityId != null) { leadOpportunityViewModel.setFieldLeadOpportunity(_leadOpportunityRecord, OSCOpportunitiesCommon.LeadOpportunityExtRef, opportunityId.ToString()); leadOpportunityViewModel.setFieldLeadOpportunity(_leadOpportunityRecord, OSCOpportunitiesCommon.LeadOpportunityLeadName, opportunityName); } } else if (leadType.Equals(OSCOpportunitiesCommon.SalesLeadRecordType)) { long?leadId = null; if (ownerPartyId != null) { leadId = leadOpportunityViewModel.CreateServiceLead(ownerPartyId, orgExternalRef, contactExternalRef, opportunityName); logger.Debug("Got the lead Id " + leadId); } leadOpportunityViewModel.setFieldLeadOpportunity(_leadOpportunityRecord, OSCOpportunitiesCommon.LeadOpportunityStatusFieldName, leadId != null && leadId > 0 ? OSCOpportunitiesCommon.LeadOpportunitySuccessStatus : OSCOpportunitiesCommon.LeadOpportunityFailedStatus); if (leadId != null) { leadOpportunityViewModel.setFieldLeadOpportunity(_leadOpportunityRecord, OSCOpportunitiesCommon.LeadOpportunityExtRef, leadId.ToString()); leadOpportunityViewModel.setFieldLeadOpportunity(_leadOpportunityRecord, OSCOpportunitiesCommon.LeadOpportunityLeadName, opportunityName); } } }