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
Steve HuseSteve Huse 

Javascript button code to check checkbox?

Hi,

We have a custom Javascript button in our opportunity which creates a case and carries some of the opportunity detail across into the case as it does so.

I have a checkbox field in the opportunity and another in the case, and I want the below code to check the checkbox in the new case being created if the checkbox in the opportunity was checked.

I've tried inserting the line in bold below, but this doesn't work.

Can anyone help here?

TIA
Steve


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

var status = "{!Opportunity.StageName}";

if (
"{!Opportunity.Type}" != "Maintenance - New Agreement" &&
"{!Opportunity.Type}" != "Maintenance - Agreement Renewal" &&
"{!Opportunity.Type}" != "Maintenance - Resi MBF"
){

if (status == "Closed Won") {
var caseToCreate = new sforce.SObject("Case");
caseToCreate.AccountId = "{!Opportunity.AccountId}";
caseToCreate.Type = "{!Opportunity.Type}";
caseToCreate.Subject = "{!Opportunity.Name}";
caseToCreate.Origin = "Converted Opportunity";
caseToCreate.Description = "{!Opportunity.Requirement__c}";
caseToCreate.Related_Opportunity__C = "{!Opportunity.Id}";
caseToCreate.ContactId = "{!Opportunity.Opportunity_ContactId__c}";
caseToCreate.RecordTypeId = "01230000001DNJc";
caseToCreate.Case_Value__c = "{!TEXT(Opportunity.Amount)}";
caseToCreate.Case_Parts_Required__c = "{!Opportunity.Opp_Parts_Required__c}";

result = sforce.connection.create([caseToCreate]);

if(result[0].success == "true"){
alert("Case has been created.");
}
else{
alert("Error: " + result[0].errors.message);
}

} else {
alert(
"Opportunity must be won " +
"before a case can be created."
);
}
} else
alert(
"A case cannot be created for maintenance opportunities, " +
"other than ad-hoc maintenance visits. " +
"For maintenance opportunites, please use the " +
"Create Contract button."
);
AnkaiahAnkaiah (Salesforce Developers) 
Hi Steve,

Can you check the field API names(Case_Parts_Required__c & Opp_Parts_Required__c ) are correct or not?

Thanks!!
Steve HuseSteve Huse
Yes, the API names are correct:
In the opportunity we have Opp_Parts_Required__c
In the case we have Case_Parts_Required__c

The button is on the opportunity page, and when I click it, I want the checkbox on the new case being created to be checked, if the checkbox on the opportunity is checked.

Thanks
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Steve,

I have tried the below code by removig some of the custom fields and its working.
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}

var status = "{!Opportunity.StageName}";

if (
"{!Opportunity.Type}" != "Maintenance - New Agreement" &&
"{!Opportunity.Type}" != "Maintenance - Agreement Renewal" &&
"{!Opportunity.Type}" != "Maintenance - Resi MBF"
){

if (status == "Closed Won") {
var caseToCreate = new sforce.SObject("Case");
caseToCreate.AccountId = "{!Opportunity.AccountId}";
caseToCreate.Type = "{!Opportunity.Type}";
caseToCreate.Subject = "{!Opportunity.Name}";
caseToCreate.Origin = "Converted Opportunity";
caseToCreate.Case_Parts_Required__c = "{!Opportunity.Opp_Parts_Required__c}";

result = sforce.connection.create([caseToCreate]);

if(result[0].success == "true"){
alert("Case has been created.");
}
else{
alert("Error: " + result[0].errors.message);
}

} else {
alert(
"Opportunity must be won " +
"before a case can be created."
);
}
} else
alert(
"A case cannot be created for maintenance opportunities, " +
"other than ad-hoc maintenance visits. " +
"For maintenance opportunites, please use the " +
"Create Contract button."
);

Can you please check the remaining fields API names and try.

Thanks!!

 
AnkaiahAnkaiah (Salesforce Developers) 
I have tested with the remaing fields too.. its working for me.

Can you please check, is there any validation rules are restricting while creation of case.

Thanks!!