$tm1Ui.cellGet only works when run twice

I have the following code:

    $tm1Ui.cellGet('dev', 'Details',$rootScope.$stateParams.costing,$rootScope.$stateParams.version,'10 Year Costing').then(function(data){
        $scope.page.tenYears = data.Value;
    })
    console.log($scope.page.tenYears)

If I change “10 Year Costing” in TM1 from “N” to “Y” and refresh once it shows "N"
If I refresh again it shows “Y”

As a workaround I tried repeating the $tm1Ui.cellGet, but I still get the same result.

Am I missing something?

Thanks
Mal

You should move console.log into callback function

$tm1Ui.cellGet('dev', 'Details',$rootScope.$stateParams.costing,$rootScope.$stateParams.version,'10 Year Costing').then(function(data){
    $scope.page.tenYears = data.Value;
    console.log($scope.page.tenYears);
})

Hi @mmacdonnell,

The cellGet call is asynchronous returning a “promise”, that means the function inside the .then part will return sometime in the future but not straightaway.

In you case the console.log is being executed BEFORE the cellGet part has been executed / returned.

Thanks Andrey,

That makes it log correctly, but I’m using the value in an “if” later and it still uses the old value

        if ($scope.page.tenYears === "Y") {
            $scope.mdx = {

So I have something like this (you may recognise some of this):

$scope.getYears = function () {

    $tm1Ui.cellGet('dev', 'Details',$rootScope.$stateParams.costing,$rootScope.$stateParams.version,'Display Financial Year').then(function(data){
        $scope.page.mdxYear1 = data.Value;
 ....
    })
    $tm1Ui.cellGet('dev', 'Details',$rootScope.$stateParams.costing,$rootScope.$stateParams.version,'10 Year Costing').then(function(data){
        $scope.page.tenYears = data.Value;
        console.log($scope.page.tenYears)

};

$scope.getData = function () {
    $scope.getYears();
        if ($scope.page.tenYears === "Y") {
            $scope.mdx = {
                ...
            }
        } else {
            $scope.mdx = {
              ...
            }
        }
        $tm1Ui.cubeExecuteMdx('dev', $scope.mdx.default).then(function (result) {
            $scope.dataset = $tm1Ui.resultsetTransform("dev", "Volumes Effort", result);
            console.log($scope.dataset)
        });
    })
};

When I do the if ($scope.page.tenYears === “Y”) it needs to be run twice to see the updated value

OK, just read Tim’s response.

That makes sense.

How can I do this synchronously?

I want to Change my MDX string depending on a cube value

Hi @mmacdonnell,

The short answer is you can’t do it synchronously. There are a few ways to achieve a result though, easiest it to add the getYears code from the function into getData and then put the getData logic inside the promise from cellGet:

$scope.getData = function () {
  $tm1Ui.cellGet('dev', 'Details',$rootScope.$stateParams.costing,$rootScope.$stateParams.version,'10 Year Costing').then(function(data){
    $scope.page.tenYears = data.Value;
    if ($scope.page.tenYears === "Y") {
        $scope.mdx = {
            ...
        }
    } else {
        $scope.mdx = {
          ...
        }
    }
    $tm1Ui.cubeExecuteMdx('dev', $scope.mdx.default).then(function (result) {
        $scope.dataset = $tm1Ui.resultsetTransform("dev", "Volumes Effort", result);
        console.log($scope.dataset)
    });
  });
};

OK I did try that before my last post and still got the same issue.

I’ll try again in case I just got it wrong

It’s working now, I must’ve got it wrong last time.
Thanks