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
Thorsten Haude 5Thorsten Haude 5 

Beginner: New Record Button

Hi,

I'm making my first steps, and I'm running against a wall already. It feels like this should be very easy, but I can't find a way to do it at all.

I want to create a button on the Case page to create a new record (custom object) based on the case displayed. The most promising (if the word applies) description is a six-year-old post on how to do your very own URL rewriting, but that does not seem to apply on custom objects (or the description is simply out of date).

In my fantasy SF I'd use a custom action of type "Create a Record", and set some translation table for which data to set in the new record (if required at all, should be obvious really). For some reason, "Create a Record" is limited to very few objects, and has no way to seed data into the new record.

So where to start? Is there some piece of SF wisdom I've still to pick up, so that I can answer questions like this with a simple search in the documentation?

There is still a whole lot of SF I haven't even looked at, maybe the answer is simple. I'm supposed to learn Apex anyway, and always wanted to try Javascript, so if one of those is the best solution, then that's where I want to go. Again, where to start reading?

Thanks in advance!

Cheers,
Thorsten
Best Answer chosen by Thorsten Haude 5
Vivek DVivek D
1. Do you want to create a record onlick of a button. Or 2. Open new record screen of that object on click of a button.
I am going by the first assumption that you want to create a record on click of a button.
Step 1. Create a Detail page button with Execute JavaScript.
Add this code into your button.
This button will call a class, in which you can pass case id. Perform your DML operation there and return a success or error message you want to display
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}

var message = sforce.apex.execute("YourClassName","YourMethodName", {methodVariable :localVariableCaseIdhere});
alert(message);
Class code to perform rest of the action. Create a class with the below details :- 
global class YourClassName{

 Webservice static String yourMethodName(String methodVariable){
  try{
     Id caseId = methodVariable; // just for example
    // Do Something here
       return 'Record Inserted Succesfully';
    }catch(Exception e){
       return e.getMessage();
    } 
 }
}
Note : - You can have your own name I used YourClassNames etc just for better understanding.


 

All Answers

Vivek DVivek D
1. Do you want to create a record onlick of a button. Or 2. Open new record screen of that object on click of a button.
I am going by the first assumption that you want to create a record on click of a button.
Step 1. Create a Detail page button with Execute JavaScript.
Add this code into your button.
This button will call a class, in which you can pass case id. Perform your DML operation there and return a success or error message you want to display
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}

var message = sforce.apex.execute("YourClassName","YourMethodName", {methodVariable :localVariableCaseIdhere});
alert(message);
Class code to perform rest of the action. Create a class with the below details :- 
global class YourClassName{

 Webservice static String yourMethodName(String methodVariable){
  try{
     Id caseId = methodVariable; // just for example
    // Do Something here
       return 'Record Inserted Succesfully';
    }catch(Exception e){
       return e.getMessage();
    } 
 }
}
Note : - You can have your own name I used YourClassNames etc just for better understanding.


 
This was selected as the best answer
Thorsten Haude 5Thorsten Haude 5
Working on it.... Class is in, but I get fuzzy Javascript errors ("Unexpected token ILLEGAL")... getting back to you.
Thorsten Haude 5Thorsten Haude 5
The class works from the console though...
Thorsten Haude 5Thorsten Haude 5
Here is my solution, just added a few attributes:
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")} 
var name = "{!Case.Subject}"; 
var contactID = "{!Case.ContactId}"; 
var accountID = "{!Case.AccountId}"; 

// should be {!$User.Username}, but that does not work in sandboxes. 
var owner = "{!Case.OwnerId}"; 

var message = sforce.apex.execute("ProjectFacade", "newProjectFromCase", {name:name, contactID:contactID, accountID:accountID, owner:owner}); 
alert(message);
 
global class ProjectFacade
{
    Webservice static String newProjectFromCase(String name, String contactID, String accountID, String owner)
    {
        try
        {
            Project__c p = new Project__c();
            p.name = name;
            p.contact__c = contactID;
            p.account__c = accountID;
            p.owner__c = owner;
            insert p;
            return name + ' inserted.';
        } catch (Exception e)
        {
            return e.getMessage();
        }
    }
}

Thank you very much for your help!