0

I am a novice to creating onclick javascript buttons so please forgive my ignorance. I am creating a button that will flag the field SOW_Needed__c on the custom object Project_Request_Form__c. I was able to get this code from forums however I cannot get it to work. I get an error "Unexpected Identifier Error". I have tried to sift through the many posts about this same issue however I simply cannot understand why it doesn't work. Here is my code below, any help is appreciated.

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}

     if({!Project_Request_Form__c.SOW_Needed__c} == false)
     {
          var c = new sforce.SObject("Project_Request_Form__c");
          c.id = "{!Project_Request_Form__c.Id}";
          c.SOW_Needed__c = true;
          result = sforce.connection.update([c]);
          if ( result[0].getBoolean( "success" ) )
          {
               window.location.reload();
          }
          else
          {
               alert( result[0].errors.message);
          }
     }
     else
     {
          alert("Already True");
     }
  • I think c.id must be c.Id JavaScript case sensitive right. – Safiya PV Apr 1 '17 at 18:02
  • if you are getting this error-> "Uncaught Syntax Error: Unexpected token ==" then try using quotes in your If statement as Eric's code. '{!Project_Request_Form__c.SOW_Needed__c}' . – Safiya PV Apr 1 '17 at 19:25
2

I could not find a specific issue with your code. I copied and pasted into my org and it works as expected....

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}

     if('{!Account.Active__c}' != 'Yes')
     {
          var c = new sforce.SObject("Account");
          c.id = "{!Account.Id}";
          c.Active__c = true;
          result = sforce.connection.update([c]);
          if ( result[0].getBoolean( "success" ) )
          {
               window.location.reload();
          }
          else
          {
               alert( result[0].errors.message);
          }

     }
     else
     {
          alert("Already True");
     }

I did not make any modification except to change the Object and field name to one in my org. Note Active__c is a picklist in this case not a boolean

I even left the lowercase id and the mixed " and ' as well as the non declared var result

Maybe try with the above using standard fields then verify it works. Once you have it working with standard fields, start adding in your custom fields and see if it breaks.

Another way is to remove all lines of code and add them back in one by one until they break. Once they break, alert the value just before and you should be able to see what is going on.

While not an ideal way to test, the code is short enough that it is completely feasible.

  • i think error is at his IF statment. he didnot use quotes if({!Project_Request_Form__c.SOW_Needed__c} == false) . – Safiya PV Apr 1 '17 at 19:08
  • You don't need to for a Boolean – Eric Apr 1 '17 at 19:08
  • {!Project_Request_Form__c.SOW_Needed__c} im talikng about this – Safiya PV Apr 1 '17 at 19:09
  • 1
    Yea, it evaluates to a Boolean – Eric Apr 1 '17 at 19:10
  • 1
    When doing comparisons, I usually prefer to evaluate inside the merge field. In your example, if( {!IsPickVal(Account.Active__c, 'Yes')} ) is how I'd write it; it will evaluate to either if( true ) or if( false ). Not that there's anything wrong with your way, I just thing it's easier to read. – sfdcfox Apr 2 '17 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.