RE: foreach keyword in CRMScript?
Have given the idea of iterate(type) {...} a little more thought and have a question which may impact foreach.
I know you said the language is not well fitted for creating some kind of generic iterator class, but just brainstorming here...
Would it not be possible to create a generic KeyValue type that internally transforms a Map, SearchEngine, GenericSearch, AppointmentSlicer, etc when called in a foreach into a KeyValueCollection that can be iterated over? Would this not be what you do anyway in an iterate {...} statement?
What if....the KeyValue Value property exposed relevant accessors?
Map map;
map.insert("1", "one");
map.insert("2", "two");
map.insert("3", "three");
map.insert("4", "four");
map.insert("5", "five");
foreach (KeyValue kv in map) {
print(kv.getKey + " " + kv.getValue); // key as string, and value is value
}
//-----------------------------------------------------------------------------
SearchEngine se;
se.addField("ticket.cust_id.firstname");
se.addCriteria("ticket.status", "OperatorEquals", "1", "OperatorAnd", 0);
foreach (KeyValue kv in se) {
print(kv.getKey + " " + kv.getField(0)); // key as string, and getField functions.
// or
print(kv.getKey + " " + kv.getValue().getField(0));
}
//-----------------------------------------------------------------------------
String sql = "select id,title from ticket where status = 1";
StatLib sl;
sl.setSql(sql);
StatResult sr;
sl.execute(sr);
foreach (KeyValue kv in sr) {
print(kv.getKey + " " + kv.getField(0)); // key as string, and getField functions.
// or
print(kv.getKey + " " + kv.getValue().getField(0)); // key as string, and getField functions.
}
With regards to the KeyValue.Value property, perhaps this is a good time to make use of the new Generic data type and expose the range of Generic methods either off the KeyValue.Value property, and whatever would be needed for accessing a fields collection?
//-----------------------------------------------------------------------------
SearchEngine se;
se.addField("ticket.cust_id.firstname");
se.addCriteria("ticket.status", "OperatorEquals", "1", "OperatorAnd", 0);
foreach (KeyValue kv in se) {
print(kv.getKey + " " + kv.getGenericValue());
// or
print(kv.getKey + " " + kv.getGenericValue(String fieldName));
// or
print(kv.getKey + " " + kv.getGenericValue(Integer fieldIndex));
}
By the way, a search for GenericSearch and AppointmentSlicer didn't return a single result in these forums - apart from this thread.
Best regards.