• Aishwary Gupta 12
  • 0 Points
  • Member since 2017

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1
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