1

Can somebody help me with this code I am trying to change the value if the Validated__c field to true whenever the button is clicked.

Class Code:

global class checkbox{
    webservice static void check(Id localId) { 
        Account acc = new Account();
        system.debug(localId);
        system.debug('localId');
        acc.Id = localId ;
        acc.Validated__c = true;
        update acc;
    }
}

Button Code:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
alert("This is it!");
if({!Account.Validated__c}=true)
{
    sforce.apex.execute("checkbox","check",{localId:"{!Account.Id}"});   
    alert("This is it!");
}
  • whats the error message? – StartingAgain Sep 3 '15 at 19:07
  • And when I am trying to run this class via developer console like this: checkbox.check(00128000002kYzm); (Passing some account's ID) It is showing error: Line: 1, Column: 26 expecting a right parentheses, found 'kYzm' – happy Sep 3 '15 at 19:09
  • If I've understood correctly then you want to mark Validated__c to true on click of button, right? Then why are you checking Account.Validated__c = true ?? (BTW, condition is also wrong, there should be one extra = if you want to check) – Abbas Sep 3 '15 at 19:16
  • Yeah you got it right. – happy Sep 3 '15 at 19:17
  • And btw if I am trying like: acc.Validated__c == true; Error: Compile Error: Expression cannot be a statement at line 7 column 17 – happy Sep 3 '15 at 19:17
1

I think the syntax around the execute statement is incorrect, I think you need to use single quotes here as it is being interpreted by Salesforce still and not JavaScript:

var retStr; // good to catch a return value, if any (I dont remember)
sforce.apex.execute("checkbox","check",{localId:'{!Account.Id}'}); //note single quotes, not double quotes in final parameter
| improve this answer | |
1

Instead of calling a apex class from javacscript using AJAX toolkit, you could do the update in AJAX toolkit itself like below

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
var newRecords = [];
var A = new sforce.SObject("Account"); 
A.id ="{!Account.Id}"; 
A.Validated__c = 'true';
newRecords.push(A); 
var result = sforce.connection.update(newRecords);

Hope it helps.

| improve this answer | |

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.