How to add variable using BO RE Java SDK

I got an interesting task – add variable VERSION specifying the service pack to about 50 webi documents. I had to invent something more clever then going through all them manually.

The following function can be used to add a dimension variable with name name and value value to webi document widoc.

public static boolean addReplaceVariable(DocumentInstance widoc,
                                              String name,
                                              String value)
{
   ReportDictionary dic = widoc.getDictionary();
   VariableExpression[] variables = dic.getVariables();
   boolean found = false;
   for (VariableExpression e : variables) {
      if (e.getName().equalsIgnoreCase(name)) {
         System.out.println("variable " + name
             + " expression " + e.getFormula().getValue()
             + " was replaced with " + value);
         e.setValue(value);
         found = true;
         break;
      }
   }
   if (!found) {
      try  {
         dic.createVariable(name, ObjectQualification.DIMENSION, value);
         System.out.println("variable " + name
                          + " with value " +  value
                          + " has been created");
      } catch (Exception e) {
         System.out.println("ERROR: the variable " + name + " cannot be created");
         return false;
      }
   }
   return true;
}

It can be executed

if (addReplaceVariable(widoc, "VERSION", "=\"12.00.18.00\"")) {
   widoc.save();
}

More information

Getting started with BO RE Java SDK