You need to sign in to do that
Don't have an account?
Peter 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
Anyone have any thoughts on why that wouldn't update the user record? I have the ability to manually update it.
{!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.
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
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>