", "answerCount": 2, "upvoteCount": 0, "datePublished": "2024-03-07T10:13:12.000Z", "author": { "@type": "Person", "name": "翔 岩室", "url": "https://trailblazers.salesforce.com/profileView?u=0053A00000DiX7nQAF", "affiliation": { "@type": "Organization", "name": "--" } }, "acceptedAnswer": { "@type": "Answer", "text": "結局どうにもならなかったのでapex:actionFunctionに切り替えたところすんなりいきました。 この4日間はいったい何だったのか…   ", "upvoteCount": 1, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007ZC1VfSAL", "datePublished": "2024-03-12T10:11:34.000Z", "author": { "@type": "Person", "name": "翔 岩室", "url": "https://trailblazers.salesforce.com/profileView?u=0053A00000DiX7nQAF", "affiliation": { "@type": "Organization", "name": "--" } } }, "suggestedAnswer": [] } } Skip to main content

お世話になっております。

 

少々特殊な環境下で動作するVFページを実装しています。

動作環境の都合でJavascriptから値を取得する必要があり、Remote Actionを使用しました。

Apexでひとしきり処理を実行し、実行結果を文字列で返してalert()で出力したいと思っています。

検索やCopilotなどの助けを借りて下記JavascriptコードをVFに埋め込みました。

 

function method1(method){

// 環境特有の処理

method002(param1, param2).then(function(result){

alert(result);

}).catch(function(event){

alert(event.message);

});

}

function method2(param1, param2){

alert('No.1');

return new Promise(function(resolve, reject){

Visualforce.remoting.Manager.invokeAction(

'{!$RemoteAction.VisualforcePageController.apexRemoteActionMethod}',

param1,

param2,

function(result, event){

alert('No.2');

if (event.status) {

alert('No.3-1');

resolve(result);

} else {

alert('No.3-2');

reject(event);

}

},

{ buffer: true, escape: true, timeout: 30000 }

);

});

}

この状態でmethod1を実行したところalertが出力されるのですが、No.1のみ出力される場合とNo.3-1まで出力される場合が感覚ランダムで発生します。

Apex側に問題はなく、No.1のみの場合もNo.3-1まで実行された場合も想定されたメッセージが返されていることは確認できています。

No.3-1まで必ず実行できるようにするにはどうすればよいでしょうか。

 

また、このVFページにはapex:selectRadioが配置してあります(選択した値は別のJavascriptメソッド・変数経由でmethod2のparam2に入ります)。

method1実行後、画面上ではラジオボタンの選択がされたままなものの選択された値そのものは空、という状況になっております。

こちらの解消もしたいと思っているので、ご教示いただければと思います。

 

<apex:page controller="VisualforcePageController" standardStylesheets="true">

<apex:includeScript value="{!$Resource.JavascriptFile}"/>

<script>

var selectedValue = '';

function selectValue(value){

selectedValue = value;

}

</script>

<apex:outputPanel layout="block" style="">

<apex:form >

<apex:pageBlock >

<apex:pageBlockSection collapsible="false" columns="1">

<apex:selectRadio onchange="selectValue(this.value)" layout="pageDirection">

<apex:selectOptions value="{!selectList}"/>

</apex:selectRadio>

<apex:commandButton onclick="method1();" value="実行"/>

</apex:pageBlockSection>

</apex:pageBlock>

</apex:form>

</apex:outputPanel>

</apex:page>

2 answers
  1. Mar 12, 2024, 10:11 AM

    結局どうにもならなかったのでapex:actionFunctionに切り替えたところすんなりいきました。

    この4日間はいったい何だったのか…

     

    <apex:page controller="VisualforcePageController" standardStylesheets="true">

    <script>

    function executeApexMethod(){

    //環境特有の処理

    apexMethodFromJavascript(document.getElementById('formId').elements['radio'].value, javascriptValue);

    }

    </script>

    <apex:outputPanel id="panel" layout="block" style="text-align: center; width:280px; font-size: 80%;">

    <apex:pageMessages />

    <apex:form >

    <apex:pageBlock >

    <apex:pageBlockSection collapsible="false" columns="1">

    <apex:selectRadio value="{!selectedValue}" layout="pageDirection" id="radio">

    <apex:selectOptions value="{!selectList}"/>

    </apex:selectRadio>

    <apex:commandButton onclick="executeApexMethod(); return false;" value="処理実行"/>

    <apex:actionFunction name="apexMethodFromJavascript" action="{!apexRemoteActionMethod}" reRender="j_id0:panel">

    <apex:param name="param1" assignTo="{!selectedValue}" value=""/>

    <apex:param name="param2" assignTo="{!javascriptValue}" value=""/>

    </apex:actionFunction>

    </apex:pageBlockSection>

    </apex:pageBlock>

    </apex:form>

    </apex:outputPanel>

    </apex:page>

0/9000