protected virtual void PMTemplateListSS_TemplateDefault_FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
        {
            PMTemplateListSS row = e.Row as PMTemplateListSS;

            if (row == null)
            {
                return;
            }
            object oldValue = sender.GetValue <PMTemplateListSS.templateDefault>(row);

            if (row.TemplateDefault != null && (bool)e.NewValue == true)
            {
                PXResultset <PMSSMapping> pmSSMappingRows = this.MappingSetup.Select();
                if (pmSSMappingRows.Count() > 0)
                {
                    PMSSMapping pmSSMappingRecord = pmSSMappingRows.RowCast <PMSSMapping>().Where(_ => !String.IsNullOrEmpty(_.NameAcu)).FirstOrDefault();
                    if (pmSSMappingRecord != null)
                    {
                        return;
                    }
                }
                e.NewValue = oldValue;
                sender.RaiseExceptionHandling <PMTemplateListSS.templateDefault>(e.Row, oldValue, new PXSetPropertyException(SmartsheetConstants.Messages.ERROR_TEMPLATE_DEFAULT, PXErrorLevel.RowError));
            }
        }
        protected virtual void PMTemplateListSS_TemplateDefault_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
        {
            PMTemplateListSS row = e.Row as PMTemplateListSS;

            if (row == null || row.TemplateDefault == null)
            {
                return;
            }

            PMSetup      setupRecord  = this.Base.Setup.Current as PMSetup;
            PMSetupSSExt pmSetupSSExt = PXCache <PMSetup> .GetExtension <PMSetupSSExt>(setupRecord);

            if ((bool)row.TemplateDefault)
            {
                PMTemplateListSS pmSSMappingRecord = this.TemplateSetup.Select().RowCast <PMTemplateListSS>().Where(_ => (bool)_.TemplateDefault == true && _.TemplateSS != row.TemplateSS).FirstOrDefault();
                if (pmSSMappingRecord != null)
                {
                    pmSSMappingRecord.TemplateDefault = false;
                }
                pmSetupSSExt.UsrSSTemplate = row.TemplateSS;
            }
            else
            {
                pmSetupSSExt.UsrSSTemplate = null;
            }
            this.Base.Setup.Update(setupRecord);
            TemplateSetup.View.RequestRefresh();
        }
        protected void PMSSMapping_RowInserted(PXCache cache, PXRowInsertedEventArgs e)
        {
            if (e.Row == null)
            {
                return;
            }

            PMSSMapping      mappingRow             = (PMSSMapping)e.Row;
            PMTemplateListSS pmTemplateListSSRecord = this.TemplateSetup.Current;

            mappingRow.TemplateSS = pmTemplateListSSRecord.TemplateSS;
        }
        public virtual void loadTemplateSS()
        {
            PMSetup pmSetupRow = this.Base.Setup.Current;
            Dictionary <string, string> templateSS          = GetTemplateSS();
            PXCache <PMTemplateListSS>  pmtemplateListCache = this.Base.Caches <PMTemplateListSS>();

            foreach (PMTemplateListSS item in TemplateSetup.Select())
            {
                pmtemplateListCache.Delete(item);
            }

            foreach (var templateRow in templateSS)
            {
                PMTemplateListSS ci = (PMTemplateListSS)pmtemplateListCache.Insert(new PMTemplateListSS
                {
                    TemplateSS      = templateRow.Key.ToString(),
                    TemplateName    = templateRow.Value.ToString(),
                    TemplateDefault = false
                });
            }
        }
        /// <summary>
        /// Gets the columns of the Sheet in SmartSheet
        /// </summary>
        /// <param name="refreshedToken">Smartsheet Token</param>
        /// <returns></returns>
        public Dictionary <string, string> GetColumnSheet(string refreshedToken = "")
        {
            PMTemplateListSS pmTemplateListSSRecord = this.TemplateSetup.Current;

            if (pmTemplateListSSRecord == null)
            {
                throw new PXException(SmartsheetConstants.Messages.ERROR_SETUP);
            }

            Users userRecord = PXSelect <
                Users,
                Where <Users.pKID, Equal <Required <AccessInfo.userID> > > >
                               .Select(this.Base, this.Base.Accessinfo.UserID);

            if (userRecord == null)
            {
                throw new PXException(SmartsheetConstants.Messages.ERROR_USER);
            }

            UsersSSExt userRecordSSExt = PXCache <Users> .GetExtension <UsersSSExt>(userRecord);

            if (userRecordSSExt == null)
            {
                throw new PXException(SmartsheetConstants.Messages.ERROR_USEREXT);
            }

            Dictionary <string, string> currentColumnMap = new Dictionary <string, string>();

            try
            {
                Token token = new Token();

                token.AccessToken = (String.IsNullOrEmpty(refreshedToken)) ? userRecordSSExt.UsrSmartsheetToken : refreshedToken;

                SmartsheetClient smartsheetClient = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build();

                if (!String.IsNullOrEmpty(pmTemplateListSSRecord.TemplateSS))
                {
                    Sheet sheet = new Sheet.CreateSheetFromTemplateBuilder(SmartsheetConstants.Messages.NAME_PROJECT_TEMP_SMARTSHEET, Convert.ToInt64(pmTemplateListSSRecord.TemplateSS)).Build();

                    //A temporary project is created in order to obtain the columns content
                    sheet = smartsheetClient.SheetResources.CreateSheet(sheet);

                    Sheet newlyCreatedSheet = smartsheetClient.SheetResources.GetSheet((long)sheet.Id, null, null, null, null, null, null, null);

                    currentColumnMap = new Dictionary <string, string>();
                    foreach (Column currentColumn in newlyCreatedSheet.Columns)
                    {
                        currentColumnMap.Add(currentColumn.Title, currentColumn.Type.ToString());
                    }

                    smartsheetClient.SheetResources.DeleteSheet((long)sheet.Id);
                }
                return(currentColumnMap);
            }
            catch (Exception e)
            {
                if (e.Message.Contains(SmartsheetConstants.SSConstants.EXPIRED_TOKEN_MESSAGE))
                {
                    MyProfileMaint    profileMaintGraph = PXGraph.CreateInstance <MyProfileMaint>();
                    MyProfileMaintExt graphExtended     = profileMaintGraph.GetExtension <MyProfileMaintExt>();
                    string            updatedToken      = graphExtended.RefreshSmartsheetToken();
                    return(GetColumnSheet(updatedToken));
                }
                else
                {
                    throw new PXException(e.Message);
                }
            }
        }