-5

I am using Javascript code in Custom link to open a URL or display Error message based on the condition.

Behavior= Execute Javascript
Content Source= Onclick javascript

if('{object__c.field1__c}' <> 'Z0' || '{object__c.field1__c}' <> 'Z4')
{
window.alert("Error Message");
}
else
{
window.open("https://xxxx.net/parmeter1={!object__c.field2}&parameter2= 
{!object__c.field3}");
}

But, It is not working. Can anyone give suggestion please.

The following error is coming when I click on the custom link:

Error:

enter image description here

2
  • 2
    What is not working? Jun 20, 2018 at 13:15
  • I am getting error. I have added in question. Please check.
    – Ravindar D
    Jun 20, 2018 at 14:39

1 Answer 1

2

JavaScript does not use <> to mean "not equals." Instead, you could use !=, or you can even just use a formula. Also, make sure your merge fields are also in the {! ... } format. You're also confusing || with &&. With ||, if field1__c is Z0, then it will not be Z4; in other words, it would always evaluate to true! Here's a modified version of your code:

// Using merge formula!
if({!Object__c.Field1__c<>'Z0'&&Object__c.Field1__c<>'Z4'}) {
  alert('Error Message');
} else {
  window.open("https://xxxx.net/parmeter1={!object__c.field2}&parameter2={!object__c.field3}");
}

If you were not using a merge formula, it'd look like this:

if('{!Object__c.Field1__c}'!='Z0'&&'{!Object__c.Field1__c}'!='Z4') {

Note that this method is definitely not recommended; it's way too easy to make mistakes, and is definitely less readable than a simple formula.

Not the answer you're looking for? Browse other questions tagged .