0 Replies Latest reply on Apr 24, 2019 9:38 AM by qc4vmware

    Working with vCACCAFELiteralMap class... convert to JSON?

    qc4vmware Master

      I really hate working with the vCACCAFELiteralMap class.  I've written a small utility to convert them to JSON so I can more easily explore the contents and interact with the data contained within.  Before I spend too much more time on this effort I wanted to make sure I wasn't missing a built in way to accomplish this.  Its not really an issue getting the data once I know where things are its just a pain picking it apart.  I'll include my code as well for comment.  I'm using the .getExtensionData() method from a reservation as my test data

       

      var literalMap = res.getExtensionData();

      var jsonObj = convertLiteralMapToJson(literalMap);

      System.debug(JSON.stringify(jsonObj));

       

       

      function convertLiteralMapToJson(literalMap) {

      var mapObj = {};

      for each (var key in literalMap.keySet()) {

      var obj = literalMap.get(key);

      var className = System.getObjectClassName(obj);

      if (className == "vCACCAFEStringLiteral" || "vCACCAFEDateTimeLiteral" || "vCACCAFEIntegerLiteral" ||

          "vCACCAFEBooleanLiteral" || "vCACCAFEDecimalLiteral" || "vCACCAFESecureStringLiteral") mapObj[key] = obj.getValue();

       

       

      if (className == "vCACCAFEComplexLiteral") mapObj[key] = convertComplexLiteralToJson(obj);

       

      if (className == "vCACCAFEMultipleLiteral") {

      multClassName = System.getObjectClassName(obj.getValue());

      mapObj[key] = [];

      for each (var item in obj.getValue()) {

      var itemClassName = System.getObjectClassName(item);

      if (itemClassName == "vCACCAFEComplexLiteral") {

      mapObj[key].push(convertComplexLiteralToJson(item));

      }

      else {

      System.debug("unhandled itemClassName::" + itemClassName);

      }

       

       

      }

      }

       

      if (className == "vCACCAFEEntityReference" ) {

      mapObj[key] = {};

      mapObj[key].referenceType = "vCACCAFEEntityReference";

      mapObj[key].classId = obj.getClassId();

      mapObj[key].componentId = obj.getComponentId();

      mapObj[key].id = obj.getId();

      mapObj[key].label = obj.getLabel();

      mapObj[key].typeId = obj.getTypeId();

      mapObj[key].label = obj.getLabel();

      }

       

       

      if (mapObj[key] == undefined) {

      System.log(key + " unhandled className::" + className);

      mapObj[key] = "QCconvertLiteralMapToJson: This mapping has an unhandled class for key:" + key + ", class: " + classname ;

      }

      };

       

      return mapObj;

      };

       

       

      function convertComplexLiteralToJson(complexLiteral) {

      var complexClass = System.getObjectClassName(complexLiteral.getValue());

      if(complexClass == "vCACCAFELiteralMap") {

      return convertLiteralMapToJson(complexLiteral.getValue());

      }

      else {

      System.log("not sure what to do with complexClass::" + complexClass);

      return "QCconvertComplexLiteralToJson: This complex literal has an unhandled class:" + complexClass ;

      }

      }