We’ve developed some resources to help you work effectively from home during COVID-19 Click to learn more

foreach keyword in CRMScript?

Hi,

we write CRMScript all day. Everyday. All the time.

One thing that I wish CRMScript had was support for foreach.

So instead of this long syntax

for (Integer i = 0; i < contacts.length(); i++)
{
  NSContact contact = contacts[i];
  //...
}

I wish we could use this shorter variant

foreach (NSContact contact in contacts)
{
  //...
}

What do you other CRMScript developers out there think?

RE: foreach keyword in CRMScript?

Yes, for gods sake, yes!

Von: Pär Pettersson 11. Jun 2020

RE: foreach keyword in CRMScript?

We really appreciate your enthusiasm for CRMScript :-)

Coming soon to a release near you 👍

Sverre

Von: Sverre Hjelm 15. Jun 2020

RE: foreach keyword in CRMScript?

Sweet :)

Von: Simen Mostuen Iversen 15. Jun 2020

RE: foreach keyword in CRMScript?

Great addition! While this work for all types?

Von: David Hollegien 15. Jun 2020

RE: foreach keyword in CRMScript?

Nice, thank you very much!

Von: Frode Lillerud 16. Jun 2020

RE: foreach keyword in CRMScript?

Yes, it will work for any type, including your own structs. Here is my test-script which shows some variations:

#setLanguageLevel 3;

String[] array;
array.pushBack("Time");
array.pushBack("is");
array.pushBack("an");
array.pushBack("illusion");

foreach (String s in array) {
  print(s + " ");
  s = s + " was here"; // Does not change array elements
}

struct Person {
  String firstname;
  
  Void print() {
    printLine("Person: " + this.firstname);
  }
};

Person Person(String firstname) {
  Person p;
  p.firstname = firstname;
  return p;
}

Person[] persons;

persons.pushBack(Person("Steve"));
persons.pushBack(Person("Bill"));

foreach (Person p in persons) {
  p.print();
  p.firstname = p.firstname + " was here"; // Changes array elements
}

foreach (Person p in persons) 
  p.print();

Note: foreach'ing an array of basic types, such as strings, will not let you manipulate them as they are "pass by value", while all other types can be manipulated as they are "pass by reference". This is standard CRMScript behaviour.

Also, fun fact: Stian did some performance testing and it seems that foreach is twice as fast as index-looping.

Sverre

Von: Sverre Hjelm 16. Jun 2020

RE: foreach keyword in CRMScript?

Nice :D 

Von: Suran Basharati 16. Jun 2020

RE: foreach keyword in CRMScript?

Sverre,

Sounds great! Especially the performance improvement!

Now that we are on the topic of improvements, next to writing for loops I think that following is something that I type multiple times a day:

SearchEngine se;

// configure search

for (se.execute(); !se.eof(); se.next())
{
    // process 
}

Would it be possible to also create a short-hand for the execute(); eof(); next() syntax? Would same a lot of repeated key strokes!

Von: David Hollegien 16. Jun 2020

RE: foreach keyword in CRMScript?

Very nice! 

Btw, any chance for struct and members to be dynamically added to intellisense? 
Would make life easier when you #include other scripts that contain functions/structs/whatever you need to use. 

//Eivind

Von: Eivind Johan Fasting 16. Jun 2020

RE: foreach keyword in CRMScript?

Building on Davids example, perhaps something like this. SearchEngine could have a execute method that returns an iterator, and perhaps a new type "Result"(?) would be needed.

SearchEngine se;
se.addField("contact.contact_id");
se.addField("contact.name");
for (Result row in se.executeResult())
{
  Integer contactId = row.get("contact.contact_id").toInteger();
  String name = row.get("contact.name");
}

 

Von: Frode Lillerud 16. Jun 2020