[SalesForce] How to pass id from javascript function to apex controller

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?

Best Answer

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);
        };