Reputation: 7
I have a requirement on which the Ship-to Contact & Address in Shipments screen (for Transfer only) will be overridden by using the location of Customer selected from UDF.
A piece of code that I used to test:
#region AddressLine1
[PXMergeAttributes(Method = MergeMethod.Merge)]
[PXDBScalar(typeof(Search2<SOAddress.addressLine1,
InnerJoin<SOShipment, On<SOAddress.customerID, Equal<SOShipment.customerID>>,
InnerJoin<BAccount, On<BAccount.bAccountID, Equal<SOShipment.customerID>>,
InnerJoin<SOShipmentKvExt, On<SOShipment.noteID, Equal<SOShipmentKvExt.recordID>>>>>,
Where<BAccount.acctCD, Equal<Current<SOShipmentKvExt.valueString>>>>))]
public string AddressLine1 { get; set; }
#endregion
Using the above code, the Address Line 1 field remains empty despite the customer in UDF being selected.
I would appreciate an easy-to-understand explanation since I'm fairly new in C#. Thanks!
Edit:
I managed to populate the Ship-to-Contact and Ship-to-Address fields by using the following code:
My DAC Extension:
public class SOShipmentExt : PXCacheExtension<PX.Objects.SO.SOShipment>
{
#region UsrCustomerID
[CustomerActive(DescriptionField = typeof(Customer.acctName))]
[PXUIField(DisplayName = "Customer ID")]
[PXDefault(PersistingCheck = PXPersistingCheck.Nothing)]
public virtual int? UsrCustomerID { get; set; }
public abstract class usrCustomerID : PX.Data.BQL.BqlInt.Field<usrCustomerID> { }
#endregion
#region UsrShipAddressID
[PXDBInt()]
[PXUIField(DisplayName = "Ship Address ID")]
public virtual Int32? UsrShipAddressID { get; set; }
public abstract class usrShipAddressID : PX.Data.BQL.BqlInt.Field<usrShipAddressID> { }
#endregion
#region UsrShipContactID
[PXDBInt()]
[PXUIField(DisplayName = "Ship Contact ID")]
public virtual Int32? UsrShipContactID { get; set; }
public abstract class usrShipContactID : PX.Data.BQL.BqlInt.Field<usrShipContactID> { }
#endregion
}
My Graph Extension:
public class SOShipmentEntryExt : PXGraphExtension<SOShipmentEntry>
{
#region Event Handlers
public virtual void SetShipAddressAndContact(SOShipment shipment, int? shipAddressID, int? shipContactID)
{
SOShipmentExt sOShipmentExt = shipment.GetExtension<SOShipmentExt>();
foreach (SOShipmentAddress address in Base.Shipping_Address.Select())
{
if (address.AddressID < 0)
{
Base.Shipping_Address.Delete(address);
}
}
foreach (SOShipmentContact contact in Base.Shipping_Contact.Select())
{
if (contact.ContactID < 0)
{
Base.Shipping_Contact.Delete(contact);
}
}
}
protected virtual void SOShipment_UsrCustomerID_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
{
SOShipment row = (SOShipment)e.Row;
SOShipmentExt sOShipmentExt = row.GetExtension<SOShipmentExt>();
SOShipmentAddress sOShipment = SelectFrom<SOShipmentAddress>.Where<SOShipmentAddress.customerID.IsEqual<@P.AsInt>>.View.Select(this.Base, sOShipmentExt.UsrCustomerID);
SOShipmentContact sOShipmentContact = SelectFrom<SOShipmentContact>.Where<SOShipmentContact.customerID.IsEqual<@P.AsInt>>.View.Select(this.Base, sOShipmentExt.UsrCustomerID);
if (row != null && sOShipmentExt != null)
{
sOShipmentExt.UsrShipAddressID = sOShipment.AddressID;
sOShipmentExt.UsrShipContactID = sOShipmentContact.ContactID;
}
SetShipAddressAndContact(row, sOShipmentExt.UsrShipAddressID, sOShipmentExt.UsrShipContactID);
#endregion
}
The issues I’m facing now:
Once the document has been saved and I switched to another document, I am unable to access back the previous document that has been saved and it returns below errors: ShipAddressID - Specified cast is not valid ShipContactID - Specified cast is not valid
If the selected Customer ID doesn’t have any data in SOShipmentAddress or SOShipmentContact, then it won’t allow me to select the customer. Ideally, I should be able to select the customer and the fields should be auto populated with address details from Address table.
I would appreciate any help since I’m getting really close to solving this.
Upvotes: 0
Views: 147
Reputation: 1066
SOAddress is a table of addresses that are linked to SOs & Shipments. Since this data generally doesn't change very much, the idea is that records will be linked to multiple SOs and shipments. When an address is changed for a Customer, a new record is created in SOAddress. That way, historical data is preserved intact. Thus, records in the table should not be edited. So, rather than thinking about overwriting the address information in SOAddress, you should think about overwriting the ShipAddressID linked to the Shipment to either a new address you create or more likely an already existing one.
Upvotes: 0