function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
b.gonzalezb.gonzalez 

OnClick JavaScript custom close button on case object?

I am not sure how to write a OnClick JavaScript custom close button on case object.

When the user clicks on this custom button, the following fields need to be updated with the given values.

Fields on case object:
Product Family = Unknown        
Product Model = Unknown
Problem Code = Non-Technical
Resolution Code = Spam
Status = Closed
Resolution Description = Spam
Account Name (lookup field) =  Unclassified Customer – Minnetonka 

Also need to associate the contact to the account; Unclassified Customer – Minnetonka


I appreciate the help!

Beth
Best Answer chosen by b.gonzalez
sandeep sankhlasandeep sankhla
do one thing simply remove that if condition where you are checking if status is closed and then check if onclick of button it isupdating or not..

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

	

	var record = new sforce.SObject("Case");

	record.Id = "{!Case.Id}";

	record.Product_Family__c= 'Unknown';

	record.Product_Model__c= 'Unknown';

	record.Problem_Code__c= 'Inquiry - Non-Technical';

	record.Resolution_Code__c= 'Spam';

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

	// check the result here

	window.location.reload();
chek if it is updating or not ?

Thanks
 

All Answers

sandeep sankhlasandeep sankhla
Hi,

You mean you want to create one custom button on case detail page and when we click on that button then it shgould asign the above values to that specific object right ??

for this you can create a detail page button on that object and then you can simply update the same object with above values..

Let me know if you need any help in this..

Thanks,
Sandeep
sandeep sankhlasandeep sankhla
Hi,

please check below link adn let me know if it helps you..
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008ogFIAQ

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
b.gonzalezb.gonzalez
HI Sandeep,

Thank you for responding to my question. That is correct. 

If you could please help me I would appreciate. I am not sure how to start writing the button.

Thanks.

Beth
sandeep sankhlasandeep sankhla
Hi,

Following are the steps to achieve this..

1. Go to setup and type case and then click on button and links ..

2. Click new button and link and then provide the Label and name adn also select *Detail page button*.

3. Select Behaviour as executeJavascript and Content source as onclickjavascript..

4. then paste below code to script section

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var record = new sforce.SObject("Case");
record.Id = "{!Case.Id}";
record.Type= 'Other';
var result = sforce.connection.update([record]);
// check the result here
window.location.reload();


this is a example code which will update the type field to other when you click on the button adn it will reload the page with latets values..you can addd all your fields and provid ethe values ...

5. Also you just need to go to case page layout and add the custom button there ..

Please check with these adn let me knwo if it helps ..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer
b.gonzalezb.gonzalez
Hi Sandeep,

Thank you. I am experiencing an issue with when clickinig the button: Unexpected end of input

Here is the script:
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
if("{!Case.Status}" != "Closed"){ 
var record = new sforce.SObject("Case"); 
record.Id = "{!Case.Id}"; 
record.Product_Family__c= 'Unknown'; 
record.Product_Model__c= 'Unknown'; 
record.Problem_Code__c= 'Inquiry - Non-Technical'; 
record.Resolution_Code__c= 'Spam'; 
var result = sforce.connection.update([record]); 
// check the result here 
window.location.reload();

Please advise.

Thanks!

Beth
sandeep sankhlasandeep sankhla
Hi Beth,

Use below code:
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

	if("{!Case.Status}" != "Closed"){

	var record = new sforce.SObject("Case");

	record.Id = "{!Case.Id}";

	record.Product_Family__c= 'Unknown';

	record.Product_Model__c= 'Unknown';

	record.Problem_Code__c= 'Inquiry - Non-Technical';

	record.Resolution_Code__c= 'Spam';

	var result = sforce.connection.update([record]);
}
	// check the result here

	window.location.reload();
let me know if it helps..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer

 
b.gonzalezb.gonzalez
HI Sandeep,

I applied the code below and performed a test. The button is not working, the fields listed in the code on the contact record are not being update and the case is being closed. 

Beth
sandeep sankhlasandeep sankhla
HI,

These fields are on Contact or Case ? Based on current code it is taking current fields as case fields and try to update ...

Initally whatever code I haev given if you ahev tried that then that was updating case fields...if you want to update other objects then you need to first query them and then you need to update them..

Let me know exactly what you need...

Thanks
b.gonzalezb.gonzalez
Sorry, I wrote contact. I meant case record. 
sandeep sankhlasandeep sankhla
do one thing simply remove that if condition where you are checking if status is closed and then check if onclick of button it isupdating or not..

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

	

	var record = new sforce.SObject("Case");

	record.Id = "{!Case.Id}";

	record.Product_Family__c= 'Unknown';

	record.Product_Model__c= 'Unknown';

	record.Problem_Code__c= 'Inquiry - Non-Technical';

	record.Resolution_Code__c= 'Spam';

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

	// check the result here

	window.location.reload();
chek if it is updating or not ?

Thanks
 
This was selected as the best answer
b.gonzalezb.gonzalez
I did that. The record is updating now. Now I just need the Status = Closed. :-)

Beth
sandeep sankhlasandeep sankhla
HI
So what you need? you need tos et status as closed or in condition you need to check if its not closed then only update ?

Thanks
b.gonzalezb.gonzalez
I need the Status field to change to 'Closed'.

Beth
 
sandeep sankhlasandeep sankhla
HI,

Have you tried
record.status = 'Closed';

check with this and let me know if it helps..

Thanks
sandeep sankhlasandeep sankhla
If you dont have status closed enable for your status picklist then do following things ..

setup-->case--?support setting-->chek Show Closed Statuses in Case Status Field then this closed status value will be visible in picklist and as I mentioned you will be able to simply update the field using
record.status = 'Closed';

Please check and let m eknow..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer
 
b.gonzalezb.gonzalez
Thanks Sandeep for the help. That worked. 

Beth
sandeep sankhlasandeep sankhla
WC Beth.
Kailash SatiKailash Sati
Hi Sandeep, 
could you please tell me, how I could return to the "All Cases" page and not on the Case that is closed.

Thanks and regards
Kailash 
Radu Costache 17Radu Costache 17
Hello Kailash,

 Try the following snippet: - make sure to insert the proper URL for the window.location.href:
if (sforce.console.isInConsole()){ 
isConsoleTest = true; 

else{
    window.location.href = "https://developer.salesforce.com/forums/?id=906F0000000BJ0zIAG"; 
}

function testCloseTab() { 
//First find the ID of the current primary tab to close it 
sforce.console.getEnclosingPrimaryTabId(closeSubtab); 


var closeSubtab = function closeSubtab(result) { 
//Now that we have the primary tab ID, we can close it 
var tabId = result.id; 
sforce.console.closeTab(tabId); 
}; 
if(isConsoleTest){ 
testCloseTab(); 
}
sandeep sankhlasandeep sankhla
Hello Kailash,

Instead of hard coding you can just redirect it to that page using prefix which is constant in all the orgs...

/500/o...u can redirect it to this url then it will show you all teh cases....

Please check and let me know if that helps you.

Thanks,
Sandeep
Mohammed A KhanMohammed A Khan
I have a similar custom list button to change the status on Case object - this javascript executes perfectly for Classic users but not for Service Cloud User.
------------------------------------------
{!REQUIRESCRIPT('/soap/ajax/40.0/connection.js')}
{!REQUIRESCRIPT("/soap/ajax/40.0/apex.js")} 
var caseIds = {!GETRECORDIDS($ObjectType.Case)};
var cases = [], TCase, result;
while(caseIds.length) {
    TCase = new sforce.SObject('Case');
    TCase.Id = caseIds.shift();
    TCase.Delete__c= 'Purge';
    cases.push(TCase);
}
result = sforce.connection.update(cases);
window.top.location.href = window.top.location.href;

---------------------------------------------------------

Any help is appreciated.
 
Jose Vasquez 3Jose Vasquez 3
Hello there,

I'm having the same situation, in "license users" everithing is working properly. But when I try to press any javascript button from a Site the button show me this message:
"EntityObject cannot be initialized with null EntityInfo"

Every button in my organization its having this problem, I try to change the API version to 29, 40 and nothing works.