+ Start a Discussion
Aidel BruckAidel Bruck 

On-click java button on opportunity to open case AND update field on opportunity

I need to have a custom onclick java button on the opportunity record to open a new case AND to update a field on the opportunity. 

Here is what I have so far. 

{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")} 
var Opportunity = new sforce.SObject("Opportunity"); 
Opportunity.ID = '{!Opportunity.Id}'; 
Opportunity.case_created__C = 1; 

result = sforce.connection.update([Opportunity]); 

How do I add the new case?
 
Best Answer chosen by Aidel Bruck
Khan AnasKhan Anas (Salesforce Developers) 
Aidel, you need to use window.location = "/"+caseresult[0].id+"/e"; to redirect a user to case detail page.

Try this:
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 

var opptyObj = new sforce.SObject("Opportunity"); 
var caseObj = new sforce.SObject("Case"); 

caseObj.AccountId = "{!Opportunity.AccountId}"; 
caseObj.Opportunity__c = "{!Opportunity.Id}"; 
var caseresult = sforce.connection.create([caseObj]); 

opptyObj.Id = '{!Opportunity.Id}'; 
opptyObj.StageName = 'Prospecting'; 
var oppresult = sforce.connection.update([opptyObj]); 


if (oppresult[0].success=='false') 
{ 
alert("Opportunity updation failed: " + oppresult[0].errors.message); 
} 
else 
{ 
alert("Successfully Updated"); 
} 

if (caseresult[0].success=='false') 
{ 
alert("Case creation failed: " + caseresult[0].errors.message); 
} 
else 
{ 
window.location = "/"+caseresult[0].id+"/e"; 
}

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Aidel,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 

var opptyObj = new sforce.SObject("Opportunity"); 
var caseObj = new sforce.SObject("Case"); 

caseObj.AccountId = "{!Opportunity.AccountId}"; 
caseObj.Opportunity__c = "{!Opportunity.Id}"; 
var caseresult = sforce.connection.create([caseObj]);

opptyObj.Id = '{!Opportunity.Id}';
opptyObj.Case_Created__c = 1;
var oppresult = sforce.connection.update([opptyObj]);


if (oppresult[0].success=='false') 
{ 
alert("Opportunity updation failed: " + oppresult[0].errors.message); 
} 
else 
{ 
alert("Successfully Updated"); 
location.reload(true); 
}

if (caseresult[0].success=='false') 
{ 
alert("Case creation failed: " + caseresult[0].errors.message); 
} 
else 
{ 
alert("A Case has been created and linked to this Opportunity."); 
location.reload(true); 
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Aidel BruckAidel Bruck
Thank you! 
I want the button to redirect the user to the new case. how can I do that?
Khan AnasKhan Anas (Salesforce Developers) 
Aidel, you need to use window.location = "/"+caseresult[0].id+"/e"; to redirect a user to case detail page.

Try this:
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 

var opptyObj = new sforce.SObject("Opportunity"); 
var caseObj = new sforce.SObject("Case"); 

caseObj.AccountId = "{!Opportunity.AccountId}"; 
caseObj.Opportunity__c = "{!Opportunity.Id}"; 
var caseresult = sforce.connection.create([caseObj]); 

opptyObj.Id = '{!Opportunity.Id}'; 
opptyObj.StageName = 'Prospecting'; 
var oppresult = sforce.connection.update([opptyObj]); 


if (oppresult[0].success=='false') 
{ 
alert("Opportunity updation failed: " + oppresult[0].errors.message); 
} 
else 
{ 
alert("Successfully Updated"); 
} 

if (caseresult[0].success=='false') 
{ 
alert("Case creation failed: " + caseresult[0].errors.message); 
} 
else 
{ 
window.location = "/"+caseresult[0].id+"/e"; 
}

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
JayantJayant
Try
window.top.location = "/" + caseresult[0].Id;
instead of 
location.reload(true); in line # 32
Aidel BruckAidel Bruck
caseresult[0].id is undefined for some reason. Here is the button {!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} var opptyObj = new sforce.SObject("Opportunity"); var caseObj = new sforce.SObject("Case"); caseObj.AccountId = "{!Opportunity.AccountId}"; caseObj.Service_Name__c = "{!Opportunity.Id}"; var caseresult = sforce.connection.create([caseObj]); opptyObj.Id = '{!Opportunity.Id}'; opptyObj.Case_Created__c = 1; var oppresult = sforce.connection.update([opptyObj]); if (oppresult[0].success=='false') { alert("Opportunity updation failed: " + oppresult[0].errors.message); } else { alert("Successfully Updated"); location.reload(true); } if (caseresult[0].success=='false') { alert("Case creation failed: " + caseresult[0].errors.message); } else { alert("A Case has been created and linked to this Opportunity. "+ caseresult.Id); window.top.location = caseresult[0].Id; } *Aidel Bruck* Rofeh Cholim Cancer Society *Office*: 077-300-7482 ext: 200 *Mobile*: 011-972-50-418-9123 *US:* 1-929-255-1759
Khan AnasKhan Anas (Salesforce Developers) 
Hi Aidel,

Use window.location = "/"+caseresult[0].id+"/e"; 
Use the above code which I have provided. It will work definitely.

Regards,
Khan Anas
Aidel BruckAidel Bruck
Its great! Thank you. One more thing- When I click save on the edit case screen I want to be redirected to the case. As of now I am redirected to the home page. *Aidel Bruck* Rofeh Cholim Cancer Society *Office*: 077-300-7482 ext: 200 *Mobile*: 011-972-50-418-9123 *US:* 1-929-255-1759
JayantJayant
What is your exact requirement and the sequence of events you want ?

What do you mean by opening a case and updating a field on Opportunity ?

When user clicks the button, should he be navigated to the Case creation page or you want the Case to be inserted in the database automatically (how are you setting Case fields in this case ?) and then navigate the user to the detail page of the Case that was automatically inserted ? 
JayantJayant
 "/"+caseresult[0].id+"/e"; 
represents the edit page for the Case.

"/" + caseresult[0].Id;
represents the detail page of the Case.

If you are not changing any info on the Case Edit page and simply clicking the Save button without modifying any fields, you should redirect to detail page directly (intermediate edit page is not required).

Else you should add a retURL or saveURL query string parameter to the URL that points to Case detail page.

i.e. if you are using
window.location = "/"+caseresult[0].id+"/e"; 
as of now, you should instead use
window.location = "/"+caseresult[0].id+"/e" + "?saveURL=/" + caseresult[0].id; 
or
window.location = "/"+caseresult[0].id+"/e" + "?retURL=/" + caseresult[0].id; 
Aidel BruckAidel Bruck
Thank you all! Here is the final button for future reference {!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} var opptyObj = new sforce.SObject("Opportunity"); var caseObj = new sforce.SObject("Case"); caseObj.AccountId = "{!Opportunity.AccountId}"; caseObj.Service_Name__c = "{!Opportunity.Id}"; var caseresult = sforce.connection.create([caseObj]); opptyObj.Id = '{!Opportunity.Id}'; opptyObj.Case_Created__c = 1; var oppresult = sforce.connection.update([opptyObj]); window.location = "/"+caseresult[0].id+"/e" + "?saveURL=" + caseresult[0].id; /* if (oppresult[0].success=='false') { alert("Opportunity updation failed: " + oppresult[0].errors.message); } else { alert("Successfully Updated"); location.reload(true); } if (caseresult[0].success=='false') { alert("Case creation failed: " + caseresult[0].errors.message); } else { alert("A Case has been created and linked to this Opportunity. "+ caseresult.Id); } */
JayantJayant
Congrats! Good to know it finally works. :)
Please mark the answer that you feel helped you the most to resolve this as the best answer.

Thanks.