Hey folks ,
Using the below code we can easily create or update vendors in D365 FO.
The example I am using is picking the values for vendor creation from a staging table :-
VendTable vendTable, vendTableFind, vendTableUpdate;
DirParty dirParty;
DirPartyPostalAddressView dirPartyPostalAddressView;
DirPartyContactInfoView dirPartyContactInfo, dirPartyContactEmail, dirPartyContactFax;
DirPartyTable dirPartyTable;
PurchData vendorStaging , vendorStagingUpdate;
DirPartyLocation dirPartyLocation;
ContactPerson contactperson;
LogisticsPostalAddress address;
DirPartyPostalAddressView addressView;
LogisticsLocationRole roles;
while select vendorStaging
{
vendTableFind = VendTable::find(vendorStaging.SupplierCode);
try
{
if(!vendTableFind)
{
ttsBegin;
vendTable.initValue();
vendTable.AccountNum = vendorStaging.SupplierCode;
vendTable.VendGroup = vendorStaging.VendGroup;
vendTable.Currency = vendorStaging.Currency;
vendTable.PaymTermId = vendorStaging.PaymentTerms;
vendTable.TaxGroup = vendorStaging.TaxCode;
vendTable.INTC_Emirates = vendorStaging.Emirates;
vendTable.VATNum = vendorStaging.TRNNo;
vendTable.insert();
dirPartyTable = DirPartyTable::findRec(vendTable.Party, true);
dirPartyTable.Name = vendorStaging.SupplierName;
dirPartyTable.update();
dirParty = DirParty::constructFromCommon(vendTable);
dirPartyPostalAddressView.Street = vendorStaging.Address;
dirPartyPostalAddressView.CountryRegionId = 'ARE';
dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);
//phone
dirPartyContactInfo.LocationName = 'Phone';
dirPartyContactInfo.Locator = vendorStaging.ContactInfoPhoneNo;
dirPartyContactInfo.Type = LogisticsElectronicAddressMethodType::Phone;
dirPartyContactInfo.IsPrimary = NoYes::Yes;
dirParty.createOrUpdateContactInfo(dirPartyContactInfo);
dirPartyContactEmail.LocationName = 'Email';
dirPartyContactEmail.Locator = vendorStaging.ContactInfoEmailAddress;
dirPartyContactEmail.Type = LogisticsElectronicAddressMethodType::Email;
dirPartyContactEmail.IsPrimary = NoYes::Yes;
dirParty.createOrUpdateContactInfo(dirPartyContactEmail);
//Fax
dirPartyContactFax.LocationName = 'Fax';
dirPartyContactFax.Locator = vendorStaging.ContactInfoFax;
dirPartyContactFax.Type = LogisticsElectronicAddressMethodType::Fax;
dirPartyContactFax.IsPrimary = NoYes::Yes;
dirParty.createOrUpdateContactInfo(dirPartyContactFax);
ttsCommit;
select forupdate vendorStagingUpdate
where vendorStagingUpdate.RecId== vendorStaging.RecId;
ttsbegin;
vendorStagingUpdate.UpdateFlag = 1;
vendorStagingUpdate.update();
ttscommit;
}
else if(vendTableFind)
{
dirParty = DirParty::constructFromCommon(vendTableFind);
select forupdate vendTableUpdate
where vendTableUpdate.AccountNum == vendTableFind.AccountNum;
{
ttsbegin;
vendTableUpdate.VendGroup = vendorStaging.VendGroup;
vendTableUpdate.Currency = vendorStaging.Currency;
vendTableUpdate.PaymTermId = vendorStaging.PaymentTerms;
vendTableUpdate.TaxGroup = vendorStaging.TaxCode;
vendTableUpdate.INTC_Emirates = vendorStaging.Emirates;
vendTableUpdate.VATNum = vendorStaging.TRNNo;
vendTableUpdate.INTC_BankAccountId = vendorStaging.BankCode;
vendTableUpdate.INTC_BankAccountName = vendorStaging.BankName;
vendTableUpdate.update();
ttscommit;
}
ttsbegin;
dirPartyPostalAddressView.Street = vendorStaging.Address;
dirPartyPostalAddressView.CountryRegionId = "ARE";
dirPartyPostalAddressView.IsPrimary = NoYes::Yes;
dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);
if(!(dirParty.getContactInfo(vendTableFind.RecId)))
{
//phone
dirPartyContactInfo.LocationName = 'Phone';
dirPartyContactInfo.Locator = vendorStaging.ContactInfoPhoneNo;
dirPartyContactInfo.Type = LogisticsElectronicAddressMethodType::Phone;
dirPartyContactInfo.IsPrimary = NoYes::Yes;
dirParty.createOrUpdateContactInfo(dirPartyContactInfo);
dirPartyContactEmail.LocationName = 'Email';
dirPartyContactEmail.Locator = vendorStaging.ContactInfoEmailAddress;
dirPartyContactEmail.Type = LogisticsElectronicAddressMethodType::Email;
dirPartyContactEmail.IsPrimary = NoYes::Yes;
dirParty.createOrUpdateContactInfo(dirPartyContactEmail);
//Fax
dirPartyContactFax.LocationName = 'Fax';
dirPartyContactFax.Locator = vendorStaging.ContactInfoFax;
dirPartyContactFax.Type = LogisticsElectronicAddressMethodType::Fax;
dirPartyContactFax.IsPrimary = NoYes::Yes;
dirParty.createOrUpdateContactInfo(dirPartyContactFax);
}
ttscommit;
}
}
catch(Exception::Error)
{
throw Exception::Error;
}
}
Please let me know in comments in case of any further queries.
Happy Coding!!!
No comments:
Post a Comment