Hey folks ,
This code will be helpful where the requirement is either to create or update customers using x++.
In my example I have used this code to pickup the values for customer creation from a staging table.
The code is shown below :-
#define.CountryRegionId("US")
CustomerDetails customerDetailsStaging, custMasterupdate;
LogisticsElectronicAddress logisticselectronicaddress;
LogisticsContactInfoView logisticsContactInfoView;
LogisticsPostalAddress logisticspostaladdress;
LogisticsLocation logisticslocation;
CustTable custTable, custTableUpdate, custTableFind;
DirParty dirParty;
DirPartyLocation dirPartyLocation;
DirPartyContactInfoView dirPartyContactInfo;
DirPartyTable dirPartyTable;
ContactPerson contactperson;
DirPartyPostalAddressView dirPartyPostalAddressView;
LanguageId languageId = CompanyInfo::find().LanguageId;
LogisticsPostalAddress address;
DirPartyPostalAddressView addressView;
LogisticsLocationRole roles;
TaxVATNumTable taxvatnumtableget , taxvatnumtableins;
DimensionAttributeValue dimAttrBINValue,dimAtrContractValue;
DimensionAttribute dimAttrBIN,dimAtrContract;
DimensionAttributeValueSetStorage davss;
RecId defaultDimension;
while select customerDetailsStaging where customerDetailsStaging.RecId!=0
{
custTable = CustTable::find(customerDetailsStaging.CustomerId);
try
{
if(!custTable)
{
davss = DimensionAttributeValueSetStorage::find(
CustTable::find(custTable.AccountNum).DefaultDimension);
ttsBegin;
custTable.clear();
custTable.initValue();
custTable.AccountNum = customerDetailsStaging.CustomerId;
custTable.CustGroup = customerDetailsStaging.CustGroup;
custTable.CustClassificationId = customerDetailsStaging.CustClassGroup;
custTable.VATNum = customerDetailsStaging.TRNNo;
custTable.INTC_Emirates = customerDetailsStaging.Emirates;
custTable.TaxGroup = customerDetailsStaging.VAT;
custTable.PaymTermId = customerDetailsStaging.PaymentTerms;
dimAttrBIN = DimensionAttribute::findByName("BIN");
dimAtrContract = DimensionAttribute::findByName("Contract");
dimAttrBINValue = DimensionAttributeValue::findByDimensionAttributeAndValue(
dimAttrBIN, customerDetailsStaging.BIN, false, true);
dimAtrContractValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAtrContract, customerDetailsStaging.Contract, false, true);
if(dimAttrBINValue || dimAtrContractValue)
{
davss.addItem(dimAttrBINValue);
davss.addItem(dimAtrContractValue);
custTable.DefaultDimension = davss.save();
}
custTable.insert(DirPartyType::Organization, customerDetailsStaging.CustomerName);
dirParty = DirParty::constructFromCommon(custTable);
dirPartyPostalAddressView.Street = customerDetailsStaging.Address;
dirPartyPostalAddressView.CountryRegionId = #CountryRegionId;
dirPartyPostalAddressView.IsPrimary = NoYes::Yes;
dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);
ttsCommit;
if(customerDetailsStaging.TRNNo!='0')
{
select taxvatnumtableget where taxvatnumtableget.CountryRegionId=='ARE'
&& taxvatnumtableget.VATNum==customerDetailsStaging.TRNNo &&
taxvatnumtableget.Name==custTable.name();
if(!taxvatnumtableget)
{
ttsbegin;
taxvatnumtableins.CountryRegionId = "ARE";
taxvatnumtableins.VATNum = customerDetailsStaging.TRNNo;
taxvatnumtableins.Name = custTable.name();
taxvatnumtableins.insert();
ttscommit;
}
}
}
else if(custTable)
{
dirParty = DirParty::constructFromCommon(custTable);
select taxVatNumTablefetch where taxVatNumTablefetch.Name==custTable.name() && taxVatNumTablefetch.VATNum!='0';
select forupdate custTableUpdate where custTableUpdate.AccountNum == custTable.AccountNum;
{
davss = DimensionAttributeValueSetStorage::find(CustTable::find(custTable.AccountNum).DefaultDimension);
ttsbegin;
custTableUpdate.CustGroup = customerDetailsStaging.CustGroup;
custTableUpdate.CustClassificationId = customerDetailsStaging.CustClassGroup;
custTableUpdate.VATNum = customerDetailsStaging.TRNNo;
custTableUpdate.INTC_Emirates = customerDetailsStaging.Emirates;
custTableUpdate.TaxGroup = customerDetailsStaging.VAT;
custTableUpdate.PaymTermId = customerDetailsStaging.PaymentTerms;
dimAttrBIN = DimensionAttribute::findByName("BIN");
dimAtrContract = DimensionAttribute::findByName("Contract");
dimAttrBINValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttrBIN, customerDetailsStaging.BIN, false, true);
dimAtrContractValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAtrContract, customerDetailsStaging.Contract, false, true);
if(dimAttrBINValue || dimAtrContractValue)
{
davss.addItem(dimAttrBINValue);
davss.addItem(dimAtrContractValue);
custTableUpdate.DefaultDimension = davss.save();
}
custTableUpdate.update();
ttscommit;
}
ttsbegin;
while select dirPartyLocation
where dirPartyLocation.Party == custTable.Party
join dirPartyPostalAddressView
where dirPartyPostalAddressView.Location == dirPartyLocation.Location
&& dirPartyPostalAddressView.Party == dirPartyLocation.Party
{
dirPartyPostalAddressView.Street = customerDetailsStaging.Address;
dirPartyPostalAddressView.CountryRegionId = #CountryRegionId;
dirPartyPostalAddressView.IsPrimary = NoYes::Yes;
dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);
}
if(!dirPartyLocation)
{
address.Street = customerDetailsStaging.Address;
address.CountryRegionId = #CountryRegionId;
addressView.Party = custtable.Party;
addressView.IsPrimary = NoYes::Yes;
addressview.initFromPostalAddress(address);
DirParty = DirParty::constructFromPartyRecId(CustTable.Party);
roles = LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Delivery);
DirParty.createOrUpdatePostalAddress(addressView);
}
ttscommit;
if(customerDetailsStaging.TRNNo!='0')
{
select taxvatnumtableget where taxvatnumtableget.CountryRegionId=='ARE'
&& taxvatnumtableget.VATNum==customerDetailsStaging.TRNNo &&
taxvatnumtableget.Name==custTable.name();
if(!taxvatnumtableget)
{
ttsbegin;
taxvatnumtableins.CountryRegionId = "ARE";
taxvatnumtableins.VATNum = customerDetailsStaging.TRNNo;
taxvatnumtableins.Name = custTable.name();
taxvatnumtableins.insert();
ttscommit;
}
}
}
}
catch(Exception::Error)
{
throw Exception::Error;
}
}
Happy Coding!!!
This code will be helpful where the requirement is either to create or update customers using x++.
In my example I have used this code to pickup the values for customer creation from a staging table.
The code is shown below :-
#define.CountryRegionId("US")
CustomerDetails customerDetailsStaging, custMasterupdate;
LogisticsElectronicAddress logisticselectronicaddress;
LogisticsContactInfoView logisticsContactInfoView;
LogisticsPostalAddress logisticspostaladdress;
LogisticsLocation logisticslocation;
CustTable custTable, custTableUpdate, custTableFind;
DirParty dirParty;
DirPartyLocation dirPartyLocation;
DirPartyContactInfoView dirPartyContactInfo;
DirPartyTable dirPartyTable;
ContactPerson contactperson;
DirPartyPostalAddressView dirPartyPostalAddressView;
LanguageId languageId = CompanyInfo::find().LanguageId;
LogisticsPostalAddress address;
DirPartyPostalAddressView addressView;
LogisticsLocationRole roles;
TaxVATNumTable taxvatnumtableget , taxvatnumtableins;
DimensionAttributeValue dimAttrBINValue,dimAtrContractValue;
DimensionAttribute dimAttrBIN,dimAtrContract;
DimensionAttributeValueSetStorage davss;
RecId defaultDimension;
while select customerDetailsStaging where customerDetailsStaging.RecId!=0
{
custTable = CustTable::find(customerDetailsStaging.CustomerId);
try
{
if(!custTable)
{
davss = DimensionAttributeValueSetStorage::find(
CustTable::find(custTable.AccountNum).DefaultDimension);
ttsBegin;
custTable.clear();
custTable.initValue();
custTable.AccountNum = customerDetailsStaging.CustomerId;
custTable.CustGroup = customerDetailsStaging.CustGroup;
custTable.CustClassificationId = customerDetailsStaging.CustClassGroup;
custTable.VATNum = customerDetailsStaging.TRNNo;
custTable.INTC_Emirates = customerDetailsStaging.Emirates;
custTable.TaxGroup = customerDetailsStaging.VAT;
custTable.PaymTermId = customerDetailsStaging.PaymentTerms;
dimAttrBIN = DimensionAttribute::findByName("BIN");
dimAtrContract = DimensionAttribute::findByName("Contract");
dimAttrBINValue = DimensionAttributeValue::findByDimensionAttributeAndValue(
dimAttrBIN, customerDetailsStaging.BIN, false, true);
dimAtrContractValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAtrContract, customerDetailsStaging.Contract, false, true);
if(dimAttrBINValue || dimAtrContractValue)
{
davss.addItem(dimAttrBINValue);
davss.addItem(dimAtrContractValue);
custTable.DefaultDimension = davss.save();
}
custTable.insert(DirPartyType::Organization, customerDetailsStaging.CustomerName);
dirParty = DirParty::constructFromCommon(custTable);
dirPartyPostalAddressView.Street = customerDetailsStaging.Address;
dirPartyPostalAddressView.CountryRegionId = #CountryRegionId;
dirPartyPostalAddressView.IsPrimary = NoYes::Yes;
dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);
ttsCommit;
if(customerDetailsStaging.TRNNo!='0')
{
select taxvatnumtableget where taxvatnumtableget.CountryRegionId=='ARE'
&& taxvatnumtableget.VATNum==customerDetailsStaging.TRNNo &&
taxvatnumtableget.Name==custTable.name();
if(!taxvatnumtableget)
{
ttsbegin;
taxvatnumtableins.CountryRegionId = "ARE";
taxvatnumtableins.VATNum = customerDetailsStaging.TRNNo;
taxvatnumtableins.Name = custTable.name();
taxvatnumtableins.insert();
ttscommit;
}
}
}
else if(custTable)
{
dirParty = DirParty::constructFromCommon(custTable);
select taxVatNumTablefetch where taxVatNumTablefetch.Name==custTable.name() && taxVatNumTablefetch.VATNum!='0';
select forupdate custTableUpdate where custTableUpdate.AccountNum == custTable.AccountNum;
{
davss = DimensionAttributeValueSetStorage::find(CustTable::find(custTable.AccountNum).DefaultDimension);
ttsbegin;
custTableUpdate.CustGroup = customerDetailsStaging.CustGroup;
custTableUpdate.CustClassificationId = customerDetailsStaging.CustClassGroup;
custTableUpdate.VATNum = customerDetailsStaging.TRNNo;
custTableUpdate.INTC_Emirates = customerDetailsStaging.Emirates;
custTableUpdate.TaxGroup = customerDetailsStaging.VAT;
custTableUpdate.PaymTermId = customerDetailsStaging.PaymentTerms;
dimAttrBIN = DimensionAttribute::findByName("BIN");
dimAtrContract = DimensionAttribute::findByName("Contract");
dimAttrBINValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttrBIN, customerDetailsStaging.BIN, false, true);
dimAtrContractValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAtrContract, customerDetailsStaging.Contract, false, true);
if(dimAttrBINValue || dimAtrContractValue)
{
davss.addItem(dimAttrBINValue);
davss.addItem(dimAtrContractValue);
custTableUpdate.DefaultDimension = davss.save();
}
custTableUpdate.update();
ttscommit;
}
ttsbegin;
while select dirPartyLocation
where dirPartyLocation.Party == custTable.Party
join dirPartyPostalAddressView
where dirPartyPostalAddressView.Location == dirPartyLocation.Location
&& dirPartyPostalAddressView.Party == dirPartyLocation.Party
{
dirPartyPostalAddressView.Street = customerDetailsStaging.Address;
dirPartyPostalAddressView.CountryRegionId = #CountryRegionId;
dirPartyPostalAddressView.IsPrimary = NoYes::Yes;
dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);
}
if(!dirPartyLocation)
{
address.Street = customerDetailsStaging.Address;
address.CountryRegionId = #CountryRegionId;
addressView.Party = custtable.Party;
addressView.IsPrimary = NoYes::Yes;
addressview.initFromPostalAddress(address);
DirParty = DirParty::constructFromPartyRecId(CustTable.Party);
roles = LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Delivery);
DirParty.createOrUpdatePostalAddress(addressView);
}
ttscommit;
if(customerDetailsStaging.TRNNo!='0')
{
select taxvatnumtableget where taxvatnumtableget.CountryRegionId=='ARE'
&& taxvatnumtableget.VATNum==customerDetailsStaging.TRNNo &&
taxvatnumtableget.Name==custTable.name();
if(!taxvatnumtableget)
{
ttsbegin;
taxvatnumtableins.CountryRegionId = "ARE";
taxvatnumtableins.VATNum = customerDetailsStaging.TRNNo;
taxvatnumtableins.Name = custTable.name();
taxvatnumtableins.insert();
ttscommit;
}
}
}
}
catch(Exception::Error)
{
throw Exception::Error;
}
}
Happy Coding!!!
No comments:
Post a Comment