+ Start a Discussion
AddaAdda 

Javascript Owner Change prevention

Hi,

I have a javascript which gets the next case assigned to user who is logged in. If the case owner is a queue, by clicking the get next case, the case is assigned to that user. What I want is to prevent the user to change the owner from queue to him/her if the condition says the user is out of office = True. Here is my code. I don't how to insert the ischange(owner) function in javascript.
{!REQUIRESCRIPT("/soap/ajax/18.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/18.0/apex.js")} 
{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/support/console/30.0/integration.js")} 

var id = sforce.apex.execute("RetrieveNextUtils","retrieveNextCase",{userId:""}); 

// Prevent the user to change the owner from queue to him/her if the condition says the user is out of office = True//
if ( '{!Case.Agent_Out_of_Office__c}' == true ){
   alert ('Next case cannot be assigned because you are marked as out of office.');
     }

else if (id!='') { 
var querystringParams = ""; 
if (window.location.href.indexOf("isdtp=mn")!=-1) { 
querystringParams = "?isdtp=mn"; 
} else if (window.location.href.indexOf("isdtp=vw")!=-1) { 
querystringParams = "?isdtp=vw"; 
} 
//We have successfully retrieved a case 
if (sforce.console.isInConsole()) { 
sforce.console.openPrimaryTab(null, '/'+id + querystringParams, true, null, null, null);
} else { 
navigateToUrl('/'+id + querystringParams);
}
} else { 
alert('No cases are available at this time.'); 
}

Can anyone help please?
Best Answer chosen by Adda
Sumitkumar_ShingaviSumitkumar_Shingavi
Below code should work for you. Try this out:
{!REQUIRESCRIPT("/soap/ajax/18.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/18.0/apex.js")} 
{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/support/console/30.0/integration.js")} 

if (('{!User.Out_of_Office2__c}' == true)){
   alert ('Next case cannot be assigned because you are marked as out of office.');
   location.reload(true);
} else {
	var id = sforce.apex.execute("RetrieveNextUtils","retrieveNextCase",{userId:""}); 
	
	if (id != '') {
		var querystringParams = ""; 
		if (window.location.href.indexOf("isdtp=mn")!=-1) { 
			querystringParams = "?isdtp=mn"; 
		} else if (window.location.href.indexOf("isdtp=vw")!=-1) { 
			querystringParams = "?isdtp=vw"; 
		} 
		
		//We have successfully retrieved a case 
		if (sforce.console.isInConsole()) { 
			sforce.console.openPrimaryTab(null, '/'+id + querystringParams, true, null, null, null);
		} else { 
			navigateToUrl('/'+id + querystringParams);
		}
	}
	else {
		alert('No cases are available at this time.'); 
	}
}

We should be executing "RetrieveNextUtils.retrieveNextCase" only if not out of office.

Hope this helps! If yes, then mark it as solution.

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
Hello Adda,

You need to use global variables to make this check on user object.
if ( '{!$User.Out_of_Office__c}' == true ) {
	alert ('Owner you are trying to assign is out of office. Assign this case to other user!');
}

Hope this helps!

Thanks,
Sumit
AddaAdda
Thanks for the reply Sumit. I figured that out last Friday but the problem is my code is now not able to handle the validation error exception. Can you please help mw with the code how to handle validation error?
Sumitkumar_ShingaviSumitkumar_Shingavi
Code I posted should also show you if you are failing any validations. Any failing validations are always posted as exception so my button will handle it.
AddaAdda
Hi Sumit,

I tried with you code. It is throwing validation error first before executing the piece of code. Any suggestion. Here is my code.
{!REQUIRESCRIPT("/soap/ajax/18.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/18.0/apex.js")} 
{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/support/console/30.0/integration.js")} 

var id = sforce.apex.execute("RetrieveNextUtils","retrieveNextCase",{userId:""}); 

if (('{!User.Out_of_Office2__c}' == true)){
   alert ('Next case cannot be assigned because you are marked as out of office.');
   location.reload(true);
          }

else if (id!='') { 
var querystringParams = ""; 
if (window.location.href.indexOf("isdtp=mn")!=-1) { 
querystringParams = "?isdtp=mn"; 
} else if (window.location.href.indexOf("isdtp=vw")!=-1) { 
querystringParams = "?isdtp=vw"; 
} 
//We have successfully retrieved a case 
if (sforce.console.isInConsole()) { 
sforce.console.openPrimaryTab(null, '/'+id + querystringParams, true, null, null, null);
} else { 
navigateToUrl('/'+id + querystringParams);
}
} else { 
alert('No cases are available at this time.'); 
}



 
Sumitkumar_ShingaviSumitkumar_Shingavi
Below code should work for you. Try this out:
{!REQUIRESCRIPT("/soap/ajax/18.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/18.0/apex.js")} 
{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/support/console/30.0/integration.js")} 

if (('{!User.Out_of_Office2__c}' == true)){
   alert ('Next case cannot be assigned because you are marked as out of office.');
   location.reload(true);
} else {
	var id = sforce.apex.execute("RetrieveNextUtils","retrieveNextCase",{userId:""}); 
	
	if (id != '') {
		var querystringParams = ""; 
		if (window.location.href.indexOf("isdtp=mn")!=-1) { 
			querystringParams = "?isdtp=mn"; 
		} else if (window.location.href.indexOf("isdtp=vw")!=-1) { 
			querystringParams = "?isdtp=vw"; 
		} 
		
		//We have successfully retrieved a case 
		if (sforce.console.isInConsole()) { 
			sforce.console.openPrimaryTab(null, '/'+id + querystringParams, true, null, null, null);
		} else { 
			navigateToUrl('/'+id + querystringParams);
		}
	}
	else {
		alert('No cases are available at this time.'); 
	}
}

We should be executing "RetrieveNextUtils.retrieveNextCase" only if not out of office.

Hope this helps! If yes, then mark it as solution.
This was selected as the best answer