Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

i am currently having certain hyperlinks in VF page which i need to make compatible in service cloud and hence using the API toolkit.I am quite new to callback methods, what is the syntax to pass arguments into the callback method.Any help will be very useful, below is the current code i am using which is working fine but its doesn't take any arguments. Is there a way i can use like openReportedContact('{!caserec.id'}) and use this parameter in the callback function callBackopenReportedContact(result,'{!caserec.id'}) as well. The purpose is to write a generic JS method, curretnly i have 3 JS method doing the same work and want to write one generic JS.

VF Page
<A HREF="#" onClick="openReportedContact();return false">{!caserec.contact.name}</A>

function openReportedContact() {
     if(!sforce.console.isInConsole())
         window.parent.location.replace('/{!case.ContactId}');
     else sforce.console.getEnclosingPrimaryTabId(callBackopenReportedContact);
}

function callBackopenReportedContact(result) {
 sforce.console.openSubtab(result.id, '/{!case.ContactId}',true,'{!case.contact.name}', null);
};
share|improve this question
    
Your code seems alright and like you say it works. Do you additionally want to launch the case in a subtab along with the related contact? What is the primary tab? –  techtrekker Dec 4 '12 at 9:06
    
@techtrekker, there are multiple contact lookup fields on the case and clicking in each link a different contact record detail page needs to be displayed. If you see this method i am using this only for the contactid field of case, incase i need to use the same logic for another custom lookup i end up writing another JS function(with that recordid) which i want to avoid. –  Anil Dec 4 '12 at 9:11
add comment

2 Answers

up vote 3 down vote accepted

A better way to do this is with a closure. The code is very similar to your example, but we can lose the global variables if we change the callBackopenTab function to be an anonymous function defined inline. It then has access to the local variables in the enclosing scope:

<A HREF="#" onClick="openTab('{!caserec.contactid}','{!caserec.contact.name}');return false">{!caserec.contact.name}</A>  

function openTab(recid, recname) {
  if(!sforce.console.isInConsole())
    window.parent.location.replace('/'+recid);
  else
    sforce.console.getEnclosingPrimaryTabId(function(result) {
      sforce.console.openSubtab(result.id, '/'+recid , true, recname, null);
    });
}

If you really wanted to keep the callBackopenTab function, you could alternatively do:

<A HREF="#" onClick="openTab('{!caserec.contactid}','{!caserec.contact.name}');return false">{!caserec.contact.name}</A>  

function openTab(recid, recname) {
  if(!sforce.console.isInConsole())
    window.parent.location.replace('/'+recid);
  else
    sforce.console.getEnclosingPrimaryTabId(function(result) {
      callBackopenTab(result, recid, recname);
    });
}

function callBackopenTab(result, recordid, recordname) {
  sforce.console.openSubtab(result.id, '/'+recordid , true, recordname, null);
}; 

Note - I don't have a test rig for this, but it should work for you!

share|improve this answer
add comment

I did it finally using a global variable in JS. Not sure if its the best solution, but wanted to share with you guys and let me know if there is a better way. Now this becomes generic method for any record where i just pass in the recordid and name which reduces redundant code.

VF page

<A HREF="#" onClick="openTab('{!caserec.contactid}','{!caserec.contact.name}');return false">{!caserec.contact.name}</A>  

//globale JS variables
var recordid;
var recordname;

function openTab(recid, recname) {
     recordid = recid;
     recordname = recname;
  if(!sforce.console.isInConsole())
     window.parent.location.replace('/'+recid);
  else
     sforce.console.getEnclosingPrimaryTabId(callBackopenTab);
  }
function callBackopenTab(result) {
     sforce.console.openSubtab(result.id, '/'+recordid , true,recordname,null);
 }; 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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