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
Kalyan123Kalyan123 

How to add custom OnLoad javascript functionality to standard Contact Forms

Problem:

- I have added one Custom Button 'My Button" to the contact's standard forms.

- When I open any contact, based on the Value of Case_ID (which is also custom field to contact entity), I want to show/Hide the "My Button". How can I achieve this?

 

Options Tried:

 

So far I have tried the below optons. But no success.

Option 1: Thought of adding OnLoad event and based on the field can hide/show the button. But I couldn't find how to add OnLoad functionality to standard Contact form

Option 2: Created dummy Visual Force Page/ S-Control added OnLoad javascript event, in that onLoad function tried to access the Contact forms  "My Button". But not getting access.

 

S-Control code looks like below:

 

<apex:page standardController="Contact">
  
    <script type="text/javascript" src="/js/functions.js"></script>
    <script src="/soap/ajax/11.1/connection.js"></script>
   
<body onload="javascript&colon;init();">       
</body>
 <script language="javascript">
    window.onload = new function() {
        alert('1234567');
        var CaseId = '{!Contact.case_id__c}';
        alert('CaseId: ' + CaseId);
        var inputs = document.getElementsByTagName("input");
        var nameStr = "";
        for(var i=0; i<inputs.length; i++)
        {
            nameStr += ' - ' + inputs[0].name;
        }
        alert(inputs.length + ' - ' + nameStr);
       
        //var btnCopy = window.parent.document.getElementsByName("My_Button");
        //alert(btnCopy.length);
     };
   
 </script>

</apex:page>

 

Please help me on this. Thanks in advance. Is this approach Correct? Guide me to the correct approach to get it work.

Message Edited by Kalyan123 on 09-23-2009 08:23 PM
Message Edited by Kalyan123 on 09-23-2009 08:26 PM
Best Answer chosen by Admin (Salesforce Developers) 
Kalyan123Kalyan123

Thanks for your response. The script is running when OnLoad.

 

There is a bug in the javascript code. I have corrected that and is working good.

 

Here is the correct code, to access custom buttons and hide them conditionally based on other fields.

Below is code for S-Control. I think it should work with Apex page also. I didn't try.

 

<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<script language="javascript"  type="text/javascript">
    window.onload = new function() {
        var caseId = '{!Contact.case_id__c}';
        var viewCaseBtns = window.parent.document.getElementsByName("view_case");
        if(caseId == null || caseId.trim().length==0)
        {
            for(var i = 0; i < viewCaseBtns.length; i++)
            {
                viewCaseBtns[i].style.display = 'none';
            }
        }
     };
</script>
<body >


</body>

</html>

All Answers

bob_buzzardbob_buzzard

Need a bit more information here.  When you say in option 2 that you had no access, did your onload handler run but no do what you expected, run and throw an error, not run?

Kalyan123Kalyan123

Thanks for your response. The script is running when OnLoad.

 

There is a bug in the javascript code. I have corrected that and is working good.

 

Here is the correct code, to access custom buttons and hide them conditionally based on other fields.

Below is code for S-Control. I think it should work with Apex page also. I didn't try.

 

<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<script language="javascript"  type="text/javascript">
    window.onload = new function() {
        var caseId = '{!Contact.case_id__c}';
        var viewCaseBtns = window.parent.document.getElementsByName("view_case");
        if(caseId == null || caseId.trim().length==0)
        {
            for(var i = 0; i < viewCaseBtns.length; i++)
            {
                viewCaseBtns[i].style.display = 'none';
            }
        }
     };
</script>
<body >


</body>

</html>

This was selected as the best answer
ThomasTTThomasTT

Wow... I didn't know we can do this... but, unfortunately, this can be done by S-Control.

 

S-Control is running on the same domain as the detail page does.

However, VF Pages running on the different domain. It cause this error

 

Error: Permission denied for <https://c.na6.visual.force.com> to get property Window.document from <https://na6.salesforce.com>.

It's Cross Site Scripting. This is one reason why we still need S-Control after 2010...

 

ThomasTT

Kalyan123Kalyan123

Exactly. I knoe S-Cotrols are going off. I tried on APex:page and it's giving the security exception you told. It should not this much dificult to control the fields or buttons conditionally.

 

How long S-Controls are supported by Sales force? 

 

I am glad if anybody knows the better solution to this, please let us know.

 

Thanks,

Kalyan Anne.

ThomasTTThomasTT

Hmm, function-wise, you're right. There should be some control from VF page to main page.

However, generally, controlling parent document from child document is treated as CSS and that is reasonable enought to treat it as a security breach. I heard that that's the reason why they moved VF pages to different domain. They wanted to protect main function from adhoc functions.That makes sense.

 

I guess they left S-Controls because it was already decided to be gone.

 

There should be another way to control parent document, like apex:component/apex:attribute or proxy.

 

and, I guess this is how we live in others' house.

 

ThomasTT

Kalyan123Kalyan123
Thank you Thomas. I will try to use apex: component etc. I will post, If I find anything. As a Java Programmer and newbie to Saleforce customization as per my project integrating to SF, litttlebit difficult to all these terms. :)
ThomasTTThomasTT

Sorry,

 

> There should be another way to control parent document, like apex:component/apex:attribute or proxy.

 

is just my murmuring. I should have post it in ideas. Forget about it.

 

Good luck!

 

ThomasTT

MissouriAdminMissouriAdmin

Get the S-control to do this, but where do you run the S-Control from?  All S-Control action that i have used is the result of pushing a button.  How do you automatically run this for standard Salesforce.  I was able to do this for Partner Portal by putting the code in the Footer of the Portal Page.

 

Additionally, when doing these Javascript pieces, has anyone had language problems and non-english pages failing to run the code, or the language functions running after the code and undoing these javascript pieces?

Kalyan123Kalyan123

Written OnLoad Javascript function to the SControl. Then added this SControl to the standard SF contact form.

When the contact form opened, this s-control will be loaded as a result onLoad function of Scontrol will be executed, which in turn to show/hide my custom button  based on the custom field.

 

Regarding the language, I didn't try.

MissouriAdminMissouriAdmin

"Then added this SControl to the standard SF contact form"

 

I am not aware where to add an Scontrol to the Standard SF Contact Form.  IS this building an Apex page, put this code in it, then just use apex:detail to get the rest of the page?  Or is there a more straight forward way to add "Scontrol to the standard SF contact form".

 

Thanks for your help on this.