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
Adriana VoyceAdriana Voyce 

JS button to update Opportunity "Unexpected Token Illegal"

I am trying to create a button that will update the stage of an opportunity to a specific stage, and the owner to whom ever clicks the button, additionally I want the update to happen only if a specific profile clicks the button (this may be best accomplished through a validation rule though so its not in my code) 

This is what I have written just trying to get the stage to change.... I get this error: "Unexpected Token Illegal"

***Note*** I am not a developer but trying to learn so my knoweledge is mimited 


{!requireScript("/soap/ajax/10.0/connection.js")};
sforce.connection.session = "{!$Api.Session_ID}";

function updateOpportunity( )
{
    try
    {

 
var opp = new sforce.SObject(“Opportunity”); 
opp.id =”{!Opportunity.Id}”;                  
opp.StageName = "AE Qualified”;               

var saveResult = sforce.connection.update([Opportunity]); 


if (result[0].getBoolean("success") == false ) { 
alert(result[0].errors.message); 
return; 

window.top.location.href=window.top.location.href; 

catch (e) { 
alert(e); 
}    


updateOpportunity();
Best Answer chosen by Adriana Voyce
AMIT KAMBOJAMIT KAMBOJ
Hi Adriana,

Yes your code was very close. I have enhanced the code to change the owner to user that clicked the button but only if the users role contains AE or is an Admin. I tested code works fine.

Please remember to mark this as a solution/answer if this resolves your question.
 
{!requireScript("/soap/ajax/29.0/connection.js")};
{!requireScript("/soap/ajax/29.0/apex.js")};
sforce.connection.session = "{!$Api.Session_ID}";

function updateOpportunity( )
{
    try
    {

	var currentUser = sforce.connection.getUserInfo();
	var currentuserrole ;
	var resultuser = sforce.connection.query('select name from userrole where id=\''+currentUser.roleId+'\'');
	var arrayResults = resultuser.getArray("records");
	currentuserrole = arrayResults[0].Name;
	if(currentuserrole != null )
	{
		if(currentuserrole.indexOf("AE") != '-1' || currentuserrole.indexOf("Admin") != '-1')
		// Amit : above line checks if role is of AE or Admin 
		{
			var opp = new sforce.SObject("Opportunity");  // Amit: cleaned the double quotes manually and retyped
			opp.id ="{!Opportunity.Id}";                  // Amit: cleaned the double quotes manually and retyped
			opp.StageName = "AE Qualified";               // Amit: cleaned the double quotes manually and retyped
			opp.OwnerId = "{!$User.Id}";		      // Amit: Added this to set owner
			var result = sforce.connection.update([opp]); // Amit: changes Opportunity to opp , changed saveResult to result


			if (result[0].getBoolean("success") == false ) 
			{ 
				alert(result[0].errors.message); 
				return; 
			} 
			window.top.location.href=window.top.location.href; 
		}
		else
		{
		alert('You Do Not Have Sufficient Priviliges To Perform This Action. Only AE or Admin roles can do this.');

		}
	}
    } 
	catch (e) 
	{ 
		alert(e.message); 
	}    
} 

updateOpportunity();

 
 

All Answers

Balaji Chowdary GarapatiBalaji Chowdary Garapati
Try this version of code:
 
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}

var OpportunityObj= new sforce.SObject("Opportunity");
OpportunityObj.Id='{!Opportunity.Id}';
OpportunityObj.StageName = "AE Qualified”;
var result=sforce.connection.update([OpportunityObj]);

if (result[0].success=='false') {
    alert(result[0].errors.message);
} else {
    location.reload(true);
}

If you want to validate the profile name in the code its self, you can do it  like this:
 
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}

If('{!$Profile.Name}'=='Your Desired Profile Name'){
var OpportunityObj= new sforce.SObject("Opportunity");
OpportunityObj.Id='{!Opportunity.Id}';
OpportunityObj.StageName = "AE Qualified”;
var result=sforce.connection.update([OpportunityObj]);

if (result[0].success=='false') {
    alert(result[0].errors.message);
} else {
    location.reload(true);
}
}else{
 alert('You Do Not Have Sufficient Priviliges To Perform This Action');
}



Hope it helps.,

Thanks,
balaji
AMIT KAMBOJAMIT KAMBOJ
Hi Adriana,

I was able to reproduce the error and fix it.
You code (updated) below works now.
  1. {!requireScript("/soap/ajax/10.0/connection.js")};
    sforce.connection.session = "{!$Api.Session_ID}";
    
    function updateOpportunity( )
    {
        try
        {
    
     
    var opp = new sforce.SObject("Opportunity");  // Amit: cleaned the double quotes manually and retyped
    opp.id ="{!Opportunity.Id}";                  // Amit: cleaned the double quotes manually and retyped
    opp.StageName = "AE Qualified";               // Amit: cleaned the double quotes manually and retyped
    
    var result = sforce.connection.update([opp]); // Amit: changes Opportunity to opp , changed saveResult to result
    
    
    if (result[0].getBoolean("success") == false ) { 
    alert(result[0].errors.message); 
    return; 
    } 
    window.top.location.href=window.top.location.href; 
    } 
    catch (e) { 
    alert(e); 
    }    
    } 
    
    updateOpportunity();
Adriana VoyceAdriana Voyce
@AMIT KAMBOJ Thank you so much... So I was very close right?! How do I get it to change the owner to the user that clicked the button? I tried the code you gave me @Balaji Chowdary Garapati but I got the illegal token error the idea is change the owner to the user that clicked the button but only if the users role contains AE or is an Admin 
AMIT KAMBOJAMIT KAMBOJ
Hi Adriana,

Yes your code was very close. I have enhanced the code to change the owner to user that clicked the button but only if the users role contains AE or is an Admin. I tested code works fine.

Please remember to mark this as a solution/answer if this resolves your question.
 
{!requireScript("/soap/ajax/29.0/connection.js")};
{!requireScript("/soap/ajax/29.0/apex.js")};
sforce.connection.session = "{!$Api.Session_ID}";

function updateOpportunity( )
{
    try
    {

	var currentUser = sforce.connection.getUserInfo();
	var currentuserrole ;
	var resultuser = sforce.connection.query('select name from userrole where id=\''+currentUser.roleId+'\'');
	var arrayResults = resultuser.getArray("records");
	currentuserrole = arrayResults[0].Name;
	if(currentuserrole != null )
	{
		if(currentuserrole.indexOf("AE") != '-1' || currentuserrole.indexOf("Admin") != '-1')
		// Amit : above line checks if role is of AE or Admin 
		{
			var opp = new sforce.SObject("Opportunity");  // Amit: cleaned the double quotes manually and retyped
			opp.id ="{!Opportunity.Id}";                  // Amit: cleaned the double quotes manually and retyped
			opp.StageName = "AE Qualified";               // Amit: cleaned the double quotes manually and retyped
			opp.OwnerId = "{!$User.Id}";		      // Amit: Added this to set owner
			var result = sforce.connection.update([opp]); // Amit: changes Opportunity to opp , changed saveResult to result


			if (result[0].getBoolean("success") == false ) 
			{ 
				alert(result[0].errors.message); 
				return; 
			} 
			window.top.location.href=window.top.location.href; 
		}
		else
		{
		alert('You Do Not Have Sufficient Priviliges To Perform This Action. Only AE or Admin roles can do this.');

		}
	}
    } 
	catch (e) 
	{ 
		alert(e.message); 
	}    
} 

updateOpportunity();

 
 
This was selected as the best answer
Adriana VoyceAdriana Voyce
Amit thank you soooooooooooooooooooooo much!!! quick question... is it possible with a js to override permissions? The AE's currently cannot change opportunities to them selves so they get an error... but works great for me but obviously because I'm an admin 
AMIT KAMBOJAMIT KAMBOJ
Hi Adriana,

As I understand there is existing logic in system which does not allow AE's to change the owner for opportunities to themselves currently(lets call it A ). I think we need to do some change there(in A ) too to make it happen. Why dont we takeoff A's logic for AE and put that whole logic in JS.
That way it is possible. If you explain me how permissions are enforce for A then I can try to find s solution.

Thanks
Amit