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
Phill KempPhill Kemp 

Onclick javascript button setup

Hi,
I'm using Salesforce professional eddition and i'm hopeing to create a onclick button on my oppertunities layout which will direct me to a visual force page/url based on a field on the current oppertunities.

I had a quick look and there was a option under behaviour to select the visual force page, however this does not allow a if or case statement to navigate to the right visual force page based on a field/value.

After searching online i found some javascript code which had worked for someone else, however it does not appear to work for me and im wondering i'm making a school boy error.

The code is (field name's and visual force page name subsituted to example and examplefield)

var event = '{!EXAMPLEFIELD}';
if(event.length == 0){
     alert("No VisualForce page Exists, please contact your administrator");
}
else{

     window.location.href='/apex/EXAMPLE'
}
I was getting a error returned of "invalid token >"

I guess my question is where am i going wrong? Do i need to enable or call javascript librarys before my code? is this a limitation of my version of salesforce, or more likely a limitation of my knowlege?

Help would be very much appreciated.

Thanks
Phill
Best Answer chosen by Phill Kemp
Arunkumar RArunkumar R
Hi Phill,

    I have tried the below button code in my org. You can checkout the below code in your org.

Object: Opportunity
Lookup Field: AccountId
 
var event = '{!Opportunity.AccountId}'; 
if(event.length == 0){ 
alert("No VisualForce page Exists, please contact your administrator"); 
} 
else{ 

window.location.href = '/apex/ll'; 
}

This code works fine in my org. Can you try this in your org, but ensure button behaviour should be "Execute JavaScript".

Somewhere you were doing wrong, but i am unable to find. Try the above one if it's works change the actual requirement code like above.

All Answers

Arunkumar RArunkumar R
Hi Phil,

Please try the below one,
 
var event = '{!EXAMPLEFIELD}';
if(event.length == 0){
     alert("No VisualForce page Exists, please contact your administrator");
}
else{

     window.location.href = '/apex/EXAMPLE';
}

Thanks.
Phill KempPhill Kemp

Hi Arunkumar,

Thanks for your response. I've copied your code directly into by custom button setup, replacing the example field and example visual force but i'm now getting:

"A problem with the OnClick JavaScript for this button or link was encountered:
Unexpected identifier"

Any ideas?

Arunkumar RArunkumar R
Copy and Paste code might be a problem. I just added semicolon at the end of 7th line.

Single quote and doubles quotes might be a problem.  so again try to write instead of copy.
Phill KempPhill Kemp

Hi,

Here is my code as it appears in salesforce:

var event = '{!Opportunity.Event__c}';

if(event.length == 0){

     alert("No VisualForce page exists for this event, please contact your administrator");

}

else{

 

     window.location.href = '/apex/Quote' ;

}

are you saying i should remove the double quotes or single quotes?

I'm still getting the error:

A problem with the OnClick JavaScript for this button or link was encountered:

Unexpected identifier

i cant work out where i'm going wrong?

Cheers
Phill

 

Arunkumar RArunkumar R
Hi Phill,

What is Event__c field data type ?
Phill KempPhill Kemp

Hi,

it's a lookup field of the text field "event name" in a custom object.

Cheers
Phill

Phill KempPhill Kemp
Hi,
I've changed the variable to just a value of 1 to test the rest of the code/syntax and everything works fine, it seems it must not like the variable being set as a looked up value? is this the case? if so, is there any way of getting the looked up value as text?

Cheers
Phill
Arunkumar RArunkumar R
Hi Phill,

    I have tried the below button code in my org. You can checkout the below code in your org.

Object: Opportunity
Lookup Field: AccountId
 
var event = '{!Opportunity.AccountId}'; 
if(event.length == 0){ 
alert("No VisualForce page Exists, please contact your administrator"); 
} 
else{ 

window.location.href = '/apex/ll'; 
}

This code works fine in my org. Can you try this in your org, but ensure button behaviour should be "Execute JavaScript".

Somewhere you were doing wrong, but i am unable to find. Try the above one if it's works change the actual requirement code like above.
This was selected as the best answer
Phill KempPhill Kemp

Hi,
Yes your code above works, so it must be to do with the type of data that is getting returned (as my Opportunity.Event__c is a lookup field)

I guess my next problem is how to get round that. 

Also, is there a way to get the visual force page to relate this? mine just goes to my visual force page but with no data, when what i want is values related to the opportunity?

Thanks for all you help this far! you've saved my much time.

Cheers
Phill

 

Phill KempPhill Kemp

Just an update for other people in the same boat.

You can specify the record id in a visual force page which was the next step I needed. This is a helpful read https://developer.salesforce.com/page/Building_Visualforce_Pages_Using_the_Standard_Controller

Also a huge tip for newbies like myself, is if you are have the button setup in one window and the oppertunites (or whatever screen the button is on ) in another window, DONT FORGET TO REFRESH THE PAGE. Having only worked with dynamicly updating databases I was scratching my head many times thinking why it was not working, when really it was and a browser refresh to update the cache was the only thing i needed to do.