+ Start a Discussion
finalistfinalist 

Basic javascript error question: "missing ; before statement"

Hi all --

I am working on a s-control that will change an Opportunity Name based
upon fields within the opportunity.  It worked fine w/o any error checking,
but I don't want to assume that the (custom) fields that I'm using have been
populated (and if they aren't, it breaks).

The script shouldn't be too hard to parse; essentially it checks for values
in the three custom Opportunity fields From, To and Departure Date, and
then builds a new Opp name using the Account Name and the three fields.

The error 'looks' like it should be simple to fix, but I can't seem to find where
my missing piece is in my function (or its application).

Any and all thoughts are appreciated.

<script>
function change(){
   var newName = "";
   If ({!Opportunity.From__c} && {!Opportunity.To__c} && {!Opportunity.Departure_Date__c} )  {

      var newName = "{!Account.Name} -" + "{!Opportunity.From__c} -";
      // breaking into multiple lines for readability
      newName = newName +  "{!Opportunity.To__c}- " +    "{!Opportunity.Departure_Date__c}";
 
      // Open Opportunity Edit page & replace Opp. Name with newName
      var url = '{!URLFOR($Action.Opportunity.Edit, Opportunity.Id)}';
      window.parent.location = url + "&opp3=" + newName + "&save=1";
     
      }
      else {        // at least one of the custom fields is empty or undefined

             // pop-up alert message to indicate the problem
             notify();
             }
      }
        
      function notify() {
          Alert("From, To, and/or Departure Date is not null.  Opportunity Name will not be changed");
      }
    </script>

The reference to the function looks like this:

Opp Name: <a href="javascript: change();">Account - From ! To - Departs</a>

When I look at it, there's a semicolon after every line unless it's the beginning or end
of a function, IF or ELSE.  Does anyone see anything different?

Thanks!
finalistfinalist
The error that appears when the Opportunity Detail page loads is this:

missing ; before statement
If (from != "") {\n

That appears to be a straightforward error, but I'm thinking that it's hiding a more sinister one in my script..?

This is the current version of my script, employing 3 custom fields:

 <script>
         function change() {

              var from = "{!Opportunity.From__c}";
              var to = "{!Opportunity.To__c}";
              var departs = "{! Opportunity.Departure_Date__c }";

              If (from != "") {
                       If (to != "") {
                          If (departs != "")  {

                           var newName = "{!Account.Name} -" + from + " - " + to + " - " + departs;

                           // Open Opportunity Edit page & replace Opp. Name with newName
                           var url = '{!URLFOR($Action.Opportunity.Edit, Opportunity.Id)}';
                           window.parent.location = url + "&opp3=" + newName + "&save=1";
                      } // end If ( departs...

                     else {
                          // otherwise, pop-up alert message to indicate the problem
                          notify();
                           }
                   } // end If ( to...

                  else {
                            notify();
                            }
                } // end If ( from ...

             else {
                       notify();
              }
 
         } // end function

  </script>
  <script>

        function notify() {
            alert("From, To, or Departure Date is null.  Opportunity Name will not be changed");
        }

    </script>

And here is the implementation:

<a href="javascript: change();">Opp Name: Account - From - To - Departs</a>

cheenathcheenath
If (from != "") {

If must be lower case if:



finalistfinalist
cheenath, you are correct!  (I win!)

It was that sneaky little syntax error -- well, not little enough to not matter, but little nevertheless.

Thanks much - -