+ Start a Discussion
Rob SeniorRob Senior 

Replacing hyperlink with javascript functionality

I am a 1st time poster so apologies if this is the wrong place for this question.
Also I am not that experienced with VF and Apex
Some work was done before I started with the company that uses a Hyperlink formula field with JavaScript in it.  Now Salesforce is stopping this and I am struggling to find a way to replace this functionality.
This is what the current functionality does:

On the case object there is a related list (linking to a custom object).  The Related list shows the "Add Coment" field which is the formula field in question

Related List on the Case object

When you click on the “New Comment” hyperlink it opens a new screen (with some fields pre-populated) that allows a new Case Comment record to be generated that is linked against the specific Journal Entry (and against that specific case):

Screenshot of when the hyperlink is clicked

This is accomplished by the formula/JavaScript building a URL (using multiple custom settings so hardcoding is not required).
The formula in question:
HYPERLINK("javascript:if(typeof(srcUp)=='function') {srcUp('/" & $Setup.CaseManagement__c.CaseCommentObject__c & "/e?" & $Setup.CaseManagement__c.CaseCommentJournalIDField__c & "=" & Name & "&" & $Setup.CaseManagement__c.CaseCommentJournalIDField__c & "_lkid=" & Id & "&" & $Setup.CaseManagement__c.CaseCommentTypeFieldID__c & "=Comment&retURL=%2F" & $Setup.CaseManagement__c.CaseCommentObject__c & "%2Fo&" & $Setup.CaseManagement__c.CaseCommentCaseIDField__c & "_lkid=" & Case__c & "&" & $Setup.CaseManagement__c.CaseCommentCaseIDField__c & "=" & Case__r.CaseNumber & "&isdtp=vw');}"+ " else {window.location.href='/" & $Setup.CaseManagement__c.CaseCommentObject__c & "/e?" & $Setup.CaseManagement__c.CaseCommentJournalIDField__c & "=" & Name & "&" & $Setup.CaseManagement__c.CaseCommentJournalIDField__c & "_lkid=" & Id & "&" & $Setup.CaseManagement__c.CaseCommentTypeFieldID__c & "=Comment&retURL=%2F" & $Setup.CaseManagement__c.CaseCommentObject__c & "%2Fo&" & $Setup.CaseManagement__c.CaseCommentCaseIDField__c & "_lkid=" & Case__c & "&" & $Setup.CaseManagement__c.CaseCommentCaseIDField__c & "=" & Case__r.CaseNumber& "'}", "New Comment", "_self" )

My initial thought was to replace this with a Visual Force page and an Apex controller – so the formula field would be:
HYPERLINK("apex/Journal_Entry_Add_Comment","New Comment")
The VF page code would be very basic:
<apex:page controller="Journal_Entry_Add_Comment_Controller">
    <apex:outputLink >value= "!{URL}"</apex:outputLink>
Then the URL string would be created in the Apex controller.
public class Journal_Entry_Add_Comment_Controller {
	public Journal_Entry_Add_Comment_Controller()
		CaseManagement__c CS = CaseManagement__c.getOrgDefaults();      // loading custom settings so field values can be used
		id Jid = apexpages.currentPage().getparameters().get('id'); 	// get the id of the journal entry record id 
system.debug('PAGE ID = ' + Jid);        

        string name = apexpages.currentPage().getparameters().get('name');  
system.debug('NAME = ' + name);        

        Journal_Time__c JTrecord = [SELECT id, name, case__c, case__r.CaseNumber 
                                    FROM Journal_Time__c 
                                    WHERE id = :Jid]; 					// loads the journal time record field data into memory
		string URL;        // URL to be constructed 
		URL = '/' + CS.CaseCommentObject__c + '/e?' + CS.CaseCommentJournalIDField__c + '=' + JTrecord.Name + '&' + CS.CaseCommentJournalIDField__c +
            '_lkid=' + Jid + '&' + cs.CaseCommentTypeFieldID__c + '=Comment&retURL=%2F' + cs.CaseCommentObject__c + '%2Fo&' + cs.CaseCommentCaseIDField__c +
            '_lkid=' + JTrecord.Case__c + '&' + cs.CaseCommentCaseIDField__c + '=' + JTrecord.case__r.CaseNumber + '&isdtp=vw'; 
system.debug('URL = ' + URL);        


However the following returns a null value
string name = apexpages.currentPage().getparameters().get('name');
id Jid = apexpages.currentPage().getparameters().get('id');

So when the SOQL runs looking for records with that ID - it kicks a runtime error.

So I thought to try and re-create the javascript in the Visual Force page in <Script> tags – however the parameters from the custom settings are not picked up (IE the URL contains “$Setup.CaseManagement__c.CaseCommentObject__c” rather than “a0s” which is what it should do.)
I have never written any JavaScript – so I can’t break down the URL and create functions to recreate it like I tried to do in Apex – but I guess this is an option.  
I have thought about replacing the related list altogether with an inline Visual Force page.  This would recreate the look of the related list but would use a command button (rather than hyperlink) in the second column to create the new records. 
I have passed parameters using buttons before – so I think this would be doable. 

However I have read the cons against replacing the standard related list with a Visual Force made related list and would prefer not to go down that route.

Does anybody have advice on how they would tackle this?
(Or maybe steer me in the correct direction on my Apex controller)

Thanks for any help/advice

Rob SeniorRob Senior
1st screenshot looks terible here is an attempt to clear it up a bit:

User-added image
Aishwary Gupta 12Aishwary Gupta 12

Hi Rob,

Did you get the solution for this ?

I also came across similar issue.




Rob SeniorRob Senior
Hi Aishwary,
I logged a call with Salesforce and they advised me to create an embeded Visual Force page to replace the standard related list.

So I created a VF page that replicated the related list functionality using "Pageblock table" functionality and then for the "New Comment" link - I recreated the URL hack that the javascript was doing -

my VF code:

<apex:page standardcontroller="case"  extensions="CaseRelatedListExtension">
  <apex:form >
  <apex:pageBlock title="Journal Entries" mode="new">
    <apex:commandButton value="Case Journal Time Entry" onclick="window.open('/a0t/e?CF00NE0000004tV02={!casNo}&CF00NE0000005SGUx={!casNam}');" />
    <apex:pageBlockTable title="Journal Entries" value="{!linkedJournalList}" var="index" columns="8" style="width:100%">
      <apex:column headerValue="Journal Time ID" >
        <apex:outputLink value="/{!index.Id}" target="_blank">{!index.Name}</apex:outputLink>     
      <apex:column headerValue="Add Comment">
        <apex:outputText ><a href='/a0s/e?CF00NE0000004tV01={!casNo}&CF00NE0000004tV03={!index.name}' target="_blank" >  New Comment</a></apex:outputText>
      <apex:column headerValue="Created By" value="{!index.CreatedBy.Name}, {!index.CreatedDate}"></apex:column>
      <apex:column headerValue="Talk-Labor Hours" value="{!index.Talk_Labor_Hours__c}"></apex:column>
      <apex:column headerValue="Research-Travel Hours" value="{!index.Research_Travel_Hours__c}"></apex:column>
      <apex:column headerValue="Indirect Hours" value="{!index.Indirect_Hours__c}"></apex:column>
      <apex:column headerValue="Overtime Hours" value="{!index.Overtime_Hours__c}"></apex:column>
      <apex:column headerValue="Chargeable Hours" value="{!index.Chargeable_Hours__c}"></apex:column>

The extension class just searches for Journal Entry records that are linked to the case and puts them in a list (which is then used by the VF page)

Hope this helps,

Ross Gilbert 31Ross Gilbert 31
Hey Rob can you post your extension class?  Thanks
Rob SeniorRob Senior
Hi Ross,

here is the extender class:

public class CaseRelatedListExtension {
 public String currentRecordId {get;set;}
    public String casNo {get;set;}
    public String casNam {get;set;}
    public List<Journal_Time__c> linkedJournalList {get;set;}

 public CaseRelatedListExtension(ApexPages.StandardController controller) {
  // gets the case ID from the VF page
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        // get case details to pass into VF page 
        List<Case> caseList = new List<Case>();
        caseList = [SELECT casenumber, createdby.Name FROM CASE WHERE ID = :currentRecordId];       
        casNo = caseList[0].casenumber;  // case number to be used when creating new journal time records
        casNam = caseList[0].createdby.Name; // technician name to be used when creating a new journal time record

        // get the list of all linked journal time entries - so can be displayed in the table (like related list)
        linkedJournalList = [SELECT Id, Name, CreatedBy.Name, Talk_Labor_Hours__c, Research_Travel_Hours__c, Indirect_Hours__c, Overtime_Hours__c, Chargeable_Hours__c, createdDate // Add_Comment__c,
                            FROM Journal_Time__c
                            WHERE Case__c = :currentRecordId];        

So the extender class gets the ID of the case (when the case is opened with the VF added to page layout as inline VF)
It runs a query to find all journal time records attached to the case.
It passes that list to the VF page to use in the table.

It also captures 2 other variables - the case number and the user who created the case
These variables are then used in the "new journal time" button code (replication of the standard "related list" functionality) and also used in the hyperlink for "new comment"

Both the hyperlink and button both use URL hacks to go to the edit screen for the relevent records and use the variables to pre-populate the data.
I used this technique as it was already in our system so it was quick to get the details - but if possible dont use the URL hacking as Salesforce doesnt support them anymore (or wont in lightning) - it just bought me enough time to try and figure out a better way to do it.