Exemple #1
0
        /// <summary>
        /// Create an opportunity
        /// </summary>
        /// <param name="ownerPartyId"></param>
        /// <param name="orgExternalRef"></param>
        /// <param name="contactExternalRef"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public long?CreateOpportunity(long?ownerPartyId, long?orgExternalRef, long?contactExternalRef, string name)
        {
            IOpportunityService service = OpportunityService.GetService();
            OpportunityModel    model   = new OpportunityModel();

            model.Name = name;

            if (orgExternalRef != null)
            {
                model.TargetPartyId          = (long)orgExternalRef;
                model.TargetPartyIdSpecified = true;
            }

            model.OwnerResourcePartyId          = (long)ownerPartyId;
            model.OwnerResourcePartyIdSpecified = true;
            model.KeyContactId          = contactExternalRef;
            model.KeyContactIdSpecified = true;

            OpportunityResourceModel resourceModel = new OpportunityResourceModel();

            resourceModel.OwnerFlag           = true;
            resourceModel.OwnerFlagSpecified  = true;
            resourceModel.ResourceId          = (long)ownerPartyId;
            resourceModel.ResourceIdSpecified = true;

            model.OpportunityResourceModel = resourceModel;

            OpportunityModel result = service.CreateOpportunity(model);

            if (result != null && result.OpportunityId != null)
            {
                return(result.OpportunityId);
            }
            return(null);
        }
        /// <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);
        }