[SalesForce] Delete record created by JavaScript on Cancel

I have added the following JavaScript detail page custom button, to our Opportunity page layouts, to create a record for my custom object WE_FEP__c

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

try{ 
var recToCreate = new sforce.SObject("WE_FEP__c"); 
recToCreate.Account_Name__c = '{!Opportunity.AccountId}'; 
recToCreate.Name = '{!Opportunity.Account}'; 
recToCreate.Description__c = '{!JSENCODE(Opportunity.Description)}'; 
recToCreate.Value_Of_WE_Spend__c = '{!TEXT(Opportunity.Annual_Settlement_Value__c)}'; 

var result = sforce.connection.create([recToCreate]); 

if(result[0].success == "true"){ 
window.location = "/" + result[0].id + "/e"; 
} 
else{ 
alert("An Error has Occurred. Error:" + result[0].errors.message); 
} 
} 
catch(e){ 
alert("An Error has Occurred. Error:" + e); 
}

If the user clicks the Cancel button on the record edit page, the new record is still inserted (or at least, not deleted) and the user is returned to the homepage.

I need the user to be taken to the edit page of the new record on button click and then if they click Cancel, to delete the new record and return the user to the Opportunity.

But I can't find any guides which explain how to enable this functionality or why the normal cancel behavior isn't executed when the record's been created through JavaScript?

Best Answer

You can do this by creating a custome cancel button to delete all records created at this session. For that on the code of button to create WE_FEP__c object do the below modification to keep the id of the newly created record in a jvascript varaible.. Now when you click custom cancel button delete all WE_FEP__c records with id specified by the javascript variable.. mentioned .. Below is the code to be used for button to create WE_FEP__c object..

Here when you click on create button page will not be redirected as you may need to cancel it explicitly

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

    var recordIds = ''

    try{ 

    var recToCreate = new sforce.SObject("WE_FEP__c"); 
    recToCreate.Account_Name__c = '{!Opportunity.AccountId}'; 
    recToCreate.Name = '{!Opportunity.Account}'; 
    recToCreate.Description__c = '{!JSENCODE(Opportunity.Description)}'; 
    recToCreate.Value_Of_WE_Spend__c = '{!TEXT(Opportunity.Annual_Settlement_Value__c)}'; 

    var result = sforce.connection.create([recToCreate]); 

    if(result[0].success == "true"){ 
        window.location = "/" + result[0].id + "/e"; 


    } 
    else{ 
    alert("An Error has Occurred. Error:" + result[0].errors.message); 
    } 
    } 
    catch(e){ 
    alert("An Error has Occurred. Error:" + e); 
    }

On cancel custom button button put the following code..

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

try{ 

var result = sforce.connection.deleteIds([recordIds.split(',')]); 

if(result[0].success == "true"){ 
  window.location = "/" + result[0].id + "/e"; 
} 
else{ 
alert("An Error has Occurred. Error:" + result[0].errors.message); 
} 
} 
catch(e){ 
alert("An Error has Occurred. Error:" + e); 
}
Related Topic