Determine if a field is a system field - axapta

I would like to know if there is a clever/short way to determine if a field in a table is generated from the system. I only have the TableNum and the FieldNum as variables (nothing hard coded, only dynamic values) and I'd like to be able to write something like this (pseudo-code):
if( Sys::isSystemField(tableId, fieldId) )
{
//...
}
Instead of:
//...
str fieldName;
//...
;
//...
fieldName = dictTable.fieldName(fieldId);
if(fieldName == "modifiedDateTime"
|| fieldName == "DEL_ModifiedTime"
|| fieldName == "modifiedBy"
|| //etc...)
{
//...
Which is what I'll be writing if there is no way to do what I'm looking for. Hopefully someone can help, I haven't find anything about that in the documentation unfortunately.
Cheers

Use isSysId a global method.
It is for example used in Global::buf2buf:
static void buf2Buf(Common _from, Common _to)
{
DictTable dictTable = new DictTable(_from.TableId);
fieldId fieldId = dictTable.fieldNext(0);
while (fieldId && ! isSysId(fieldId))
{
_to.(fieldId) = _from.(fieldId);
fieldId = dictTable.fieldNext(fieldId);
}
}

Related

AX2009 update form field value

I want to auto-update the Format field of the NumberSequenceTable form. The purpose is to help the user while manually entering the data.
For example this field should preferably be updated with concatenated values of the fields Number sequence code and Largest.
After thinking for a while I decided to change the underlying table NumberSequenceTable. I changed its "modifiedField" method by adding another case to the already existing switch statement. I think this is the accepted best practice in such cases:
public void modifiedField(fieldId _fieldId)
{
#Define.DefaultCleanInterval(24)
#Define.DefaultCacheSize(10)
str sequenceFormat;
super(_fieldId);
switch (_fieldId)
{
....
//BEGINING
case fieldnum(NumberSequenceTable, NumberSequence):
if (this.NumberSequence && this.Highest && !this.Format)
{
sequenceFormat = System.Text.RegularExpressions.Regex::Replace(int2str(this.Highest), "[0-9]", "#");
this.Format = strFmt("%1_%2",this.NumberSequence, sequenceFormat);
}
break;
//END
default :
break;
}
}
My previous solution involved overriding the "enter" method on the form field. However this would not be in line with best practices. For information purposes I am pasting here the initial solution.
public void enter()
{
str sequenceNode;
str sequenceValue;
str maxValue;
str formatValue;
;
super();
//Get required fields value
sequenceNode = numberSequenceTable_ds.object(fieldnum(NumberSequenceTable,NumberSequence)).getValue();
maxValue = numberSequenceTable_ds.object(fieldnum(NumberSequenceTable,Highest)).getValue();
formatValue = numberSequenceTable_ds.object(fieldnum(NumberSequenceTable,Format)).getValue();
if(sequenceNode && maxValue && !formatValue) {
//Match regex pattern
maxValue = System.Text.RegularExpressions.Regex::Replace(maxValue, "[0-9]", "#");
sequenceValue = strFmt("%1_%2",sequenceNode,maxValue);
//Change field value
numberSequenceTable_ds.object(fieldnum(NumberSequenceTable,Format)).setValue(sequenceValue);
this.update();
}
}

replace DescriptionAttr with Dictionary or smth

I have a enum that's contain Description Attribute(for audit):
public enum ActivityType
{
[NotExist("Not Assign")]
[Description("Change Level")]
LevelChanged,
[NotExist("Not Assign")]
[Description("Change Skill Level")]
SkillLevelChanged
}
And all had been great until i needed to put Desription in Resource files(attribute didn't support them), so i need Dictionary or something like this.The question is: How implement this feature without big changing in all another logic?Something like this:
private static readonly Dictionary<ActivityType, String> ActivityDescription = new Dictionary<ActivityType, String>()
{
{ActivityType.LevelChanged, "Change"},
{ActivityType.SkillLevelChanged, "SkillChange"}
}
The solution that requires the minimal amount of code change for you would be to alter your descriptions to be the resource keys in your resource file. Then you could read these dynamically by doing something like:
[Description("Change_Level")]
Then your resource key/value would be:
Change_Level Change Level
And to read it you can do:
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute = value.GetType()
.GetField(value.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.SingleOrDefault() as DescriptionAttribute;
if (attribute != null)
{
var resManager = new ResourceManager(typeof(MyResources));
return resManager.GetString(attribute.Description);
}
else
{
return value.ToString();
}
If however you're wanting a nicer solution with the option to pass in the resource file, you can hijack the Display attribute:
[Display(ResourceType = typeof(MyResources), Name = "Change_Level")]
Then you can do:
FieldInfo fi = value.GetType().GetField(value.ToString());
DisplayAttribute attribute = value.GetType()
.GetField(value.ToString())
.GetCustomAttributes(typeof(DisplayAttribute), false)
.SingleOrDefault() as DisplayAttribute;
if (attribute != null)
{
var resManager = new ResourceManager(attribute.ResourceType);
return resManager.GetString(attribute.Name);
}
else
{
return value.ToString();
}

X++ passing current selected records in a form for your report

I am trying to make this question sound as clear as possible.
Basically, I have created a report, and it now exists as a menuitem button so that the report can run off the form.
What I would like to do, is be able to multi-select records, then when I click on my button to run my report, the current selected records are passed into the dialog form (filter screen) that appears.
I have tried to do this using the same methods as with the SaleLinesEdit form, but had no success.
If anyone could point me in the right direction I would greatly appreciate it.
Take a look at Axaptapedia passing values between forms. This should help you. You will probably have to modify your report to use a form for the dialog rather than using the base dialog methods of the report Here is a good place to start with that!
Just wanted to add this
You can use the MuliSelectionHelper class to do this very simply:
MultiSelectionHelper selection = MultiSelectionHelper::createFromCaller(_args.caller());
MyTable myTable = selection.getFirst();
while (myTable)
{
//do something
myTable = selection.getNext();
}
Here is the resolution I used for this issue;
Two methods on the report so that when fields are multi-selected on forms, the values are passed to the filter dialog;
private void setQueryRange(Common _common)
{
FormDataSource fds;
LogisticsControlTable logisticsTable;
QueryBuildDataSource qbdsLogisticsTable;
QueryBuildRange qbrLogisticsId;
str rangeLogId;
set logIdSet = new Set(Types::String);
str addRange(str _range, str _value, QueryBuildDataSource _qbds, int _fieldNum, Set _set = null)
{
str ret = _range;
QueryBuildRange qbr;
;
if(_set && _set.in(_Value))
{
return ret;
}
if(strLen(ret) + strLen(_value) + 1 > 255)
{
qbr = _qbds.addRange(_fieldNum);
qbr.value(ret);
ret = '';
}
if(ret)
{
ret += ',';
}
if(_set)
{
_set.add(_value);
}
ret += _value;
return ret;
}
;
switch(_common.TableId)
{
case tableNum(LogisticsControlTable):
qbdsLogisticsTable = element.query().dataSourceTable(tableNum(LogisticsControlTable));
qbrLogisticsId = qbdsLogisticsTable.addRange(fieldNum(LogisticsControlTable, LogisticsId));
fds = _common.dataSource();
for(logisticsTable = fds.getFirst(true) ? fds.getFirst(true) : _common;
logisticsTable;
logisticsTable = fds.getNext())
{
rangeLogId = addrange(rangeLogId, logisticsTable.LogisticsId, qbdsLogisticsTable, fieldNum(LogisticsControlTable, LogisticsId),logIdSet);
}
qbrLogisticsId.value(rangeLogId);
break;
}
}
// This set the query and gets the values passing them to the range i.e. "SO0001, SO0002, SO000£...
The second methods is as follows;
private void setQueryEnableDS()
{
Query queryLocal = element.query();
;
}
Also on the init method this is required;
public void init()
{
;
super();
if(element.args() && element.args().dataset())
{
this.setQueryRange(element.args().record());
}
}
Hope this helps in the future for anyone else who has the issue I had.

default values in LINQ query

I have following Linq query/function in my MVC3 application.
public AuditTrail GetNamesAddressesEmployers(long registryId , int changedField) {
var otherNameAndAddress = (from a in context.AuditTrails
where a.ChangedField == changedField
&& a.RegistryId == registryId
select a).FirstOrDefault();
return otherNameAndAddress;
}
I want that if otherNameAndAddress = null then its properties should be assigned some values.
otherNameAndAddress has Name and description property. This GetNamesAddressesEmployers is being used at 3 places. I want to assign different values to name and description when otherNameAndAddress = null at all three locations.
You're already using FirstOrDefault() so why not specify the Default:
public AuditTrail GetNamesAddressesEmployers(long registryId, int changedField)
{
return context.AuditTrails
.Where(a => a.ChangedField == changedField
&& a.RegistryId == registryId)
.DefaultIfEmpty(new AuditTrail { /* fill properties here */ })
.FirstOrDefault();
}
Well, you could change the return statement to:
return otherNameAndAddress ?? new AuditTrail { Name = "Default",
Description = "Default };
or something like that... but you say you want to assign different default values for different calls. That means you'll either need to pass the default in, or perform the defaulting (e.g. in the same way, via the null-coalescing operator) at the call site.
For example:
public AuditTrail GetNamesAddressesEmployers(long registryId, int changedField,
AuditField defaultValue) {
var otherNameAndAddress = (from a in context.AuditTrails
where a.ChangedField == changedField
&& a.RegistryId == registryId
select a).FirstOrDefault();
return otherNameAndAddress && defaultValue;
}
or keep it as it currently is, and use this at the call site:
var auditTrail = GetNamesAddressesEmployers(registryId, changedField) ??
new AuditTrail { Name = "Foo", Description = "Bar" };
It's not really clear which is best based on your description.
EDIT: As mentioned by Justin, you could use DefaultIfEmpty instead (just before FirstOrDefault). That means you have to pass the value in rather than doing it at the call site, but other than that they're very similar solutions.

To check if an object is empty or not

I want to check in my function if a passed argument of type object is empty or not. Sometimes it is empty but still not null thus I can not rely on null condition. Is there some property like 'length'/'size' for flex objects which I can use here.
Please help.
Thanks in advance.
If you mean if an Object has no properties:
var isEmpty:Boolean = true;
for (var n in obj) { isEmpty = false; break; }
This is some serious hack but you can use:
Object.prototype.isEmpty = function():Boolean {
for(var i in this)
if(i != "isEmpty")
return false
return true
}
var p = {};
trace(p.isEmpty()); // true
var p2 = {a:1}
trace(p2.isEmpty()); // false
You can also try:
ObjectUtil.getClassInfo(obj).properties.length > 0
The good thing about it is that getClassInfo gives you much more info about the object, eg. you get the names of all the properties in the object, which might come in handy.
If object containes some 'text' but as3 doesn't recognize it as a String, convert it to string and check if it's empty.
var checkObject:String = myObject;
if(checkObject == '')
{
trace('object is empty');
}
Depends on what your object is, or rather what you expect it to have. For example if your object is supposed to contain some property called name that you are looking for, you might do
if(objSomeItem == null || objSomeItem.name == null || objSomeItem.name.length == 0)
{
trace("object is empty");
}
or if your object is actually supposed to be something else, like an array you could do
var arySomeItems = objSomeItem as Array;
if(objSomeItem == null || arySomeItems == null || arySomeItems.length == 0)
{
trace("object is empty");
}
You could also use other ways through reflection, such as ObjectUtil.getClassInfo, then enumerate through the properties to check for set values.... this class help:
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
public class ReflectionUtils
{
/** Returns an Array of All Properties of the supplied object */
public static function GetVariableNames(objItem:Object):Array
{
var xmlPropsList:XMLList = describeType(objItem)..variable;
var aryVariables:Array = new Array();
if (xmlPropsList != null)
{
for (var i:int; i < xmlPropsList.length(); i++)
{
aryVariables.push(xmlPropsList[i].#name);
}
}
return aryVariables;
}
/** Returns the Strongly Typed class of the specified library item */
public static function GetClassByName($sLinkageName:String):Class
{
var tObject:Class = getDefinitionByName($sLinkageName) as Class;
return tObject;
}
/** Constructs an instance of the speicified library item */
public static function ConstructClassByName($sLinkageName:String):Object
{
var tObject:Class = GetClassByName($sLinkageName);
//trace("Found Class: " + tMCDefinition);
var objItem:* = new tObject();
return objItem;
}
public static function DumpObject(sItemName:String, objItem:Object):void
{
trace("*********** Object Dump: " + sItemName + " ***************");
for (var sKey:String in objItem)
{
trace(" " + sKey +": " + objItem[sKey]);
}
}
//}
}
Another thing to note is you can use a simple for loop to check through an objects properties, thats what this dumpobject function is doing.
You can directly check it as follow,
var obj:Object = new Object();
if(obj == null)
{
//Do something
}
I stole this from a similar question relating to JS. It requires FP 11+ or a JSON.as library.
function isEmptyObject(obj){
return JSON.stringify(obj) === '{}';
}
can use use the hasProperty method to check for length
var i:int = myObject.hasProperty("length") ? myObject.length: 0;

Resources