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

CRMScript - If statement that checks if value is in array

Hello! 

The title might not be that accurate. I would like to write up a Script Trigger before saving sale that does something if the source of the sale is 1 of 3.

How can that be resolved with the language of CRMScript? 

I mostly know SQL and a little javascript so this is how I can illustrate what I mean: 

#setLanguageLevel 3;

EventData ed = getEventData();

Integer sourceId = ed.getInputValue("SaleEntity.Source.Id").toInteger();

if(sourceId in (11,128,135)) //This is the bit im struggeling with
{
ed.setMessage("This is a message");
ed.setBlockExecution(true);
}

Thank you for any input :) 

RE: CRMScript - If statement that checks if value is in array

'In' is typical SQL dialect and not supported by CRMScript AFAIK.

Use 'or' instead:

if((sourceId == 11) || (sourceId == 128) || (sourceId == 135)) 

Von: Rob Jansen 30. Jun 2020

RE: CRMScript - If statement that checks if value is in array

Thank you Rob, in general is there another option in the CRMScript rather than using the OR operator? 

Von: Martine Sivertsen Nilsen 30. Jun 2020

RE: CRMScript - If statement that checks if value is in array

Hello,

You could also put the values you want to trigger on in a Map:

#setLanguageLevel 3;

Map sourceIds;
sourceIds.insert("11", "");
sourceIds.insert("128", "");
sourceIds.insert("135", "");

EventData ed = getEventData();

Integer sourceId = ed.getInputValue("SaleEntity.Source.Id").toInteger();

if (sourceIds.exists(sourceId.toString()))
{
    ed.setMessage("This is a message");
    ed.setBlockExecution(true);
}
Von: David Hollegien 30. Jun 2020

RE: CRMScript - If statement that checks if value is in array

Thank you, David!

Von: Martine Sivertsen Nilsen 3. Jul 2020