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
Steve CairneySteve Cairney 

Additional alert on click javascript button after submission

I have a simple button that when clicked, cheanged the record type of the Opportunity in order to fire an email alert.
 
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 

var oppObj = new sforce.SObject("Opportunity"); 
oppObj.Id = '{!Opportunity.Id}'; 
oppObj.RecordTypeId = '01220000000ML20'; 
var result = sforce.connection.update([oppObj]); 

if (result[0].success=='false') { 
alert(result[0].errors.message); 
} else { 
alert("Success, Admin have been notified."); 
location.reload(true); 
}

However, I'd like to add a query so that if the button has already been clicked, another alert will show to the user.

Would it be possible for the button to check the RecordTypeID and if the record has alredy been changed to 01220000000ML20, the alternative alert is shown.

Here's what I've tried, but the syntax isn't correct. Any tips?
 
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
 
var oppObj = new sforce.SObject("Opportunity"); 
oppObj.Id = '{!Opportunity.Id}'; 
oppObj.RecordTypeId = '01220000000ML20'; 
var result = sforce.connection.update([oppObj]); 
 
if ({oppObj.RecordTypeId = '01220000000ML20'; )}
{alert("This has already been approved."); 
     location.reload(true);}
){ else if (result[0].success=='false') {
     alert(result[0].errors.message);
} else {
alert("Success, Admin have been notified."); 
     location.reload(true);
}

 
Nayana KNayana K
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")} 

var actualRecTypeId = '{!Opportunity.RecordTypeId}';

var RecTypeQueried = sforce.connection.query("SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' AND DeveloperName = 'PUT_RECORD_TYPE_NAME_HERE' LIMIT 1");

var recordsRecType = actualRecTypeId.getArray("records"); 

var desiredRecTypeId = recordsRecType[0].Id; 

if(desiredRecTypeId == actualRecTypeId)
{
	alert("Recordtype has already been changed");
}
else
{
	var oppObj = new sforce.SObject("Opportunity"); 
	oppObj.Id = '{!Opportunity.Id}'; 
	oppObj.RecordTypeId = '01220000000ML20'; 
	var result = sforce.connection.update([oppObj]); 

	if (result[0].success=='false') { 
	alert(result[0].errors.message); 
	} else { 
	alert("Success, Admin have been notified."); 
	location.reload(true); 
	}
}

Hardcoding Id is badpractice. Replace PUT_RECORD_TYPE_NAME_HERE with  record type name.
Steve CairneySteve Cairney
Hi Nayana, thanks for this, however I'm having some problems with errors
 
actualRecTypeId.getArray is not a function. (In 'actualRecTypeId.getArray("records")', 'actualRecTypeId.getArray' is undefined)

Am I right in thinking that line 10 should be records.RecTypeId?

 
Nayana KNayana K
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")} 

var actualRecTypeId = '{!Opportunity.RecordTypeId}';

var RecTypeQueried = sforce.connection.query("SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' AND DeveloperName = 'PUT_RECORD_TYPE_NAME_HERE' LIMIT 1");

var recordsRecType = RecTypeQueried.getArray("records"); 

var desiredRecTypeId = recordsRecType[0].Id; 

if(desiredRecTypeId == actualRecTypeId)
{
	alert("Recordtype has already been changed");
}
else
{
	var oppObj = new sforce.SObject("Opportunity"); 
	oppObj.Id = '{!Opportunity.Id}'; 
	oppObj.RecordTypeId = '01220000000ML20'; 
	var result = sforce.connection.update([oppObj]); 

	if (result[0].success=='false') { 
	alert(result[0].errors.message); 
	} else { 
	alert("Success, Admin have been notified."); 
	location.reload(true); 
	}
}

Sorry it was by mistake