Skip to main content
Answer

Create a vendor via an action button

  • March 11, 2025
  • 5 replies
  • 77 views

Forum|alt.badge.img+2

I had implemented a custom DAC and screen called prospect vendor. then I need to create a default vendor in the system on a button click. following is the action code. 
My issue is the header section of vendor page contains data from vendorR dac, which is extended from Vendor Dac, I try to create a vendor instance and saved it, action runs without error, but data is not saved to Database. when I try to save vendorR instance it gives error. how can save a BAccount instance rated to my vendor via VendorMaint graph object.

 

public PXAction<ESProspectVendors> CreateVendor;
[PXButton(DisplayOnMainToolbar = true, CommitChanges = true)]
[PXUIField(DisplayName = "Create Vendor", Enabled = true)]
protected virtual IEnumerable createVendor(PXAdapter adapter)
{
ESProspectVendors vendor = ProspectVendorsView.Current;
PXLongOperation.StartOperation(this, delegate
{
SaveVendor(vendor);
});
return adapter.Get();
}

public static void SaveVendor(ESProspectVendors prospectVendor)
{
try
{
PXTrace.WriteInformation($" Action Started :");
VendorR vendorR = new VendorR();
Vendor vendor = new Vendor();
BAccount bAccount = new BAccount();
bAccount.AcctCD =
vendorR.AcctCD = prospectVendor.SupplierCode;
vendorR.VendorClassID = prospectVendor.VendorClassID;
vendor.AcctCD = prospectVendor.SupplierCode;
vendor.VendorClassID = prospectVendor.VendorClassID;
vendor.LegalName = prospectVendor.SupplierName;
vendorR.VStatus = "A";
vendor.AcctName = prospectVendor.BankName;

var vendorGraph = PXGraph.CreateInstance<VendorMaint>();
vendorGraph.CurrentVendor.Insert(vendor);

vendorGraph.Actions.PressSave();
//vendorGraph.BAccountAccessor.Insert(bAccount);
vendorGraph.Actions.PressSave();
var prospectVendorGraph = PXGraph.CreateInstance<ESProspectVendorMaint>();
prospectVendor.DefaultVendorCD = vendorR.AcctCD;
prospectVendorGraph.ProspectVendorsView.Update(prospectVendor);
prospectVendorGraph.Actions.PressSave();
PXTrace.WriteInformation($" Action Completed :");
}
catch (Exception ex)
{
PXTrace.WriteInformation($" Error : {ex.Message} - {ex.InnerException.Message}");
}
}

 

Best answer by DipakNilkanth

Hi ​@PDharmasena10,
Please try below code snippet.

 public static void SaveVendor(ESProspectVendors prospectVendor)
{
try
{


var vendorGraph = PXGraph.CreateInstance<VendorMaint>();

// Create and populate the BAccount
PX.Objects.CR.BAccount bAccount = new PX.Objects.CR.BAccount
{
AcctCD = prospectVendor.SupplierCode,
AcctName = prospectVendor.SupplierName,

}

bAccount = vendorGraph.BAccount.Insert((VendorR)bAccount);

// Create and populate the Vendor
Vendor vendor = new Vendor
{
AcctCD = prospectVendor.SupplierCode,
VendorClassID = prospectVendor.VendorClassID,
LegalName = prospectVendor.SupplierName
};

vendor = vendorGraph.CurrentVendor.Insert(vendor);

// Save the Vendor and BAccount
vendorGraph.Actions.PressSave();

// Update the Prospect Vendor with the new VendorCD
var prospectVendorGraph = PXGraph.CreateInstance<ESProspectVendorMaint>();
prospectVendor.DefaultVendorCD = vendor.AcctCD;
prospectVendorGraph.ProspectVendorsView.Update(prospectVendor);
prospectVendorGraph.Actions.PressSave();


}
catch (Exception ex)
{
PXTrace.WriteError($"Error: {ex.Message} - {ex.InnerException?.Message}");
}

Hope it will helps!

5 replies

Forum|alt.badge.img+1

What is the error you receive?


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • March 11, 2025

@stephenward03 ,
when try to insert VendorR instance it gives a run time error “object reference is null”. when tring to insert BAccount instance it did not accept. when try to save Vendor instance, it did not saves data to database 


Forum|alt.badge.img+8
  • Captain II
  • March 11, 2025

Hi ​@PDharmasena10 

 

You should try to create the BAccount record first, the VendorR table inherits from Vendor, which inherits from BAccount.

 

You should insert a BAccount record into the BusinessAccountMaint graph first too.

 

Another avenue you could look into is using the ExtendToVendorGraph graph extension which implements the logic of extending a business account to a vendor.

 

Aleks


DipakNilkanth
Pro III
Forum|alt.badge.img+13
  • Pro III
  • Answer
  • March 11, 2025

Hi ​@PDharmasena10,
Please try below code snippet.

 public static void SaveVendor(ESProspectVendors prospectVendor)
{
try
{


var vendorGraph = PXGraph.CreateInstance<VendorMaint>();

// Create and populate the BAccount
PX.Objects.CR.BAccount bAccount = new PX.Objects.CR.BAccount
{
AcctCD = prospectVendor.SupplierCode,
AcctName = prospectVendor.SupplierName,

}

bAccount = vendorGraph.BAccount.Insert((VendorR)bAccount);

// Create and populate the Vendor
Vendor vendor = new Vendor
{
AcctCD = prospectVendor.SupplierCode,
VendorClassID = prospectVendor.VendorClassID,
LegalName = prospectVendor.SupplierName
};

vendor = vendorGraph.CurrentVendor.Insert(vendor);

// Save the Vendor and BAccount
vendorGraph.Actions.PressSave();

// Update the Prospect Vendor with the new VendorCD
var prospectVendorGraph = PXGraph.CreateInstance<ESProspectVendorMaint>();
prospectVendor.DefaultVendorCD = vendor.AcctCD;
prospectVendorGraph.ProspectVendorsView.Update(prospectVendor);
prospectVendorGraph.Actions.PressSave();


}
catch (Exception ex)
{
PXTrace.WriteError($"Error: {ex.Message} - {ex.InnerException?.Message}");
}

Hope it will helps!


davidnavasardyan
Jr Varsity I
Forum|alt.badge.img+3

Hi ​@PDharmasena10 

here is a simplified version that often works better in a custom action. You can adapt it to your naming convention and your custom fields:

public static void SaveVendor(ESProspectVendors prospectVendor)
{
try
{
var vendorGraph = PXGraph.CreateInstance<VendorMaint>();

BAccount bAcct = new BAccount
{
AcctCD = prospectVendor.SupplierCode,
AcctName = prospectVendor.SupplierName,
Type = BAccountType.VendorType,
};
bAcct = vendorGraph.BAccount.Insert(bAcct);

Vendor vendor = new Vendor
{
AcctCD = bAcct.AcctCD,
AcctName = prospectVendor.BankName,
VendorClassID = prospectVendor.VendorClassID,
VStatus = "A"
};
// Setting Current to ensure it’s recognized as the primary
vendorGraph.Vendor.Current = vendorGraph.Vendor.Insert(vendor);

// (Optional) If you have extended DAC fields in VendorR, set them here
// Usually, you can just cast vendorGraph.Vendor.Current to your extension
// or use vendorGraph.Caches[typeof(Vendor)].Current as VendorR, then set fields.


vendorGraph.Actions.PressSave();

// Update your Prospect Vendor with the newly created VendorCD
var prospectVendorGraph = PXGraph.CreateInstance<ESProspectVendorMaint>();
prospectVendor.DefaultVendorCD = bAcct.AcctCD;
prospectVendorGraph.ProspectVendorsView.Update(prospectVendor);
prospectVendorGraph.Actions.PressSave();
}
catch (Exception ex)
{
PXTrace.WriteInformation($" Error : {ex.Message} - {ex.InnerException?.Message}");
}
}