1

I need to create a New case custom button which will be displayed on the Opportunities object and selects the record type, while also populating other fields on the case page layout.

The easiest way to do this would be to use a URL like

/500/e?cas4={!Opportunity.Account}&CF00N700000030pnt={!Opportunity.Name}&RecordType=012180000004Kle&cas14=New Application 

but I also need to assign the case to a queue so it looks like I need to use JavaScript.

I've adapted the below JavaScript code from a Success post but when clicked an error message is displayed "Unexpected token catch" and I can't figure out why?

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

try{ 
    var recToUpdate = new sforce.SObject("Case");  
    recToUpdate.Id = "{!Case.Id}";  
    recToUpdate.OwnerId = "00G18000000Ntra";  
    recToUpdate.Opportunity__c = "{!Opportunity.Id}";  
    recToUpdate.Account = "{!Opportunity.AccountId}";  
    recToUpdate.Subject = "New Application";  

    var result = sforce.connection.update([recToUpdate]); 

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

1 Answer 1

0

Syntax wise you are missing a } for the try block.. once you add it, the error should be fixed.

but logic wise, your script is trying to update an existing case.. but your requirement is to create a New Case,

so you have to get rid of the caseId assignment statement and change the update call to create method call.

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

try{ 
    var recToCreate = new sforce.SObject("Case");  
    recToCreate.OwnerId = "00G18000000Ntra";  
    recToCreate.Opportunity__c = '{!Opportunity.Id}';  
    recToCreate.AccountId = '{!Opportunity.AccountId}';  
    recToCreate.Subject = "New Application";  

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

    if(result[0].success == "true"){
        location.reload();
    }
    else{
        alert("An Error has Occurred. Error:" + result[0].errors.message);
    }
}
catch(e){
    alert("An Error has Occurred. Error:" + e);
}
3
  • Thanks, that's exactly what I need, except the following error message is displayed when I click the button now "The external foreign key reference does not reference a valid entity:Account"
    – Alex S
    Commented Apr 12, 2015 at 12:50
  • 1
    I have updated the code. use AccountId instead of Account and use single quote for merge fields instead of double quotes. hope it works for you now. Commented Apr 12, 2015 at 12:54
  • The record type ID element was missing from the above code, recToCreate.RecordTypeId = "012180000004Kle"; (for example) would need to be included.
    – Alex S
    Commented Apr 15, 2015 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .