How to correctly check for datatype

Hello, 

I have a case where i need to check if a given string is a valid Integer, Float or just a String. 

I've created a function to do it, but i'm unsure if there is a standard method/something i can use instead. 
So far i got this: 

String checkType(String text){
    if(text.isDigit()){
      return "Integer";
    }
    try{
      Float float = text.toFloat();
      if(float > 0){
        return "Float";
      }
    }
    catch{
      printLine("Exception caught: " + error);
    }
    return "String";
  }

This works well enough, as it first checks if the text .isDigit() , then it does a try/catch to figure out if it's able to convert it to a float, and if none of the above is valid it returns String. 

Could a good idea be to implement a standard function in CRMscript that spits out what datatype it is? It could be very usefull when working with xml. 

Any ideas/inputs are appreciated :) 

//Eivind

RE: How to correctly check for datatype

Hi Eivind,

 

This might not be what you are looking for in this scenario, but there is a function to check the datatype of a given variable.

String myString = "Petter";
Integer myInt = 3;
Bool myBool = false;

print(getTypeName(myString));
print(getTypeName(myInt));
print(getTypeName(myBool));

However, I could not find this one in the documenation , so could be that it is deprecated.

Do you need to use a String for this? Otherwise maybe this would be a good case for using Generic. :)

By: Petter Näslund 25 Mar 2021

RE: How to correctly check for datatype

Hello, 

Thank you for your input. 
I was not aware this function existed, its weird that its lacking from the intellisense (?). 

Anyways, i've expanded my example and put this more into context: 

#setLanguageLevel 3;

String checkNodeType(String text){
  if(text.isDigit()){
    return "Integer";
  }
  try{
    Float float = text.toFloat();
    if(float > 0){
      return "Float";
    }
  }
  catch{
    printLine("Exception caught: " + error);
  }
  return "String";
}

String json = '{"TullFloat": 23.23, "TulLString": "assad", "TullInteger": 123}';

XMLNode root = parseJSON2(json);
XMLNode[] childNodes = root.getChildren();
forEach(XMLNode node in childNodes){
  printLine(node.getName() + " is of type: " + getTypeName(node.getText()));
  printLine("Custom function returns type: " + checkNodeType(node.getText()));
}


Unfortunately getTypeName claims they are all Strings, which probably means .getText passes it in as a String. 

As you can see from my example i'm putting in a json-string that gets parsed into an XMLNode to be processed. I need to figure out what dataType each node is, so i loop through the childNodes and need it to handle it if anything is wrong. 
E.g. if i get a STRING where i am expecting an INT i need my code to pick up that something is wrong. 

//Eivind



By: Eivind Johan Fasting 25 Mar 2021

RE: How to correctly check for datatype

Update:

I found a different way of doing it. 
When using the parseJSON2-function it actually sets the parameter 'type' on the node, so you can get that parameter to decide if its a String, Bool or Number. It doesnt look like it differentiate on number or float but i just edited my own function to handle that. 

/Eivind

By: Eivind Johan Fasting 25 Mar 2021