function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Using the Javascript Tolkit to access Apex property in Page?

Hi - I'm stuck and maybe this will help me out. 


Here's the setup:  I'm working on a solution where there is a related list off of the Case object.  This related object is a mater detail to Case.  I've overridden both the New and Edit with VF pages.


The rub here is that the VF page needs to honor the parent case's record type, to then show the correct VF page which all just differ in layout only (they share the same extension).  Ok, so what I have is just Javascript in a "Gateway" page that then redirects to the proper VF page for input.


Ok, so here's a snippet of the Gateway page:


apex:form > <script type="text/JavaScript" src="/js/functions.js"></script> <script src="/dJS/en/1196898152000/library.js" type="text/javascript"></script> <script src="/soap/ajax/17.0/connection.js" type="text/JavaScript"></script> <script> sforce.connection.sessionId = '{!$Api.Session_ID}'; var caseId = '{!NewObj__c.Case__c}'; var caseNumber = ''; var website = ""; if ('//na3.salesforce') == -1) { website=""; } /* REMOVED THE SOQL CALLS FOR THE SAKE OF SPEEDING UP THE PAGE LOAD CONFIG THE REC TYPE IDS FOR PRODUCTION WHEN RELEASING */ var bvipRecordTypeId = 'xxx'; var clientSolutionRecordTypeIds = 'xxx,xxx,xx,xx'; var marketingRecordTypeIds = 'xxx,xx,xx,xx'; var caseRecordTypeId = findCaseRecordTypeId(); var caseFieldId = 'CF00N50000001or5d'; if (caseRecordTypeId == '' ) alert('Error: Record Type not found, please contact support'); function findCaseRecordTypeId() { var recordType = sforce.connection.query("Select CaseNumber, RecordTypeId from Case where Id = '" + caseId + "'"); var rit = new sforce.QueryResultIterator(recordType); while(rit.hasNext()) { var r =; caseNumber = r.CaseNumber; return r.RecordTypeId; } return ''; } if(bvipRecordTypeId.indexOf(caseRecordTypeId) >= 0){ // BVIP Case window.parent.location.href = website + '/apex/BVIPOverride?' + caseFieldId + '=' + caseNumber + '&' + caseFieldId + '_lkid=' + caseId + '&scontrolCaching=1&retURL=%2F' + caseId + '&sfdc.override=1'; } else if(clientSolutionRecordTypeIds.indexOf(caseRecordTypeId) >= 0) { // CS Case window.parent.location.href = website + '/apex/CSOverride?' + caseFieldId + '=' + caseNumber + '&' + caseFieldId + '_lkid=' + caseId + '&scontrolCaching=1&retURL=%2F' + caseId + '&sfdc.override=1'; } else if(marketingRecordTypeIds .indexOf(caseRecordTypeId) >= 0) { // Marketing Case window.parent.location.href = website + '/apex/MarketingOverride?' + caseFieldId + '=' + caseNumber + '&' + caseFieldId + '_lkid=' + caseId + '&scontrolCaching=1&retURL=%2F' + caseId + '&sfdc.override=1'; } else { alert('Error: Record Type not found, please contact support'); } </script> </apex:form>

 You'll see that I'm calling a SOQL query to see what the parent case's record type is, then using that Id in some logic. 


This works.  But the problem is that with this page rendering, the SOQL call, and then the redirect to the other page which renders, it'll take 5-6 seconds for the final page to render.  Not acceptable.


All of this would be solved if I could do something like {!NewObj.Case__r.RecordTypeId} but that always returns a blank string, so I guess that's out in the JS API.  


Is it possible to create a property to hold the record type Id in the Controller Extension (class) an then access that property via JS?  Or would I be better off to create a Component for all of this with an exposed property and then wire the page up to the Component?  I'm not 100% at all on the architecture of VF pages, but this seems so simple yet my current solution is a hack at best, and I'd like to get this to load in like 2 seconds.


Any pointers here would be most welcome.





Why don't you create a formula field on your object that refers case.recordTypeId?



I would skip using a gateway and put both layouts into 1 page (in components or not.. what ever blows your skirt up), and then load the appropriate layout based on the the recordType found in the controller constructor. This way you have no redirect at all.




Thanks for the replies.  I like the thought about loading the layout per record type so I'll see what it'll take for that.  Currently I just created a lame bypass since the one user group that needs this page to load really fast all use the same if the profile is 'XXX' then just go to the appropriate page and skip seeing what the case's recordy type is.


Thanks again for that idea.