HariEko
HariEko

Reputation: 397

Fill value of selector field into another field in Acumatica ERP System

I already create new custom field for screen Fixed Asset. The following code is my DAC extension:

using PX.Data;
using PX.Objects.CR; 
using PX.Objects.CS;
using PX.Objects.EP;
using PX.Objects.FA;
using PX.Objects.GL;
using PX.Objects;
using System.Collections.Generic;
using System;

namespace SGLCustomizeProject
{
    public class FALocationHistoryExtension : PXCacheExtension<PX.Objects.FA.FALocationHistory>
    {
        #region UsrKodeArea
        [PXDBString(50)]
        [PXUIField(DisplayName = "Kode Area")]
        [PXSelector(typeof(Search<KodeAreaMaster.roomCD, 
                        Where<KodeAreaMaster.status, 
                        Equal<statusActive>, 
                        And<KodeAreaMaster.buildingID, 
                        Equal<Current<FALocationHistory.buildingID>>>>>),
                typeof(KodeAreaMaster.roomCD), 
                typeof(KodeAreaMaster.roomDescription),
                typeof(KodeAreaMaster.buildingID),
                typeof(KodeAreaMaster.status))]
        public virtual string UsrKodeArea { get; set; }
        public abstract class usrKodeArea : IBqlField { }
        #endregion

        #region UsrDeskripsiArea
        [PXDBString(75)]
        [PXUIField(DisplayName = "Deskripsi Area")]
        public virtual string UsrDeskripsiArea { get; set; }
        public abstract class usrDeskripsiArea : IBqlField { }
        #endregion
    }
}

I need to fill the selected value into another additional field in the current screen, please see the following screenshot:

enter image description here

I need to fill value of Deskripsi Area from selector field (pop up) into Deskripsi Area field.

I have tried the following code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using PX.Common;
using PX.Data;
using PX.Objects.CS;
using PX.Objects.CR;
using PX.Objects.CM;
using PX.Objects.GL;
using PX.Objects.AP;
using PX.Objects.EP;
using PX.Objects.IN;
using PX.Objects.FA.Overrides.AssetProcess;
using PX.Objects;
using PX.Objects.FA;

namespace SGLCustomizeProject
{
    public class AssetMaint_Extension : PXGraphExtension<AssetMaint>
    {
        public virtual void _(Events.FieldUpdated<FALocationHistory, FALocationHistoryExtension.usrKodeArea> e)
        {
            var row = e.Row;
            var ext = row.GetExtension<FALocationHistoryExtension>();
            e.Cache.SetValue<FALocationHistoryExtension.usrDeskripsiArea>(row, ext.UsrKodeArea);
        }
    }
}

This code above has been worked, but the result is when I choose Kode Area field, it also fill into Deskripsi Area field. My goal is to fill Deskripsi Area with the same field (Deskripsi Area) from selector field.

enter image description here

I tried to change code above with the following code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using PX.Common;
using PX.Data;
using PX.Objects.CS;
using PX.Objects.CR;
using PX.Objects.CM;
using PX.Objects.GL;
using PX.Objects.AP;
using PX.Objects.EP;
using PX.Objects.IN;
using PX.Objects.FA.Overrides.AssetProcess;
using PX.Objects;
using PX.Objects.FA;

namespace SGLCustomizeProject
{
    public class AssetMaint_Extension : PXGraphExtension<AssetMaint>
    {
        public virtual void _(Events.FieldUpdated<FALocationHistory, FALocationHistoryExtension.usrKodeArea> e)
        {
            var row = e.Row;
            var ext = row.GetExtension<FALocationHistoryExtension>();
            e.Cache.SetValue<FALocationHistoryExtension.usrDeskripsiArea>(row, ext.UsrDeskripsiArea);
        }
    }
}

But it doesn't work. Any step that I forget ?

Upvotes: 0

Views: 999

Answers (3)

HariEko
HariEko

Reputation: 397

I used the following code to provide my goal. Thanks for everyone for all the respons. :)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using PX.Common;
using PX.Data;
using PX.Objects.CS;
using PX.Objects.CR;
using PX.Objects.CM;
using PX.Objects.GL;
using PX.Objects.AP;
using PX.Objects.EP;
using PX.Objects.IN;
using PX.Objects.FA.Overrides.AssetProcess;
using PX.Objects;
using PX.Objects.FA;


namespace SGLCustomizeProject
{
    public class AssetMaint_Extension : PXGraphExtension<AssetMaint>
    {
        public virtual void _(Events.FieldUpdated<FALocationHistory, FALocationHistoryExtension.usrKodeArea> e)
        {
            var row = e.Row;
            var ext = row.GetExtension<FALocationHistoryExtension>();
            if (ext.UsrKodeArea != null)
            {
                e.Cache.SetValue<FALocationHistoryExtension.usrDeskripsiArea>(row, ext.UsrKodeArea);
                var kodeAreaMaster = PXSelect<KodeAreaMaster,
                                 Where<KodeAreaMaster.roomCD,
                                 Equal<Required<KodeAreaMaster.roomCD>>>>
                                 .Select(Base, ext.UsrKodeArea).First().GetItem<KodeAreaMaster>();
                e.Cache.SetValueExt<FALocationHistoryExtension.usrDeskripsiArea>(row, kodeAreaMaster.RoomDescription);
            }
        }

        public virtual void _(Events.FieldUpdated<FALocationHistory.buildingID> e)
        {
            var row = e.Row as FALocationHistory;
            var ext = row.GetExtension<FALocationHistoryExtension>();

            if (row.BuildingID != null)
            {
                if (ext.UsrKodeArea != null)
                {
                    var kodeAreaMaster = PXSelect<KodeAreaMaster,
                                 Where<KodeAreaMaster.roomCD,
                                 Equal<Required<KodeAreaMaster.roomCD>>>>
                                 .Select(Base, ext.UsrKodeArea).First().GetItem<KodeAreaMaster>();
                    if (row.BuildingID == kodeAreaMaster.BuildingID)
                    {
                        return;
                    }
                    else
                    {
                         e.Cache.SetValueExt<FALocationHistoryExtension.usrKodeArea>(row, null);
                        e.Cache.SetValueExt<FALocationHistoryExtension.usrDeskripsiArea>(row, null);
                    }
                }
            }
            else
            {
                e.Cache.SetValueExt<FALocationHistoryExtension.usrKodeArea>(row, null);
                e.Cache.SetValueExt<FALocationHistoryExtension.usrDeskripsiArea>(row, null);
            }
        }
    }
}

Upvotes: 0

Yuriy Zaletskyy
Yuriy Zaletskyy

Reputation: 5151

  1. use xxx_fieldUpdated event
  2. In your aspx page check, that you have commitchanges set to true
  3. [ as usually optional ] sometime is necessary to set autorefresh = true

Starting from 2017 R2 you can also use this approach:

    public virtual void _(Events.FieldUpdated<FALocationHistory, FALocationHistoryExtension.usrKodeArea> e)
    {
        var row = e.Row;
        var ext = row.GetExtension<FALocationHistoryExtension>();
        e.Cache.SetValue<FALocationHistoryExtension.usrDeskripsiArea>(row, ext.UsrKodeArea);
        var KodeAreaMaster =
            PXSelect<KodeAreaMaster, Where<KodeAreaMaster.roomCD, Equal<Required<KodeAreaMaster.roomCD>>>>
                .Select(Base, ext.UsrKodeArea).First().GetItem<KodeAreaMaster>();
        e.Cache.SetValueExt<FALocationHistoryExtension.usrDeskripsiArea>();

    }

Upvotes: 1

Vardan Vardanyan
Vardan Vardanyan

Reputation: 669

Change the ALocationHistoryExtension_UsrKodeArea_FieldUpdated to ALocationHistory_UsrKodeArea_FieldUpdated

protected virtual void FALocationHistory_UsrKodeArea_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
    {
        if (e.Row is FALocationHistory)
        {
            sender.SetDefaultExt<FALocationHistoryExtension.usrDeskripsiArea>(e.Row);
        }
    }   

To Override an Event Handler

Upvotes: 1

Related Questions