1

I have a button called "Assign Back to Originator". This button is used when a rep is done working on a case and needs to assign it back to the creator of the case. The button will then assign the creator of the case as the owner. How can I throw an error of "You are already the case owner" when the creator ID and the owner ID is the same when they try to click the "Assign Back to Originator" button?

This is the code I have so far:

{!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"); 
caseObj.Id = '{!Case.Id}'; 
caseObj.OwnerId = '{!Case.CreatedById}'; 
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'; 
} 
}
  • Do you already having a Validation Rule enforcing this requirement and just want to surface the error? Also updated your API Version from 24.0! – Adrian Larson Jan 23 '17 at 18:07
  • You can refer my blog: santanuboral.wordpress.com/2017/01/23/… – Santanu Boral Jan 23 '17 at 18:07
  • You could get the logic to update case with owner and to throw an error referring my blog post – Santanu Boral Jan 23 '17 at 18:08
  • great blog! i'll be referring to this a lot while i learn more programming for sure. thanks! – Paul Jan 23 '17 at 18:16
0

Your logic will look like this:

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
var caseObj = new sforce.SObject("Case");
var previousOwner = "{!Case.CreatedById}";
var currentOwner = "{!$User.Id}";
caseObj.Id = "{!Case.Id}";
caseObj.OwnerId = "{!$User.Id}";

if(previousOwner == currentOwner)
{       
  alert("You are already the Case Owner ");     
}else
{   
  var result = sforce.connection.update([caseObj]); 
  alert ("This case will be assigned back to the creator.");    
  location.reload();
}
  • Santanu - as of right now, when this button is clicked, the person that clicked it has no other choice but to acknowledge that the case will be assigned back to the owner. How do I add a cancel button to the alert so the rep can go back to the page if accidentally clicked? – Paul Jan 24 '17 at 16:48
2

You can just write:

if({!case.OwnerId=case.CreatedById}) {
   alert("You already own the case!");
} else {
   // do your update logic
}
  • @SantanuBoral No, it's not a typo. You only need a single equals sign in a merge formula. Try it out sometime. – sfdcfox Jan 23 '17 at 18:15
  • I will try that – Santanu Boral Jan 23 '17 at 18:16
  • this worked like a charm. thank you very much. i'm learning a lot through you guys – Paul Jan 23 '17 at 18:16
  • @sfdcfox as of right now, when this button is clicked, the person that clicked it has no other choice but to acknowledge that the case will be assigned back to the owner. How do I add a cancel button to the alert so the rep can go back to the page if accidentally clicked? – Paul Jan 24 '17 at 16:49
  • @Paul } else if(confirm('Are you sure you want to return the case to the original owner?')) { /* update logic here */ – sfdcfox Jan 24 '17 at 16:56

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.