0

I have a button that assigns a case back to the originator. When a user clicks on the button, they have no other choice but to go through with the action after they select OK on the JS alert. How do I add an option for the user to click Cancel and return to the record in the following code?

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

if ({!Case.OwnerId=Case.CreatedById}){ 
alert('You are already the owner of the case'); 
} else { 

var caseObj = new sforce.SObject("Case"); 
var result = sforce.connection.update([caseObj]); 

if (result[0].success=='false') { 
alert(result[0].errors.message); 
} else { 
//location.reload(true); 
alert ('This case will be assigned back to the creator. You will no longer have access to this case.'); 
window.location = '/500/o'; 
} 
}

1 Answer 1

3

You can use the confirm method to allow an option for them to abort before it happens.

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

if ({!Case.OwnerId=Case.CreatedById}) { 
  alert('You are already the owner of the case'); 
} else if( confirm('This case will be assigned back to the creator. You will no longer have access to this case. Are you sure?')) { 
  var caseObj = new sforce.SObject("Case"); 
  var result = sforce.connection.update([caseObj]); 

  if (result[0].success=='false') { 
    alert(result[0].errors.message); 
  } else { 
    window.location = '/500/o'; 
  } 
}
2
  • thanks so much. After I click OK, I get an error that says "ID not specified in an update call". Is there something I'm missing in the ID?
    – Paul
    Jan 24, 2017 at 17:05
  • nevermind! I added the ID and the createdbyId and it is now working. Thank you so much!
    – Paul
    Jan 24, 2017 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .