2017-04-20 10 views
1

以下のコードは、インナー関数からブール値を親関数displayButton()に返しますか?親関数は、ダイナミクスCRMのボタンをクリックすると呼び出されます。この関数は、ケースが選択され、選択されたものがアクティブか解決されているかどうかに応じてブール値を返す必要があります。内部関数から親関数に戻り値を返す - JavaScript、Dynamics crm

//function called on click of a button in ms crm. 
    function displayButton() 
    { 
     var Obj = parent.Xrm.Page.getAttribute("regardingobjectid"); 
     var ObjValue = Obj.getValue(); 
     //parent.Xrm.Utility.alertDialog(" Value: " + ObjValue); 
     if (ObjValue == null) 
      return false; 
     //else 
     // parent.Xrm.Utility.alertDialog(" Hi"); 

     var EntityType = ObjValue[0].entityType; 

     var Guid = ObjValue[0].id; 
     var id = Guid.slice(1, -1); 
     //parent.Xrm.Utility.alertDialog(" Guid: " + id); 

//Checking if regarding field is selected a case lookup value 
     if (EntityType == "incident") 
     { 
      var req = new XMLHttpRequest(); 
      req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(" + id + ")?$select=statecode", true); 
      req.setRequestHeader("OData-MaxVersion", "4.0"); 
      req.setRequestHeader("OData-Version", "4.0"); 
      req.setRequestHeader("Accept", "application/json"); 
      req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
      req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); 

      req.onreadystatechange = function() 
      { 
       if (this.readyState === 4) 
       { 

        req.onreadystatechange = null; 
        if (this.status === 200) 
        { 

         debugger; 
         var result = JSON.parse(this.response); 

//checking if selected case is active or resolved. 
         var statecode = result["statecode"]; 

         var statecode_formatted = result["[email protected]"]; 
         if (statecode_formatted == "Active") { 
          return true; 

         } 
         else if (statecode_formatted == "Resolved") 
          return false; 
         else { 
          return false; 
         } 
        } 
        else 
        { 
         parent.Xrm.Utility.alertDialog("Zero"); 
        } 


       } 

      }; 
      req.send(); 

     } 
     else { 
      return false; 
     } 


    } 

答えて

0

いいえ、どちらかあなたがif (this.status === 200)であなたのロジックを配置する必要がありますスコープを作成するか、コールバックを提供します。それを渡し、その後getCaseStateを呼び出す

function getCaseState(id, callback) { 
    var req = new XMLHttpRequest(); 
    req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(" + id + ")?$select=statecode", true); 
    req.setRequestHeader("OData-MaxVersion", "4.0"); 
    req.setRequestHeader("OData-Version", "4.0"); 
    req.setRequestHeader("Accept", "application/json"); 
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); 

    req.onreadystatechange = function() { 
     if (this.readyState === 4) { 
      req.onreadystatechange = null; 
      if (this.status === 200) { 
       var result = JSON.parse(this.response); 
       var statecode = result["statecode"]; 
       var statecode_formatted = result["[email protected]"]; 

       callback(statecode_formatted); 
      } 
     } 
    }; 
    req.send(); 
} 

req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(" + id + ")?$select=statecode", true); 

は、コールバックを提供する2つの関数にコードを分離する:

あなたXMLHttpRequestがあるため、この行でtrueパラメータの非同期ですインシデントのidと、リクエストが完了したら電話をかける機能が用意されています。

// called on click of a button in CRM. 
function displayButton() { 
    var regarding = parent.Xrm.Page.getAttribute("regardingobjectid").getValue(); 
    var entityType = regarding[0].entityType; 
    var regardingId = regarding[0].id.slice(1, -1); 

    // Check if regarding is an active case. 
    if (entityType == "incident") { 
     getCaseState(regardingId, function(state) { 
      var isActive = state === "Active"; 

      if (isActive) { 
       // TODO 
      } 
     }); 
    } 
} 

上記のコードで渡される関数は匿名です。それをさらに分離して名前を付ける必要があります。

+0

これも機能しません。 isActive変数にtrueまたはfalseの値が含まれていますが、親関数にtrueまたはfalseが返されません。私はそれを書いた if(isActive){ trueを返します。 } else { falseを返します。 } displayButton関数にtrueまたはfalseを返しません。 – Dinoop

+0

あなたはそれが動作すると期待どおりに動作しません。これは私の答えとあなたの質問に対する他の答えが両方とも** no **である理由です:あなたはあなたの親の関数への非同期要求の結果を返すことはできません。内部関数の結果を処理する場合は、コールバックを使用する必要があります。'displayButton'自体が親関数ではなく、親関数を持っている場合は、最初にコールバックを' displayButton'に渡してから 'getCaseState'に渡します。 'displayButton'が親関数であれば、' isActive'の結果を扱っているので、 'return true'を使う必要はありません。 –

+0

displayButtonは親関数です。私はisActiveを書いて返しました: getCaseState(aboutId、function(state){ var isActive = state === "アクティブ"; return isActive }); crmリボンのボタンは、displayButtonの戻り値に依存します。しかし、ケースが解決されてもボタンは表示されています。大文字と小文字が解決されたときにisActiveはfalseであるため、ボタンは表示されません。 – Dinoop

0

短い答えはノーです。内部関数が定義され、req.onreadystatechangeに割り当てられます。 displayButton()は内部関数を決して呼び出しません。したがって、このコンテキストでは実行されないため、値は返されません。

おそらくこれはJSの関数に深く掘る良いかもしれない:あなたは、非同期XmlHttpRequestによって返された値にアクセスしたい場合はhttps://www.w3schools.com/js/js_function_definition.asp

+0

ありがとうございました。ケースがアクティブであるかどうかに応じて、値を返すように編集するにはどうすればよいですか? – Dinoop

0

まず最初に、あまりにも多くのコードを貼り付けました。 Minimal, Complete, and Verifiable example あなたのコードをあまり変更せずに行う唯一の方法は、提案されているようにリクエストを同期に変更し、結果をコールバックの外で定義された変数に代入することです。そのような何か:

function displayButton(){ 
    var result = false; //or true, depends what you want as default 
    //some code 
    //change it to synchonous request!! 

    var req = new XMLHttpRequest(); 
    req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(" + id + ")?$select=statecode", false); 
    //... 
    req.onreadystatechange = function() { 
     if (this.readyState === 4) { 
      req.onreadystatechange = null; 
      if (this.status === 200) { 
        //ommiting more code to get to the clue 
        //... 
        //... 
        if (statecode_formatted == "Active") { 
         result = true; 
        } 
        else if (statecode_formatted == "Resolved") 
         result = false; 
        else { 
         result = false; 
        } 
      } 
     } 
    }; 
    req.send(); 

    return result;  
} 

あなたはそれをあまりにも多くを貼り付けたので、私は、あなたの全体のコードを変更する必要はありませんが、私はあなたのアイデアを持っていると確信しています。別の答えで示唆されているように、コールバック関数を別の関数に移動し、このコールバックに "result"を代入する必要があります。コールバックは同期して実行されるので、関数は適切な値で "結果"を返します。

関連する問題