+ Start a Discussion
StaciStaci 

javascript button when cancelled still saving changes

I'm not sure how to accomplish this.  I have a javascript button Reject, but if someone decides to cancel rejecting the record, the Status still get changed to Rejected.  Where do I move the update code so that if they click Cancel on the Edit screen the Status won't change?

 

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 
{!requireScript("/soap/ajax/20.0/apex.js")} 

var changeObj = new sforce.SObject("Change__c");
changeObj.Id = '{!Change__c.Id}';

var profileName = '{!$Profile.Name}';
var roleName = '{!$UserRole.Name}';

var d=new Date().toISOString();;
/*var n=d.toJSON();*/

if(changeObj.StatusChange__c='Proposed')
{ 

//If current user’s profile is CW Pilot Dealer Portal User and Contact has accepted or Contact is a Dealer Portal User
if((profileName.indexOf("Dealer")!=-1)&&('{!Change__c.Contact_Accepted__c}'==true))
{
changeObj.Dealer_Rejected__c='{!$User.FirstName}'+' {!$User.LastName}';
changeObj.Dealer_Rejected_Checkbox__c= true; //check the box 
/*changeObj.Dealer_Rejected_Date_Time__c="changeObj.Dealer_Rejected_Date_Time__c";*/
changeObj.Dealer_Rejected_Date_Time__c=d;
changeObj.StatusChange__c='Rejected';

window.parent.location='/apex/rejectChangeDealer?Id={!Change__c.Id}&retURL=/{!Change__c.Id}';

}else{

//If current user's profile is CW Pilot Dealer Portal User and Contact Accepted is not checked, throw an error 
if((profileName.indexOf("Dealer")!=-1)&&('{!Change__c.Contact_Accepted__c}' != true))
{ 
alert("Additional approvals/rejections are required before you can perform this action.");
}else{

//If current users profile is Customer Portal, fill in the current users name for Contact Rejected and fill in the current date and time 
if(profileName.indexOf("Customer")!=-1)
{
changeObj.Contact_Rejected__c='{!$User.FirstName}'+' {!$User.LastName}';
changeObj.Contact_Rejected_Checkbox__c= true;
/*changeObj.Contact_Rejected_Date_Time__c="changeObj.Contact_Rejected_Date_Time__c";*/
changeObj.Contact_Rejected_Date_Time__c=d;
changeObj.StatusChange__c='Rejected';
alert("You have selected to reject this change.  Please fill in the Contact Rejected Comments in the Rejected Information section towards the bottom this Change record.")
window.parent.location.href="/{!Change__c.Id}" + "/e"; 

}else{

if((roleName.indexOf("Tier 2") != -1) || (roleName.indexOf("Product Support Manager") != -1)) 
{
if(('{!Change__c.Contact_Accepted__c}'==true) && ('{!Change__c.Dealer_Accepted__c}'==true))
{
changeObj.Change_Manager_Rejected__c='{!$User.FirstName}'+' {!$User.LastName}';
changeObj.Change_Manager_Rejected_Checkbox__c=true;
/*changeObj.Change_Manager_Rejected_Date_Time__c="changeObj.Change_Manager_Rejected_Date_Time__c";*/
changeObj.Change_Manager_Rejected_Date_Time__c=d;
changeObj.StatusChange__c='Rejected';
window.parent.location='/apex/rejectChangeChgMgr?Id={!Change__c.Id}&retURL=/{!Change__c.Id}';
}else{
alert("Additional approvals/rejections are required before you can perform this action.");
}
}else{
alert("You are not authorized to perform this action.");
}}}}
}else{
alert("Status needs to be Planned before you approve of this change.");
}

if(changeObj.StatusChange__c == 'Approved')
{
alert("Change has already been Approved.");
}else{

if((changeObj.StatusChange__c=='Implemented') || (changeObj.StatusChange__c=='Completed'))
{
alert("Change has already been Implemented. Approvals are no longer needed.");
}}

var result = sforce.connection.update([changeObj]);
if (result[0].success=='false')
{
alert(result[0].errors.message);
}

 

SoleesSolees

You need to change this to somewhere else higher or after each action you really like to do.  Since this is at the end of your code, will always be executed.

 

var result = sforce.connection.update([changeObj]);
Kaybee011Kaybee011
Hi Staci.  Did you ever manage to resolve this? I'm currently facing the same issue.  Thanks.
StaciStaci
Hi @Kaybee011 here's my working code:
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 
{!requireScript("/soap/ajax/20.0/apex.js")} 

var changeObj = new sforce.SObject("Change__c");
changeObj.Id = '{!Change__c.Id}';

var profileName = '{!$Profile.Name}';
var roleName = '{!$UserRole.Name}';

if(changeObj.StatusChange__c='Proposed')
{ 

//If current user’s profile is CW Pilot Dealer Portal User and Contact has accepted or Contact is a Dealer Portal User
if(((profileName.indexOf("Dealer")!=-1)||(roleName.indexOf("Tier 2") != -1))&&(('{!Change__c.Contact_Accepted__c}'==true)||('{!Change__c.CW_Customer_Implemented__c}'== true))&& ('{!Change__c.Dealer_Accepted__c}'!=true))
{
window.parent.location='/apex/CW_rejectChangeDealer?Id={!Change__c.Id}&retURL=/{!Change__c.Id}';

}else{

//If current user's profile is Dealer User or T2 User and Contact Accepted is not checked, throw an error 
if((profileName.indexOf("Dealer")!=-1)&&(('{!Change__c.Contact_Accepted__c}' != true)||('{!Change__c.CW_Customer_Implemented__c}'!= true)))
{ 
alert("Dealer: Additional approvals/rejections are required before you can perform this action.");
}else{

//If current users profile is Customer or T2 User, fill in the current users name for Contact Rejected and fill in the current date and time 
if(((profileName.indexOf("Customer")!=-1)||(roleName.indexOf("Tier 2") != -1))&&('{!Change__c.CW_Customer_Implemented__c}'!= true)&&('{!Change__c.Dealer_Accepted__c}'!=true))
{
window.parent.location='/apex/CW_rejectCustomer?Id={!Change__c.Id}&retURL=/{!Change__c.Id}';
}else{

if((roleName.indexOf("Tier 2") != -1) || (roleName.indexOf("Product Support Manager") != -1)) 
{
if((('{!Change__c.Contact_Accepted__c}'==true)
||('{!Change__c.CW_Customer_Implemented__c}'== true)) 
&&('{!Change__c.Dealer_Accepted__c}'==true))
{
window.parent.location='/apex/CW_rejectChangeChgMgr?Id={!Change__c.Id}&retURL=/{!Change__c.Id}';
}else{
alert("Change MGR: Additional approvals/rejections are required before you can perform this action.");
}
}else{
alert("You are not authorized to perform this action.");
}}}}
}else{
alert("Status needs to be Planned before you reject this change.");
}

var result = sforce.connection.update([changeObj]);
if (result[0].success=='false')
{
alert(result[0].errors.message);
}