0

Could you please to help this? I'm very new with javascript.I have tried many way but it doesn't work. I have problem with javascript function I have tried to call method from controller but it doesn't work. I already provide my code as below. AS my understand, The method from my controller doesn't call from javascript function because I have check in dev console. My controller doesn't excute. However, I tried to console log values in javascript function. Both variable have values. and I have found this error in my webconsole.

"HTTP-Based Public Key Pinning is deprecated. Chrome 69 and later will ignore HPKP response headers. (Host: hpcc-testliveagenthp.cs72.force.com) "

Thank you in advance

My VF

 <textarea id="comment" cols="50" row="10" values="{!surveycomments}" ></textarea>
 <button class="btn btn-sucess" type="button" id="postchat_submit" onclick="doSave();">Submit</button>

Javacript

<script> 
function doSave(){
var comments = document.getElementById("comment").value;
console.log('comment is '+comments);
var ChatKey = '{!$CurrentPage.parameters.ChatKey}'
console.log('ChatKey is '+ChatKey);
HPCC_PostChatSurveyController.saveSurvey(ChatKey, comments,  function(result, event){
        if(event.type === 'exception'){
            console.log("Exception");
            console.log(event);
        }   
        else if (event.status){
            console.log(result);
            if(result = 'Success'){
                alert("Successfully save! Thank you")
            }
            } else{
              console.log(event.message);
              }
        });
}
</script>

Controller

public with sharing class HPCC_PostChatSurveyController {

    public LiveChatTranscript livechatObj {get; set;}
    public String surveycomments {get; set;}

    public HPCC_PostChatSurveyController() {
        surveycomments = '';
    }

    @RemoteAction
    public static String saveSurvey(String chatkey, String comments){
        try{
            String key = chatkey;
            LiveChatTranscript liveObj = new LiveChatTranscript();

            liveObj = [SELECT Id, ChatKey , HPCC_Comment__c FROM LiveChatTranscript WHERE ChatKey=: key LIMIT 1];
            liveObj.HPCC_Comment__c = comments;

            update liveObj;
            return 'Success';
        }catch(Exception e){
            system.debug('Exception: '+e.getMessage());
            return 'Exception';
        }
    }

}

I also tried javascript like below

Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.HPCC_PostChatSurveyController.saveSurvey}',JSON.stringify(ChatKey), JSON.stringify(comments),function (result, event) {
      if (event.statusCode == 200) {
        // yay!
      }
      }, { escape : false })

1 Answer 1

2

Using a button in a form causes a form submit. This automatically cancels any scripts and network activity. To prevent his behavior, you need to "return false" to tell the button not to perform the default action.

<button class="btn btn-sucess" 
        type="button" 
        id="postchat_submit" 
        onclick="doSave(); return false;">Submit</button>
4
  • Or use <input type=button../>
    – Jayant Das
    Nov 14, 2018 at 17:27
  • I have add false like your advice. However My controller also didn't work. I have checked with my developer console.
    – W Nim
    Nov 14, 2018 at 17:29
  • @WNim What do you mean by "didn't work"? How do you know it didn't work? Any errors? Record not updated? How about the Salesforce Developer Console to see if you get any logs?
    – sfdcfox
    Nov 14, 2018 at 17:44
  • @sfdcfox After I clicked submit, I have got 3 vales from web console and one error message as same as above 1.comment is test 123 test 123 2. ChatKey is 75142cb3-572d-47e6-9dd1-f7d0556ac4e8 3.Exception Moreover, My record are not updated and Salesforce Developer Console didn't show log of my controller.
    – W Nim
    Nov 14, 2018 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .