/// <summary>
        /// Returns list od opportunity models.
        /// </summary>
        /// <param name="opportunities"></param>
        /// <returns></returns>
        private List<OpportunityModel> getOpportunityModels(Opportunity[] opportunities)
        {
            List<OpportunityModel> opportunityModels = new List<OpportunityModel>();

            if (opportunities == null)
            {
                return opportunityModels;
            }

            foreach (var opportunity in opportunities)
            {
                var opp = new OpportunityModel
                {
                    KeyContactId = opportunity.KeyContactId,
                    PrimaryContactPartyName = opportunity.PrimaryContactPartyName,
                    SalesAccountId = opportunity.SalesAccountId,
                    TargetPartyName = opportunity.TargetPartyName,
                    OptyId = opportunity.OptyId,
                    OptyNumber = opportunity.OptyNumber,
                    PartyName1 = opportunity.PartyName1,
                    EmailAddress = opportunity.EmailAddress,
                    Name = opportunity.Name,
                    Description = opportunity.Description,
                    StatusCode = opportunity.StatusCode,
                    SalesMethod = opportunity.SalesMethod,
                    SalesStage = opportunity.SalesStage,
                    SalesChannelCd = opportunity.SalesChannelCd,
                    CurrencyCode = opportunity.CurrencyCode,
                    Revenue = opportunity.Revenue,
                    WinProb = opportunity.WinProb,
                    CreatedBy = opportunity.CreatedBy,
                    CreationDate = opportunity.CreationDate,
                    ForecastedCloseDate = opportunity.EffectiveDate
                };

                opportunityModels.Add(opp);

            }

            return opportunityModels;

        }
        /// <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 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;
        }