Reigner Ouano
Reigner Ouano

Reputation: 135

how to update a single row in my acumatica database

Hello I'm new and I am creating a customization project on my persist on Acumatica so I added a new field

public class UsersExt : PXCacheExtension<PX.SM.Users>
    {
        #region UsrMomentTrackID
        [PXDBString]
        [PXUIField(DisplayName = "Moment Track ID")]

        public virtual string UsrMomentTrackID { get; set; }
        public abstract class usrMomentTrackID : PX.Data.BQL.BqlString.Field<usrMomentTrackID> { }
        #endregion

I want to update loop on all field on the users database here is my current code but it returns me an error that its too long

  PXUpdate<Set<UsersExt.usrMomentTrackID, Add<UsersExt.usrMomentTrackID, Current<UsersExt.usrMomentTrackID>>>, Users,
                    Where<Users.pKID, Like<Users.pKID>>>.Update(Base, MomentTrackID, useritems.PKID);

Can you guide me on what I'm doing wrong or do we have a more effective way of updating a field on Acumatica database?

Upvotes: 0

Views: 1413

Answers (1)

Hugues Beaus&#233;jour
Hugues Beaus&#233;jour

Reputation: 8268

You can use PXSelect method to get the record.

With the PXCache object you can update and persist the record.

// 1. Select Users record
Guid? pKID = useritems.PKID;
Users user = PXSelect<Users, Where<Users.pKID, Equal<Required<Users.pKID>>>>.Select(Base, pKID);

if (user != null)
{
    // 2. Select Users DAC extension
    UsersExt userExt = user.GetExtension(user);

    if (userExt != null)
    {
        // 3. Set field value
        userExt.UsrMomentTrackID = MomentTrackID;

        // 4. Update record
        PXCache cache = Base.Caches[typeof(Users)];
        cache.Update(user);

        // 5. Persist record
        cache.Persist(user, PXDBOperation.Update);
    }
}

There's two context in which you can't use that method. Those are the RowPersisted event or the Persist method override. If you were to use the normal method to persist the record in those events it would call the same events again producing an infinite loop.

To workaround those issues you can use the PXDatabase class to persist directly to database. Note that this persisting method doesn't run DAC and Graph validations so it should be avoided as much as possible, it is not best practice to use that method but it can be helpful sometimes.

PXDatabase.Update<Users>(new PXDataFieldAssign<UsersExt.usrMomentTrackID>(MomentTrackID),
                         new PXDataFieldRestrict<Users.pKID>(useritems.PKID));

In general best practice is to add the custom field on screen, initialize it's value if necessary and let the end user persist the change by pressing the save button.

Upvotes: 2

Related Questions