Good day,
We've got a custom module that allows us to import trade agreements from an CSV file type.
In AX4 this worked fine but in AX 2009 the trade agreement would not work when an existing trade agreement for an Item does not have a DateTo value.
The custom module used definition groups.
I am not sure where to do the code
And I am also unsure of how to get the itemId that a trade agreement is imported for.
Please see the project screenshot
0
Comments
Ok so what I need to do is when importing the new trade agreement from file:
Check for any existing trade agreements for the specified item and if the DateTo value is empty then fill it in using the formula:
(New Trade Agreement Date From) - 1 Day
In other words. The day before the new trade agreement's start date.
Thanks.
If you now the itemId the relation to the PriceDiscTable is PriceDistTable.ItemCode == 0 && PriceDiscTable.ItemRelation == _ItemId
Hi,
Thanks for the reply.
Mmh, the problem is I do not know how to get the ItemId from Run or Main method.
Perhaps I am a little to inexperienced with AX.
Any suggestions.
Thanks.
You can get the itemId from the salesLine or purchLine tables which are related to the SalesTable and PurchTable respectively.
The screenshot is the code in main().
I think it trade agreements is stored in PriceDiscTable.
Thanks a million.
Can you show me the code for the arg and the run method?
Thanks for the reply.
We are importing trade agreements for Customer and ItemId.
Code:
void run()
{
;
if (relationType == PriceType::PriceSales)
{
try
{
ttsbegin;
select firstonly forupdate sysExpImpTable
where sysExpImpTable.GroupId == CustParameters::find().ImportDefGroup;
sysExpImpTable.FileName = dlgFileName.value();
sysExpImpTable.Status = SysDataIntegrationStatus::Import;
sysExpImpTable.update();
ttscommit;
}
catch
{
ttsabort;
throw error('Error updating import group, update cancelled');
}
args.record(SysExpImpGroup::find(CustParameters::find().ImportDefGroup));
menuFunction = new MenuFunction(menuitemactionstr(SysDataImport), MenuItemType::Action);
menuFunction.run(args);
}
else
{
if (relationType == PriceType::PricePurch)
{
try
{
ttsbegin;
select firstonly forupdate sysExpImpTable
where sysExpImpTable.GroupId == VendParameters::find().ImportDefGroup;
sysExpImpTable.FileName = dlgFileName.value();
sysExpImpTable.Status = SysDataIntegrationStatus::Import;
sysExpImpTable.update();
ttscommit;
}
catch
{
ttsabort;
throw error('Error updating import group, update cancelled');
}
args.record(SysExpImpGroup::find(VendParameters::find().ImportDefGroup));
menuFunction = new MenuFunction(menuitemactionstr(SysDataImport), MenuItemType::Action);
menuFunction.run(args);
}
}
}
args arg(Args _args = new Args())
{
;
args = _args;
relationType = args.caller().RelationType();
return args;
}
I'll get back to you later tomorrow.
Just reread the entire thread and now I think I understand what you problem was (at last).
I am sure you already thought of this,
but have you considered adding the new date field to the csv file?
This would solve your problem for importing new records.
Please note that dependint on your group setup you may have to add the new date field
to the import/export groups in Administration -> Periodic -> Data export/import -> Definition groups.
And for the old records, I think you may find the standard import/export functionality in AX
to be of some use to you. This is just one of the options you have:
1) export the existing records in csv (or excel) file
2) modify the dates in excel (for example)
3) and than import the records back in AX with the 'updated existing records' option
One of the other options would be to write a simple job to update all the dates (where necessary).
Regards
Thanks for the assistance.
Mmmh, what I was ultimately looking for was to automate the DateTo input.
Let my explain like this.
A trade agreement is bound to a customer and item.
Should I upload a new trade agreement and the DateFrom (effective date) of the new trade agreement is:
2008/12/02, then the AX code should automatically do the following:
1. Check for an existing trade agreement for this customer and this item no.
2. If an existing one is found then update it's DateTo: 2008/12/01. End the existing trade agreement setting the DateTo = (NewTradeAgreement.DateFrom - 1 Day).
BTW: Look at the attached screenshot.
Can't implement the code in the Definition Group table setup?
The is some code and maybe if you could do an query to update anything where customer and ItemNo is equals to current selected. Doing this way you won't need to check if it exists if the query where clause will automatically filter out any mismatching records. But I am not sure when this code gets executed and if it is good and safe practise. Please advise...
Hope this explains a little better.
I, personally don't like putting too much logic on the definition groups.
That said, what you are trying to do is certainly possible.
The convert() and import() methods are executed for each record.
The convert() method is executed before the import() method.
Basically the idea is to apply your logic in the convert() method and to use the
import() method for validation, if you don't want to import the current record you should 'return false'
from the import() method.
When it comes to good practices - there is no need to hard-code your field mappings in the convert() function.
It would be much better to setup those from the 'Field Setup' button.
There in the Overview tab you can map each field from AX to the corresponding position in the CSV file.
In the Options tab you can specify "if-empty" initializing value for each field.
And in the 'X++ source code' tab you can apply ad-hoc logic for each field.
In your case converting from string to date and backwards could be a little bit awkward...
The order of execution is as follows
1)convert() function for each field
2)convert() function for each record
3)import() function for each record
Anyways, I hope this helps.
Thanks for the reply.
I did not do this project although I am asked to work on it know.
So the hard coding of the field maps was done by other people and seeing it is working I would not want to change it.
But I agree with you that it is not the best of ways to do this.
Can you perhaps give an example of how to get the date correct.
Remember, I would like to automatically fill in a ToDate for any previous agreements for this customer on this item.
The ToDate = NewFromDate - 1 day.
Maybe some example code will assist me well.
I am not that experienced with AX.
Kind regards.