protected virtual void EPEmployee_UsrSSUserID_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e) { if (e.Row == null) { return; } EPEmployee epEmployeeRow = (EPEmployee)e.Row; EPEmployeeSSExt epEmployeeExtRow = PXCache <EPEmployee> .GetExtension <EPEmployeeSSExt>(epEmployeeRow); if ((long?)e.OldValue != epEmployeeExtRow.UsrSSUserid) { PXCache <EPUsersListSS> usersListCache = this.Base.Caches <EPUsersListSS>(); EPUsersListSS epUsersListSSRow = UserList.Select().Where(x => ((EPUsersListSS)x).Ssuserid == (long?)e.OldValue).FirstOrDefault(); if (epUsersListSSRow != null) { epUsersListSSRow.BAccountID = null; usersListCache.Update(epUsersListSSRow); } if (epEmployeeExtRow.UsrSSUserid != null) { epUsersListSSRow = UserList.Select().Where(x => ((EPUsersListSS)x).Ssuserid == epEmployeeExtRow.UsrSSUserid).FirstOrDefault(); if (epUsersListSSRow != null) { epUsersListSSRow.BAccountID = epEmployeeRow.BAccountID; usersListCache.Update(epUsersListSSRow); } } } }
/// <summary> /// Links the Smartsheet user information to the Acumatica's Employee /// </summary> /// <param name="projectEntryGraph">Project graph</param> /// <param name="smartsheetClient">Smartsheet SDK Client</param> public void CreateEmployeesAcuUserSS(ProjectEntry projectEntryGraph, SmartsheetClient smartsheetClient) { User userRow = new User(); AccessUsers accessUsersGraph = PXGraph.CreateInstance <AccessUsers>(); AccessUsersSSIExt graphExtended = accessUsersGraph.GetExtension <AccessUsersSSIExt>(); try { EmployeeMaint employeeMaintGraph = PXGraph.CreateInstance <EmployeeMaint>(); foreach (EPEmployeeContract filter in projectEntryGraph.EmployeeContract.Select()) { EPEmployee ePEmployeeRecord = employeeMaintGraph.Employee.Current = employeeMaintGraph.Employee.Search <EPEmployee.bAccountID>(filter.EmployeeID); if (ePEmployeeRecord == null) { return; } EPEmployeeSSExt ePEmployeeExtRecord = PXCache <EPEmployee> .GetExtension <EPEmployeeSSExt>(ePEmployeeRecord); if (String.IsNullOrEmpty(Convert.ToString(ePEmployeeExtRecord.UsrSSUserid))) { userRow = graphExtended.CreateSmartsheetUser(filter.EmployeeID, smartsheetClient); if (userRow != null) { PXCache <EPUsersListSS> usersListCache = employeeMaintGraph.Caches <EPUsersListSS>(); EPUsersListSS epUsersListSSRow = UserSSList.Select().Where(x => ((EPUsersListSS)x).Ssuserid == userRow.Id).FirstOrDefault(); if (epUsersListSSRow == null) { EPUsersListSS usersListSSRow = (EPUsersListSS)usersListCache.Insert(new EPUsersListSS { Ssuserid = userRow.Id, FirstName = userRow.FirstName, LastName = userRow.LastName, Email = userRow.Email, BAccountID = filter.EmployeeID }); } else { epUsersListSSRow.BAccountID = filter.EmployeeID; epUsersListSSRow.FirstName = userRow.FirstName; epUsersListSSRow.LastName = userRow.LastName; epUsersListSSRow.Email = userRow.Email; usersListCache.Update(epUsersListSSRow); } employeeMaintGraph.Persist(); PXDatabase.Update <EPEmployee>(new PXDataFieldRestrict <EPEmployee.bAccountID>(filter.EmployeeID), new PXDataFieldAssign <EPEmployeeSSExt.usrSSUserID>(userRow.Id)); } } } } catch (Exception ex) { throw new PXException(ex.Message); } }
/// <summary> /// Creates or updates users obtained from SmartSheet in Acumatica /// </summary> /// <param name="refreshedToken"></param> public void GetUsersSS(string refreshedToken = "") { // User connection with SmartSheet 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); try { Token token = new Token(); token.AccessToken = (String.IsNullOrEmpty(refreshedToken)) ? userRecordSSExt.UsrSmartsheetToken : refreshedToken; SmartsheetClient smartsheetClient = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build(); PaginatedResult <User> smartsheetUserSet = smartsheetClient.UserResources.ListUsers(null, null); if (smartsheetUserSet.TotalCount > 0) { PXCache <EPUsersListSS> usersListCache = this.Base.Caches <EPUsersListSS>(); foreach (User dataUsers in smartsheetUserSet.Data) { EPUsersListSS epUsersListSSRow = UserList.Select().Where(x => ((EPUsersListSS)x).Ssuserid == dataUsers.Id).FirstOrDefault(); if (epUsersListSSRow == null) { EPUsersListSS ci = (EPUsersListSS)usersListCache.Insert(new EPUsersListSS { Ssuserid = dataUsers.Id, FirstName = dataUsers.FirstName, LastName = dataUsers.LastName, Email = dataUsers.Email }); } else { epUsersListSSRow.FirstName = dataUsers.FirstName; epUsersListSSRow.LastName = dataUsers.LastName; epUsersListSSRow.Email = dataUsers.Email; usersListCache.Update(epUsersListSSRow); } } this.Base.Persist(); } } 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(); GetUsersSS(updatedToken); } else { throw new PXException(e.Message); } } }
/// <summary> /// Creates or updates the smartsheet users listed in "EPUsersListSS" master table /// </summary> /// <param name="refreshedToken"></param> public void GetUsersSS(string refreshedToken = "") { //The smartsheet account should support the UserResources.ListUsers() functionality //Otherwise the message "The operation you are attempting to perform is not supported by your plan." is received // User connection with SmartSheet 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); try { Token token = new Token(); token.AccessToken = (String.IsNullOrEmpty(refreshedToken)) ? userRecordSSExt.UsrSmartsheetToken : refreshedToken; SmartsheetClient smartsheetClient = new SmartsheetBuilder() .SetAccessToken(token.AccessToken) .SetDateTimeFixOptOut(true) //See NOTE ON 2.93.0 RELEASE on https://github.com/smartsheet-platform/smartsheet-csharp-sdk .Build(); PaginatedResult <User> smartsheetUserSet = smartsheetClient.UserResources.ListUsers(null, null); if (smartsheetUserSet.TotalCount > 0) { PXCache <EPUsersListSS> usersListCache = this.Base.Caches <EPUsersListSS>(); foreach (User dataUsers in smartsheetUserSet.Data) { EPUsersListSS epUsersListSSRow = UserList.Select().Where(x => ((EPUsersListSS)x).Ssuserid == dataUsers.Id).FirstOrDefault(); if (epUsersListSSRow == null) { EPUsersListSS ci = (EPUsersListSS)usersListCache.Insert(new EPUsersListSS { Ssuserid = dataUsers.Id, FirstName = dataUsers.FirstName, LastName = dataUsers.LastName, Email = dataUsers.Email }); } else { epUsersListSSRow.FirstName = dataUsers.FirstName; epUsersListSSRow.LastName = dataUsers.LastName; epUsersListSSRow.Email = dataUsers.Email; usersListCache.Update(epUsersListSSRow); } } this.Base.Persist(); } } 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(); GetUsersSS(updatedToken); } else { throw new PXException(e.Message); } } }