function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nobu.SNobu.S 

カスタムボタンにてWebServiceをコールする際の処理順序の制御の方法

商談の詳細画面に配置したカスタムボタンからApexのWebServiceを呼び出していますが、処理に非常に時間がかかるため、その間ローディング画面等にしておきたいと考えます。
BlockUIのプラグインを使うことを想定しています。

意図しているところは、ブロック->Webservice呼び出し->ブロック解除ですが、実際の挙動がWebService処理->ブロック->ブロック解除となってしまいます。(解除部分をSetTimeoutで確認)
Promiseを使用しても結果が変わりません。
意図する順番通りの処理となるような方法はありませんでしょうか?(BlockUIのツールにはこだわりません)
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
{!REQUIRESCRIPT('/resource/' &  LEFT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(NOW()),':',''),'-',''),' ',''),10) & '000/jQuery')} // includescript jQuery
{!REQUIRESCRIPT('/resource/' &  LEFT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(NOW()),':',''),'-',''),' ',''),10) & '000/jQueryBlockUI')} //includescript jQueryBlockUI

var $ = jQuery.noConflict(); 
var promise = Promise.resolve(); 

function block(){$.blockUI();}
function unblock(){$.unblockUI();}
function exeWS(){
var retStr; 
retStr = sforce.apex.execute("ApexClass", "WebserviceMethod", {oppId:'{!Opportunity.Id}'});
alert(retStr);
}

promise.then(block).then(exeWS).then(unblock);
window.location.reload();
Best Answer chosen by Nobu.S
Dinesh MultaniDinesh Multani
コールバック機能を使用する (use callback fucntion )
{ onSuccess: function(result){unblock();}, onFailure: function(error){unblock();} }

以下のリンクを参照してください (For more refere below link)
https://developer.salesforce.com/forums/?id=906F00000008x3bIAA

 

All Answers

thamos michaelthamos michael
can you explain in english please
Nobu.SNobu.S
[In English]
when calling Webservice from custom button, I'd like to show loading screen while webservice is processing because it takes few seconds.
Now I'm using BlockUI plug-in, and I expect the program sequence as 'block' -> 'call webservice' -> 'unblock'. However, it works as ''call webservice' -> 'block' -> 'unblock' even if I use 'Promise' chain method.
How can I handle the sequence as I expect? ('BlockUI is not mandatory)
 
Best regards,
Dinesh MultaniDinesh Multani
コールバック機能を使用する (use callback fucntion )
{ onSuccess: function(result){unblock();}, onFailure: function(error){unblock();} }

以下のリンクを参照してください (For more refere below link)
https://developer.salesforce.com/forums/?id=906F00000008x3bIAA

 
This was selected as the best answer
Nobu.SNobu.S
Dear Dinesh,

Thanks for your comment. I adjusted code and tried callback function, however, sequence didn't fixed. Webservice ran before block.
Do you have any other idea?
var block = function(callback){ 
 $.blockUI();
 callback();
}
var exeWS = function(){
 var retStr; 
 retStr = sforce.apex.execute("BYD_Interface", "interfaceSalesOrder", 
  {oppId:'{!Opportunity.Id}'});
 alert(retStr);
}

function unblock{$.unblockUI();}

block(exeWS); 
unblock();
Dinesh MultaniDinesh Multani
Here is the working code that works properly (Load and Unload). You can tweak this and achieve requirement.
Hint : You can use oncomplete event of  apex:commandButton where you can call JavaScript function.   
<apex:commandButton action="{!GetResult}" status="updateStatus"  value="Click Here"/>

<apex:actionStatus id="updateStatus">
            <apex:facet name="start">
                <div style="z-index: 1000; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: black; opacity: 0.25">

                </div>
                <div style="z-index: 1001; text-align: center; vertical-align: middle; position: fixed; top: 50%; left: 50%; padding: 1em; margin: -2em -10em; width: 20em; height: 32px; background: white; border-radius: 5px">
                    <img src="/img/loading32.gif" style="vertical-align: middle; padding-right: 1em" />
                    Loading...
                </div>
            </apex:facet>
        </apex:actionStatus>