0

Hi I am implementing live agent in our organization and I have created a visualforce page to get the object id focused in subtab. I am trying to pass the object id to my apex controller to use for case creation and other operations. Below is my code, in alert i am getting the record id but it is not passing it to apex controller.

VF PAGE:

<apex:page controller="ShowCaseInConsole" title=" End Chat Page ">
    <script> 
        var previousOnload = window.onload; window.onload = function()     
        { 
            if (previousOnload) { previousOnload(); }
            testGetFocusedSubtabObjectId(); 
        } 
    </script>
    <apex:includeScript value="/support/console/33.0/integration.js"/> 
    <apex:form >
        <!-- <a href="#" onClick="endChat();return false;" >End Chat</a> --> 
        <apex:actionFunction name="testGetFocusedSubtabObjectId" action="{!Show}" onComplete="alert('After apex method');">
            <apex:param name="cid" value="" assignTo="{!ConId}"/>
        </apex:actionFunction>
    </apex:form>
    <script type="text/javascript">
        function testGetFocusedSubtabObjectId() {
            sforce.console.getFocusedSubtabObjectId(showObjectId);
        }

        var showObjectId = function showObjectId(result) {
            // Display the object ID
            alert ('Object ID: ' + result.id);
        }; 
    </script> 
</apex:page>

APEX CONTROLLER :

public PageReference Show() {
    String contactid= Apexpages.currentPage().getParameters().get('Cid');
    system.debug('contact id from javascript function---'+contactid);
}

How do I access the id? thank you for any help.

Below is the code for updated function for which I would like to return the value, in Alert I am getting the value but it is not returning it to action function.

     function primarytabid(){
    //sforce.console.getPrimaryTabIds(showTabId);
    sforce.console.getEnclosingPrimaryTabId(showTabId);}
    var showTabId = function showTabId(result) {

            //Display the primary tab IDs

            alert('Primary Tab IDs: ' + result.id);
            var primaryTab = result.id;
            testGetDetailsByPrimaryTabId(primaryTab)
            };
       function testGetDetailsByPrimaryTabId(primary) {
        var primaryTabId = primary;
        alert('set value to variable'+primaryTabId);
        sforce.console.chat.getDetailsByPrimaryTabId(primaryTabId, getDetailsSuccess); 
    }

    function getDetailsSuccess(result) {
        //Report whether accepting the chat was succesful
       if (result.success == true) {
            console.log(result);
            alert('result'+result);
            chatKey = result.details.chatKey;
            alert('The chatKey for this chat is: ' + chatKey);
            var value=[];
            for (var i = 0; i < result.details.customDetails.length; i++) { 
                value.push(result.details.customDetails[i].value);
            }
            alert('The custom details : '+value.join(''));
        } else {
            alert('Getting the details was not Succesful');
        }
        AddcustomDetailValue(value.join(''));
    };

I am calling PrimaryTabId() function here and in my action function I am calling AddcustomDetailValue(). how do I return value?

1

You just pass the value you want to set into the function when you call it:

testGetFocusedSubtabObjectId('Id');

Replace 'Id' with your object id. That will set the ConId property in your controller to the value you pass in and you'll able to access it using that.

EDIT:

You would do something similar to pass in the result of a function. Using your example it would be:

var showObjectId = function showObjectId(result) {
            // Display the object ID
            testGetFocusedSubtabObjectId(result.id);
        }; 
  • Hi John, thank you for your response. I want to access the return value from the javascript function and use it in controller. will passing ID to the function help me do so? – apple123 Mar 6 '17 at 17:29
  • Sorry, I don't follow. Which javascript function are you getting a value from to use in the controller? – John Towers Mar 6 '17 at 17:32
  • <script type="text/javascript"> function testGetFocusedSubtabObjectId() { sforce.console.getFocusedSubtabObjectId(showObjectId); } var showObjectId = function showObjectId(result) { // Display the object ID alert ('Object ID: ' + result.id); }; </script> this function gives me the value I need to use in my controller. I need to access the result.id value in my controller and do operation. – apple123 Mar 6 '17 at 17:48
  • Got it. I updated the answer with a clearer example given your comments. – John Towers Mar 6 '17 at 18:05
  • Thank you for the update but that sends the function in an infinite loop and also the actionfunction is not calling SHOW method from the controller. – apple123 Mar 6 '17 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.