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
Peter SchultzPeter Schultz 

OnClick Javascript to update user records

we have a limited number of licenses in our full sandbox for service cloud and are hesitant to refresh in any short period of time. I created a simple javascript that would just take away who ever was testing with the license but the update does not seem to work. it queries correctly, just doesnt update
 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
var ChangedUser = new sforce.SObject("User");
var CurrentUser = new sforce.SObject("User");

result = sforce.connection.query("select id,name from User where userpermissionssupportuser =true and isactive=true limit 1");
records = result.getArray("records");
if(records[0])
{
 for (var i=0; i< records.length; i++) {
    var record = records[i];
  if(confirm(record.Name +" has the license, do you really want to mess with them?")){
     ChangedUser.UserPermissionsSupportUser=false;
     ChangedUser.ID=record.id;
 try{    sforce.connection.update([ChangedUser]);
  }catch(er) {
alert(er);
}  
}
  }
}
window.location.reload();

Anyone have any thoughts on why that wouldn't update the user record?  I have the ability to manually update it.
Best Answer chosen by Peter Schultz
NekosanNekosan
I think problem was with using 'id' instead or 'Id'.
I tried this code in my org and its working.
Also instead of try/catch try to get result and parse to it to print those errors. 

<apex:page>
   <script src="/soap/ajax/29.0/connection.js" type="text/javascript"></script>
    <script src="/soap/ajax/29.0/apex.js" type="text/javascript"></script>
    <script>
      function update(){
        sforce.connection.sessionId = "{!$Api.Session_ID}";
      var ChangedUser = new sforce.SObject("User");
        var CurrentUser = new sforce.SObject("User");

        result = sforce.connection.query("select id,name from User  limit 1");
        console.log('>>>>>' +result )
        records = result.getArray("records");
        if(records[0])
        {
         for (var i=0; i< records.length; i++) {
            var record = records[i];
            console.log(record.Id);
          if(confirm(record.Name +" has the license, do you really want to mess with them?")){
             ChangedUser.title='CEO';
             ChangedUser.id=record.Id;
          result = sforce.connection.update([ChangedUser]);
                   
                    if (!result[0].getBoolean("success")) {
                       alert("error");
                       console.log(result);
                     }
       
          }
        }
      }

}
update();
</script>


</apex:page>

All Answers

NekosanNekosan
are you getting any error in catch?Also did you check browser's console logs for any errors?
Peter SchultzPeter Schultz
Thanks for responding,   no luck on errors on catch and  browser console logs show no errors with that javascript
NekosanNekosan
I think problem was with using 'id' instead or 'Id'.
I tried this code in my org and its working.
Also instead of try/catch try to get result and parse to it to print those errors. 

<apex:page>
   <script src="/soap/ajax/29.0/connection.js" type="text/javascript"></script>
    <script src="/soap/ajax/29.0/apex.js" type="text/javascript"></script>
    <script>
      function update(){
        sforce.connection.sessionId = "{!$Api.Session_ID}";
      var ChangedUser = new sforce.SObject("User");
        var CurrentUser = new sforce.SObject("User");

        result = sforce.connection.query("select id,name from User  limit 1");
        console.log('>>>>>' +result )
        records = result.getArray("records");
        if(records[0])
        {
         for (var i=0; i< records.length; i++) {
            var record = records[i];
            console.log(record.Id);
          if(confirm(record.Name +" has the license, do you really want to mess with them?")){
             ChangedUser.title='CEO';
             ChangedUser.id=record.Id;
          result = sforce.connection.update([ChangedUser]);
                   
                    if (!result[0].getBoolean("success")) {
                       alert("error");
                       console.log(result);
                     }
       
          }
        }
      }

}
update();
</script>


</apex:page>
This was selected as the best answer
Peter SchultzPeter Schultz
Thanks! that fixed it. I swear i spent around 2 hours debugging that....