3

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?

  • How many fields you need to copy from opportunity? – SFIntegrator Jun 9 '15 at 10:57
  • Just the fields listed so 4. – Alex S Jun 9 '15 at 10:57
  • 3
    Then no need to create the record using javascript, just do redirect to the edit page of your custom object by populating 4 fields, have a look developer.salesforce.com/forums/?id=906F0000000AUfSIAW – SFIntegrator Jun 9 '15 at 11:01
  • I can't use a list custom button for this unfortunately because the custom object records aren't related to the Opportunity. – Alex S Jun 9 '15 at 11:06
  • That isn't a list custom button. SFIntegrator is suggesting that you build a URL that takes you to the edit page for your custom object with those fields prepopulated. Once on the edit page for that custom object record, if the user clicks cancel nothing is committed to the DB. You would not need connection.js for this any more. – frontendloader Jun 10 '15 at 16:29
2

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); 
}
| improve this answer | |
  • Thanks Shebin, what would happen if the user clicks the custom cancel button having saved the new record? – Alex S Jun 9 '15 at 11:33
  • The first button creates the record and cancel button first deletes the records created on this page load and then redirect back – Shebin Mathew Jun 9 '15 at 11:37
  • I understand but what happens if the button is clicked after an addtional page load? – Alex S Jun 9 '15 at 12:10
  • it will not delete.. Can you specify some condition on which the previous records has to be deleted? – Shebin Mathew Jun 9 '15 at 12:16
  • Your suggestion wouldn't work for me, I need to open the edit page of the new record on button click and then allow the user to cancel from there. – Alex S Jun 9 '15 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.