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
Samantha ChuaSamantha Chua 

Custom Button to Generate Case of Specific Record Type

Hi,

I have a snippet of code for a custom button which works great. But issue here is that it only generates the case record type tag to the profile of the current user. 

I am trying to get it to create another case record type instead but somehow the case.recordtypeid isnt working. Would appreciate any help or feedback on my snippet of codes here! (:
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
var status = "{!Opportunity.StageName}"; 
var newRecords = []; 
if (status == "Closed Won") 
{ 
var c = new sforce.SObject("Case"); 
c.RecordType = "0126F000000uanz"; 
c.AccountId = "{!Opportunity.AccountId}"; 
c.Type = "{!Opportunity.Type}"; 
c.Subject = "{!Opportunity.Name}"; 

newRecords.push(c); 
result = sforce.connection.create(newRecords); 
alert ('Case has been created.Please refresh page')} 
else 
{ 
alert ('Opportunity must be won before a case can be generated.'); 
}

Regards,
Samantha
Best Answer chosen by Samantha Chua
Samantha ChuaSamantha Chua
Thanks guys for your input! Record type access was not a problem as the profile has got access to all record types. I have managed to resolve it with these codes to grab the record type ID out:
 
var caseRecordType = sforce.connection.query("Select Id from RecordType where SobjectType = 'Case' and DeveloperName = 'Hellos'"); 

var records = caseRecordType.getArray("records"); 


if (status == "Closed Won") 
{ 
var c = new sforce.SObject("Case"); 
c.RecordTypeId = records[0].Id;

Cheers (:
 

All Answers

Shashikant SharmaShashikant Sharma
Hi Samantha,

Change 

c.RecordType = "0126F000000uanz"; 

to
c.RecordTypeId = "0126F000000uanz";
So your code will be notice the line in bold font
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
var status = "{!Opportunity.StageName}"; 
var newRecords = []; 
if (status == "Closed Won") 
{ 
var c = new sforce.SObject("Case"); 
c.RecordTypeId = "0126F000000uanz"; 
c.AccountId = "{!Opportunity.AccountId}"; 
c.Type = "{!Opportunity.Type}"; 
c.Subject = "{!Opportunity.Name}"; 

newRecords.push(c); 
result = sforce.connection.create(newRecords); 
alert ('Case has been created.Please refresh page')} 
else 
{ 
alert ('Opportunity must be won before a case can be generated.'); 
}

Please let me know if it helps you or you still face issues.

Thanks
Shashikant
Pankaj_GanwaniPankaj_Ganwani
Hi,

You will have to specify the RecordTypeId value on Case object. There is no field exist named as RecordType on Sobjects.
 
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
var status = "{!Opportunity.StageName}"; 
var newRecords = []; 
if (status == "Closed Won") 
{ 
var c = new sforce.SObject("Case"); 
c.RecordTypeId = "0126F000000uanz";
c.AccountId = "{!Opportunity.AccountId}"; 
c.Type = "{!Opportunity.Type}"; 
c.Subject = "{!Opportunity.Name}"; 
newRecords.push(c); 
result = sforce.connection.create(newRecords); 
alert ('Case has been created.Please refresh page')
} 
else 
{ 
alert ('Opportunity must be won before a case can be generated.'); 
}

 
Samantha ChuaSamantha Chua
Hi all, I've tried that as well. It does not work unfortunately. Maybe I need a SOQL or something. Best regards, Samantha
Pankaj_GanwaniPankaj_Ganwani
Check whether the current logged in user's profile has RecordType assigned which you are going to assign in the code.
Shashikant SharmaShashikant Sharma
Are you able to create a case with this record type manualy ? If not then configure the profile access for the record type and then try the above code again.

Thanks
Shashikant
Samantha ChuaSamantha Chua
Thanks guys for your input! Record type access was not a problem as the profile has got access to all record types. I have managed to resolve it with these codes to grab the record type ID out:
 
var caseRecordType = sforce.connection.query("Select Id from RecordType where SobjectType = 'Case' and DeveloperName = 'Hellos'"); 

var records = caseRecordType.getArray("records"); 


if (status == "Closed Won") 
{ 
var c = new sforce.SObject("Case"); 
c.RecordTypeId = records[0].Id;

Cheers (:
 
This was selected as the best answer