I created a custom table with additional settings for customers. Next I added a field to the customer core table in which I'd like to store the id choice per customer. I extended with EntityExtensionInterface the customerDefinition :
public function extendFields(FieldCollection $collection): void
{
$collection->add(
(new OneToOneAssociationField(
'customerSetting',
'customer_setting',
'id',
WdtCustomerSettingsDefinition::class,
true
))->addFlags(new Inherited())
);
}
public function getDefinitionClass(): string
{
return CustomerDefinition::class;
}
When I manually manipulate the customer table, with an id from my custom table in the added field, I can retrieve and use the settings from my custom table in the storefront.
For the backend I created a single select to the entity custom_table,
<sw-entity-single-select entity="wdt_customer_settings" v-model="customer.extensions.customerSetting.id" >
</sw-entity-single-select>
and with the manually 'injected' id from the custom table, this choice indicates indeed 'selected' However, after changing to another choice and saving results in an error: Customer could not be saved.
What am I missing?
You should look always to the ajax responses. There is the explict error which is occured. Do you added some boilerplate code to check that your extensions is always available? Otherwise it would cause issues on new entities
Related
I'm creating a new screen with one new table (AMClockItem) where the key field is the employee ID. I want it to default to the logged in user employee ID. If there isn't an AMClockItem record for the employee it will treat it as a new record, but if there is an existing record for the employee I want it to retrieve the existing record. This all works perfectly EXCEPT the very first time the screen is loaded. The logged in user defaults properly but it doesn't retrieve the existing record. If I click Cancel or change another field it will retrieve the record properly.
My Graph
public PXSelect<AMClockItem, Where<AMClockItem.employeeID, Equal<Optional<AMClockItem.employeeID>>>> header;
DAC:
public abstract class employeeID : PX.Data.BQL.BqlInt.Field<employeeID> { }
protected Int32? _EmployeeID;
[PXDBInt(IsKey = true)]
[ProductionEmployeeSelector]
[PXDefault(typeof(Search<EPEmployee.bAccountID,
Where<EPEmployee.userID, Equal<Current<AccessInfo.userID>>>>), PersistingCheck = PXPersistingCheck.Null)]
[PXUIField(DisplayName = "Employee ID")]
public virtual Int32? EmployeeID
{
get
{
return this._EmployeeID;
}
set
{
this._EmployeeID = value;
}
}
#endregion
On first load, it defaults in the correct logged in user, but the rest of the fields are blank, treating it as a new record.
I hit the cancel button and the existing record loads correctly.
I tried looking for a similar Acumatica screen but can't find an example where a key value is defaulted in on load. Is there a way to force the existing record to load the first time you visit the screen?
I'm not sure this is allowed, there's no mechanism that selects record like that to my knowledge as well.
I suspect the formulas do not execute in order too.
Make sure all key fields have proper value. Record can't be selected if one of the key fields resolve to null on load.
If some key fields initialization are dependent on other key fields initialization decorate them with the PXDependsOnFields attribute:
[PXDependsOnFields(typeof(employeeID))]
You can use the PageLoadBehavior property of "DataSource". Select "SearchSavedKeys". This will help.
I need to create new Customers by mostly copying a select customer and modifying a few fields relevant to a custom Process.
Outside of the custom Process as an initial attempt to see if this is even possible to copy a Customer I have the following:
public class CustomerMaint_Extension : PXGraphExtension<CustomerMaint>
{
public PXAction<Customer> copyTest;
[PXProcessButton]
[PXUIField(DisplayName = "Copy Test")]
protected virtual void CopyTest()
{
var customer = Base.BAccount.Current;
var graph = PXGraph.CreateInstance<CustomerMaint>();
var cache = graph.BAccount.Cache;
// Set field Defaults using CustomerMaint.CopyAccounts method
graph.CopyAccounts(cache, customer);
// Create new copy of current Customer
var copyCustomer = (Customer)cache.CreateCopy(customer);
// Modify key values
copyCustomer.AcctCD = "COPY " + customer.AcctCD;
copyCustomer.BAccountID = null;
// Prevent "Customer Class Changed -- update Defaults?" dialog
cache.SetStatus(copyCustomer, PXEntryStatus.Inserted);
// Insert into cache
// *** Exception occurs here ***
copyCustomer = (Customer)cache.Insert(copyCustomer);
// Modify additional fields as necessary by custom process
// ...
// Persist to database
graph.Save.Press();
}
}
The issue I'm currently encountering with this code as it currently is, is in the cache.Insert(copyCustomer) throws an exception:
Error: An error occurred during processing of the field CustomerClassID: Value cannot be null.
Parameter name: key
I've tracked this down to be coming from the CustomerClassDefaultInserting function of the CustomerMaint graph at the point of SalesPerson.Insert(sperson). It appears this function is attempting to create the CustSalesPeople record for the Default Salesperson of the assigned Customer Class.
Is this even on the right path to copy a Customer or is there a better way? Or how to address the exception when Inserting the new customer?
The issue with copying the customer is more than just copying one object. There are many objects that need to be copied for the customer.
I copied your code and executed it. I commented out CopyAccounts as I would think it would be called on the cloned object, not on the existing customer.
The next error I received was Error: An error occurred during processing of the field Default Location value 8068 Error: Default Location '8068' cannot be found in the system.
This was due to cloning the other foreign key fields from the Customer record. DefLocationID is the default location for the customer. It is trying to set those keys as well, and cannot due to the restriction put on the Selector, requiring the location to be the same BAccountID as the record. DefAddressID, DefContactID, and other key fields will act the same way.
So to complete this, you would need to review all foreign keys on the Customer/BAccount that may be set, and then copy those objects and set them to the proper DAC's as well. Some information may not need to be copied, so I would just null those key fields and copy what is required.
Hey Nickolas Hook Like KRichardson pointed out because of all the PK/FK constraints you can't just copy the data you will have to do everything individually.
the other approach is to pull the data for the parent.
I have a modification that facilitates an internal business process utilizing PO and SO. The screen provides the ability to purchase an MRO spare part outside of the normal replenishment process. The item may or may not be maintained in inventory, so it may be ordered for replenishment or as an SO to be processed as Mark for PO.
In creating the SO, I am able to store the SO reference information to the DAC for my customization. When creating the PO directly, I also am able to capture the PO reference information. However, when creating the PO from the SO using the standard Acumatica menu action for Create Purchase Order, I have been unable to capture the right event to enable storing the PO reference being assigned in SOLineSplit3 to my custom DAC. (Worth noting that I also need to be able to override the default curyunitcost value on the PO line using the value stored on my custom DAC as these purchases do not carry a fixed price per buy. This is done by tracing the SOLineSplit back to my custom DAC and overriding POLine_CuryUnitCost_FieldDefaulting.)
The action invoked on the Sales Order Entry screen (Action - Create Purchase Order) calls the method CreatePOOrders in POCreate.cs which in turn creates an instance of the POOrderEntry graph to create the actual purchase order.
Eventually, the following code is reached, which appears to attach the PO reference information to SOLineSplit3 as soline:
soline.POType = line.OrderType;
soline.PONbr = line.OrderNbr;
soline.POLineNbr = line.LineNbr;
soline.RefNoteID = docgraph.Document.Current.NoteID;
docgraph.UpdateSOLine(soline, docgraph.Document.Current.VendorID, true);
docgraph.FixedDemand.Cache.SetStatus(soline, PXEntryStatus.Updated);
I am not yet familiar with Cache.SetStatus, but the pop-up description seems to indicate that this is using the FixedDemand select in POOrderEntry to find and set (or insert) the SOLineSplit3 record. The call to UpdateSOLine above it is a new internal method that was not in my previous version of POCrete.cs, as this entire method seems to have had some significant rework recently. In trying to capture events on SOLineSplit3 in both POCreate and POOrderEntry, it appears that Cache.SetStatus does not raise any events that I can capture... or I am just completely lost on what event to capture/override.
Immediately following this section, the following appears to update a Replenishment record and save the entire POOrderEntry graph.
if (docgraph.Transactions.Cache.IsInsertedUpdatedDeleted)
{
using (PXTransactionScope scope = new PXTransactionScope())
{
docgraph.Save.Press();
if (demand.PlanType == INPlanConstants.Plan90)
{
docgraph.Replenihment.Current = docgraph.Replenihment.Search<INReplenishmentOrder.noteID>(demand.RefNoteID);
if (docgraph.Replenihment.Current != null)
{
INReplenishmentLine rLine =
PXCache<INReplenishmentLine>.CreateCopy(docgraph.ReplenishmentLines.Insert(new INReplenishmentLine()));
rLine.InventoryID = line.InventoryID;
...
rLine.PlanID = demand.PlanID;
docgraph.ReplenishmentLines.Update(rLine);
docgraph.Caches[typeof(INItemPlan)].Delete(demand);
docgraph.Save.Press();
}
}
scope.Complete();
}
...
}
Basically, I need to insert my code right between the assignment of the PO information to "soline" and the docgraph.Save.Press(); without copying dozens of lines of code to modify this method. I have managed cloning the base method and inserting my code successfully, but I'd prefer to use an event handler and eliminate modifying the standard code. But the question... What event in which graph will let me grab the PO information and follow the breadcumbs back through SOLineSplit to my custom DAC?
Acumatica Build 18.212.0033
Extend POCreate graph because it is the one instanciating the POOrderEntry you are interested in.
Setup a hook on any POOrderEntry graph created by POCreate and subscribe your events on the intercepted graph. I tested this solution, with a Sales Order that has allocations lines in allocation window it will catch the SOLineSplit3 events:
public class POCreate_Extension : PXGraphExtension<POCreate>
{
public override void Initialize()
{
PXGraph.InstanceCreated.AddHandler<POOrderEntry>((graph) =>
{
graph.RowInserting.AddHandler<POOrder>((sender, e) =>
{
PXTrace.WriteInformation("POOrderEntry_POOrder_RowInserting");
});
graph.RowInserting.AddHandler<POOrderEntry.SOLineSplit3>((sender, e) =>
{
PXTrace.WriteInformation("POOrderEntry_SOLineSplit3_RowInserting");
});
graph.RowUpdating.AddHandler<POOrderEntry.SOLineSplit3>((sender, e) =>
{
PXTrace.WriteInformation("POOrderEntry_SOLineSplit3_RowUpdating");
});
graph.RowPersisting.AddHandler<POOrderEntry.SOLineSplit3>((sender, e) =>
{
PXTrace.WriteInformation("POOrderEntry_SOLineSplit3_RowPersisting");
});
});
}
}
I'm using EF5 code first to generate my database schema, but my new navigation property is being named in an undesirable way in the table. here is the model I'm working with.
public class User
{
[Key]
public long UserId { get; set; }
...
**public virtual ICollection<ImagePermission> KeepThisNavigationName { get; set; }**
}
However, After I've updated my database and examine the table columns, the column is named:
dbo.ImagePermission.User_UserId
And I would like it to be named
dbo.ImagePermission.KeepThisNavigationName_UserId
I believe there is a way to do this using the Fluent API, but after many failed attempts, I can't get the desired outcome.
P.s. The 'ImagePermission' Entity is currently still in development, so I would prefer to drop the migration which creates this table so I can create this column name correctly during the table create, rather than having additional code to update the column name.
Many thanks, Oliver
The correct mapping with Fluent API would be:
modelBuilder.Entity<User>()
.HasMany(u => u.KeepThisNavigationName)
.WithOptional() // or WithRequired()
.Map(m => m.MapKey("KeepThisNavigationName_UserId"));
If you have a navigation property in ImagePermission refering to User you need to use WithOptional(i => i.User) (or WithRequired(i => i.User)) instead of the parameterless version.
I am trying to add fields to com.liferay.portal.model.User, an extra attribute using Expando. Can someone explain to me how this method is adding a field because docs don't have much description.
private void addUserCustomAttribute(long companyId, ExpandoTable userExpandoTable, String attributeName, int type) throws PortalException, SystemException {
ExpandoColumnLocalServiceUtil.getColumn(userExpandoTable.getTableId(), attributeName); //should be addColumn(long tableId, String name, int type) ???
} //and where can find type description couse i have very specific type, Map(String,Object) couse in ExpandoColumnConstants didn't see it
I have taken this from Liferay Expando Wiki's Adding User Custom Attributes.
When should I call this all? Where to put this in my project? What change is required or everything needs to be changed to call it.
Some good tutorial will be nice because it's hard to find something from 0 to end, always found only some part with no explanation.
The question is not very clear. But if you simply want to add a custom attribute for your User then you can refer to my answer here and reproduced for your reference:
Custom field for the user-entity can be created through:
Control Panel -> Portal -> Custom Fields -> User.
And programmatically can be created as follows:
user.getExpandoBridge().addAttribute("yourCustomFieldKey");
Then set the value as:
user.getExpandoBridge().setAttribute("yourCustomFieldKey", "valueForCustomField");
If your custom field is already present you can check like this:
if (user.getExpandoBridge().hasAttribute("yourCustomFieldKey")) { ... };
The data is stored in tables prefixed with "EXPANDO":
EXPANDOCOLUMN: stores the custom field key and other settings
(contains the tableId refrences)
EXPANDODATA: stores the custom field value for the key (contains the
columnId and tableId refrences)
EXPANDOTABLE: stores for which liferay entity (user) are you adding
the custom field
EXPANDOROW: stores linking information between a user and its values
(contains tableId and userId refrences)
Hope this helps.
If your custom field is multivalue, you can use this:
String customVal = "yourCustomFieldValue";
user.getExpandoBridge().setAttribute("yourCustomFieldKey", new String[] {customVal }, false);
The last parameter set to "false" avoids permission check.