TCS Tools: Expressions Syntax (Introduction)

By | May 1, 2017

Here is what you should know about the expressions syntax to start working with them.

Solution entities:

  • TCS Expression – use it to create and register code expressions
  • TCS Number Sequence – works in combination with the Sequence function. Normally, there is no need to work with this entity directly. However, you can manually update “Next Value” attribute on the corresponding Number Sequence record if you want to move next number up or down for a particular sequence

What you can do:

  • You can introduce a variable without having to declare it (Ex: i = 5)
  • You can use while and foreach loops
  • You can use function syntax to introduce reusable code
  • You can use new operator as a constructor
  • You can access a lot of standard .NET framework classes and use them in y our expressions. This is done through reflection in a very generic manner, so there is no guarantee any particular class will be fully supported (you will have to test it, and, if it’s not working, I may need to look into why. Still no guarantee, though)
  • You can use “if/else”
  • You can access the record for which your expression is running through the built-in “target” variable
  • You can register your expression to run for “Update” (pre-op), “Create”(pre-op), “Delete”(pre-op), “Retrieve”(post-op), “RetrieveMultiple”(post-op)
  • You can use built-in CRMContext object
  • You can use Sequence function
  • You can raise an error with RaiseError function

What you don’t need to do:

  • You do not need to declare types

What you cannot do:

  • There are some limitations on what you can do with generic methods/types
  • You cannot define namespaces or classes
  • You cannot use try-catch
  • You cannot use “throw” operator, but you can use RaiseError function instead
  • You cannot access plugin context and/or organization service directly (working on it)

 

Below are some examples of the code you can put into the expressions

1. Using c# string functions

s = “testTESTtest”;
i = s.IndexOf(“TEST”);
j = s.IndexOf(“test”, i);

2. Using “if”

i = 0;
if(CRMContext.Target!= null)
{
i = 1;
}

3. Accessing current record attributes

if(CRMContext.Target.Contains(“telephone1”))
{
CRMContext.Target[“name”] = “Has main phone #”;
}

4. You can access framework classes

i = Math.Abs(-1) + Math.Abs(-2);

5. You can access framework classes

i = Math.Abs(-1) + Math.Abs(-2);

6. You can use foreach with collections

a = 1;
foreach(b in CRMContext.Target.Attributes)
{
if(b.Key == “”tcs_integer””)
{
a = b.Key;
}
}

7. You can use constructors

a = new Money();
a.Value = new Decimal(10);
CRMContext.Target[“tcs_money”] = a;
8. You can define simple functions

function test(i, j)
{
i=10;
return i+j;
}
test(Math.Abs(-1),test(2,3));
9. You can use while loop

i=5+1;
while(i < 20)
{
i = i+2;
}
10. You can use if/else

if(i < 25){
i = 25;
}
else i = 10;

11. You can use RaiseError to throw a regular InvalidPluginExcecutionException(hint: this helps a lot when debugging)

RaiseError(“Your message here”);

You can also use CRMContext object and Sequence function – read more..

 

2 thoughts on “TCS Tools: Expressions Syntax (Introduction)

  1. Jeroen

    Hi Alex,
    Great tooling this is, thanks for publishing!
    I was wondering, could this also be used to start an executable or batch file on a Dynamics server? Either my syntax isn’t right or it just isn’t possible.

    Thanks for the moment.

    Regards,
    Jeroen

    Reply
    1. Alex Shlega Post author

      You probably won’t be able to do it. All the code will be running in the sandbox, and starting an executable from the sandbox would not be possible.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *