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
Eyal_WizEyal_Wiz 

Automatic Create

Is there a way to add a custom button that automatically creates a new record in specific object.
 
For example:
I created a new custom object called "Invoices". I want to create a new button on the opportunity form that once clicked on- creates a new record on the "Inovices" custom object- using the data from the current opportunity record.
 
Is it possible to do such thing using Javascript?
 
Thanks,
Eyal
SteveBowerSteveBower
Yes, Use an s-control like this:

Code:
<script language="JavaScript">
function redirect() {
parent.frames.location.replace(  "/a09/e—retUTL=/{!Opportunity.Id}" +
"&00N200000011spN={!Opportunity.Question_1__c}" +
"&00N200000011spO={!Opportunity.Question_2__c}" +
"&00N200000011spP={!Opportunity.Question_3__c}" +
"&00N20000001IKZt={!Opportunity.Question_4__c}" +
"&00N20000001IKZy={!Opportunity.Question_5__c}" +
"&00N20000001IKZ9={!Opportunity.Question_6__c}" +
"&00N20000001IKYp={!Opportunity.Question_7__c}" +
"&00N20000001IKZA={!Opportunity.Question_8__c}" ;
}
redirect();
</script>

Where:
"a09" is replaced with the three digit code for the Invoices object.

"00N200000011SPN" is replaced with the ID of a field in the DOM
for the Edit page of the object you are trying to create. (Go to
the Edit page for your Invoices object, use a View Source capability,
[I use the Web Developer extension for Firefox, but there are others.] and
look at the various input fields for the values you can edit and get the
ID's of those fields.)

"{!Opportunity.Question_8__c}" is replaced with the field from the Opportunity
that you wish to use for the value to be placed into the input element.

Take that code, put it into an s-control, build a custom link or button to
execute that code and add it to the page layout for the opportunity.
Best, Steve.
 

Eyal_WizEyal_Wiz

I'll try that right away.

Thanks for the quick reply- I really appreciate it.

Eyal

Eyal_WizEyal_Wiz
I've inserted the code you gave me into a s-control and updated the id's according to the instructions you gave me - but it's still not working.
 
I'm getting the following error:
 
Code:
URL No Longer Exists 
You have attempted to reach a URL that no longer exists on salesforce.com. 

You may have reached this page after clicking on a direct link into the application. This direct link might be: 
• A bookmark to a particular page, such as a report or view 
• A link to a particular page in the Custom Links section of your Home Tab, or a Custom Link 
• A link to a particular page in your email templates 

If you reached this page through a bookmark, you are probably trying to access something that has moved. Please update your bookmark. 

If you reached this page through any of the other direct links listed above, please notify your administrator to update the link. 

If you reached this page through a link on our site, please report the broken link directly to our Support Team and we will fix it promptly. Please indicate the page you were on when you clicked the link as well as any other related information. We apologize for the inconvenience. 

Thank you again for your patience and assistance. And thanks for using Salesforce 

 
It's like he's trying to open the new custom record but failing because the record isn't really there.
 
Do you got any idea what seems to be the problem?
 
Thanks,
Eyal
SteveBowerSteveBower
My apologies, I think something got messed up in my cutting and pasting, or I hit some keys without intending to, etc.

The code that is currently   "e-retUTL" should have been "e?retURL"   .   So, a question mark instead of a dash, and spell URL correctly.

For example, this code works:
Code:
<script language="JavaScript">
function redirect() {
parent.frames.location.replace(  "/a03/e—retURL=/{!Opportunity.Id}" +
"&Name={!Opportunity.Name}" );
}
redirect();
</script>



HEY!!!!! The problem with my cutting and pasting wasn't ME, it's the SRC box
in this crappy user interface. I pasted the correct code into the box, but when
you click on "Insert Source", it botches it up! (Or at least it strips the
question mark... I must have messed up the URT thing.)

So, the code above works when you make the edits nd build a properly formed URL.
:-)

Hope this helps, Steve.



 

Eyal_WizEyal_Wiz
Thanks again! it's working now.
splashgordonsplashgordon

Steve - I read this post with interest as I need to do something similar but, rather than using a button, I would like this to happen on Save (dangerous I know).  Essentially, we have a number of instances where once a certain object enters status, a new record on a related object needs to be created.  For example, once we move an opportunity to "Sold" stage, I need to create a new "Delivery Project" (custom object).  Currently, I am constantly educating my users that they need to do these two actions separately and, guess what, they forget and only do one of them.

Is this possible?

Your advice would be very much welcomed.

Regards,

Gordon.

Eyal_WizEyal_Wiz

Hi Gordon,

You can implement exactelly what you want using triggers. create a trriger on the opportunity object, the trigger will raise on update or insert. It should look something like this:

 

Code:
trigger CreateCustomObject on Opportunity (after update, after insert) {

Opportunity[] Current = Trigger.new;

 if (Current[0].Status == sold)
      {
        YOURCUSTOMOBJECT__c Custom = new YOURCUSTOMOBJECT__c (
        field1__c = Current[0].Opportunityfield1,
        field2__c = Current[0].Opportunityfield2,
        field3__c = Current[0].Opportunityfield3);
        insert Custom;
      }
 }


 
The current variant contains the data of the opportunity that caused the trigger to raise.

just change the "if" statement to the condition that you are looking for and thats it.

 

Hope this helps (If not feel free to ask),

Eyal

splashgordonsplashgordon
Eyal,
 
Great! Thanks for that.  It looks easy enough.  The only issue (and I should have mentioned this) is that we are on Enterprise Edition.  We are actively looking at upgrading to Unlimited Edition though.  Our requirements keep hitting "can't do it without Apex" brick walls so I am fairly sure we will go that route.
 
However, until I can convince the CFO to spend the cash, is there a way to do this in Enterprise Edition (even if it is pressing a button that creates the related objects)?
 
Thanks for your help so far.
 
Regards,
Gordon.
Eyal_WizEyal_Wiz
Gordon,
 
For doing that with a button you should create a S-control, see the example of Steve , he gave there a good explanation of what code you should write in order to add an object.
 
Code:
<script language="JavaScript"> 
function redirect()
{
parent.frames.location.replace( "/a0L/e?retUTL=/{opportunity.id}"
+"&CF00N60000001KPqb={!opportunity.Name}" );
}
redirect();
</script>

 
you should replace the "ao3" with the code of the object you want to create (for finding the code for the object you want you should click "view source" when you're in update mode inside the object you want to create, you should see there a list of codes)
The "CF00N60000001KPqb" is the ID of the field that you want to update. you should also find that code in the html page i mentioned above.

The only thing you should add in your case is an "if statment" to check the condition you've mentioned.
 
for that you should write:
 
if ({!Opportunity.Status} = THE_STATUS_THAT_YOU_CHOOSE)
{
   redirect();
}
 
I hope my english was clear enough, if not, try reading steve explenation - it much clearer then mine.
 
after you finish writing the s-control you should create a new button in the opportunity object- the new button should call the s-control you created. don't forget to place the button on the opportunity layout screen.
 
 
Good  Luck,
Eyal
SteveBowerSteveBower

Well, with Apex it's easy and that would be the pretty way to go.  Albeit expensive.

I don't think there's is a simple, clean way to override Save and interject your own behaviors and then return to the default Save behavior.

You could write your own s-control to do the entire job of editing the fields, then you can obviously do anything you want and save the data yourself, but that's way overkill and far too much work to be practical.

You could write an administrative s-control that only the admin would run (like once a week or day, etc.) which checks to see if any "Sold" stage opportunities do not have a Delivery project and create them as needed.

You could create an in-line s-control which checks the Stage and if it's equal to "Sold" goes and checks for a Delivery Project, creating one if needed.  However, that relies on the user allowing the s-control to finish, not closing the window or navigating away, etc.  Not hugely reliable, but perhaps doable.   It's also slow because every time anybody brings up an opportunity to view it would do this check.  (You could of course try to optimize it, perhaps first check if the time last modified is within the last 10 seconds, and only do the rest of the checks if it is, etc.)

No clean answers.

Perhaps you can accomplish something via an obvious message to your users?   For example, you could create an in-line s-control to be displayed in the Opportunity page layout.  This s-control simply looks at the status of the Stage, and if it's "Sold" displays:  'Remember to create a Delivery Project if you just marked this as "Sold" ' in glowing red flaming letters! :-)

Apex is the best answer... that's why they charge for it.   I always find it interesting when SFDC says "yes, our platform can be used as a database", when they charge extra for one of the most basic database features... triggers.

Best, Steve.

SteveBowerSteveBower
I should have mentioned that creating a "Create Delivery Project" s-control which is presented as a button or a custom link on the Opportunity Detail page is pretty trivial.  I assumed you knew that but were asking for a way to automate it.

If you did this, then at least you would be making the job far easier for your users, they could just click on the button (which could verify that it's "Sold" and no pre-existing Delivery Project object is already associated with this Opportunity), and it would create it for the users.

If the user doesn't have to enter any data, then they are done.  If they do need to enter data, then the s-control could bring them directly to the Edit page for the newly created Deliver Project object.  

That's very similar to the s-control code posted earlier in this thread.

Best, Steve.

brunol11brunol11

Hi Eyal!

 

I Wonder if you could help me on this. I Found your message here and is exactly what I´m trying to do.

 

I Have a custom object named "Contratacao__c" and when the field "Status_do_Contrato__c" is filled with the value 'Emitir Boletos' it was suposed to create a new record on the related custom object "Faturamento__c".

 

But after insert or update a "Contratacao__c" record, the trigger below returns the following error:

 

Erro:O acionador CreateBilling do Apex causou uma exceção inesperada. Entre em contato com o administrador: CreateBilling: execution of AfterUpdate caused by: System.StringException: Invalid id: 0001282: Trigger.CreateBilling: line 8, column 36

 

Which means that the "Faturamento__c.Contrato__c" field related to the "Contratacao__c.name" is not filled with the right value. 

In the above example, I have a record in "Contratacao__c" registered as "0001282" and the related record "Faturamento__c" should have a field "Faturamento__c.Contrato__c" filled with the same "0001282"

 

Faturamento__c.Contrato__C: Relationship field (Master and Detail)

Contratacao__c.name = automatica numbered

 

What am I doing wrong? How to Fix it?

 

Here´s the code:

 



trigger CreateBilling on Contratacao__c (after update, after insert) {

Contratacao__c[] Contratos = Trigger.new;

 if (Contratos[0].Status_do_Contrato__c == 'Emitir Boletos')
      {
        Faturamento__c Billing = new Faturamento__c (
        Contrato__c = Contratos[0].name,
        F__c = Contratos[0].Total_do_Contrato__c,
        V__c = Contratos[0].Nascimento__c);
        insert Billing;
      }
 }