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
DPhilDPhil 

Custom button javascript code

I'm trying to use a custom button to create a new record in a custom object, but the code is failing, I would appreciate some advice?

I'm new to this and can't find a good reference to the Javascript toolkit/api, so I'm reduced to Googling for answers and can't figure it out.

I have created a custom object called "Project" with an api name of "Porduction__c".

I want to add a custom button to the Opportunity page to create a new Project record and pre-populate it with some fields from the Opportunity.  Then it takes the user to the record so they can edit and save it.

I did have it working, but then I added a master-detail relationship between Project and Account, plus a lookup field relationship between Project and Opportunity, after which it stoped working.

I have added the required fields to the new record object, so I don't think that's the issue.

I can't work out how to get a meaningful error message.

Here is the code:

==========

{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')}

var Proj = new sforce.SObject("Production__c");

Proj.Name = "{!Opportunity.Name}";
Proj.Account_Name__c = "{!Opportunity.Account}";
Proj.Opportunity_Name__c = "{!Opportunity.Name}";

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

if(result[0].success == 'true'){
    window.location = "/" + result[0].id + "/e";}
else {alert("Error creating Project");}

=============

Any help much appreciated!
Best Answer chosen by DPhil
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Please check if this works fine :

{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')}

var Proj = new sforce.SObject("Production__c");

Proj.Name = "{!Opportunity.Name}";
Proj.Account_Name__c = "{!Opportunity.AccountId}";
Proj.Opportunity_Name__c = "{!Opportunity.Id}";

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

if(result[0].success == 'true'){
    window.location = "/" + result[0].id + "/e";}
else {alert("Error creating Project");}


Thanks,
N.J

All Answers

Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Please check if this works fine :

{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')}

var Proj = new sforce.SObject("Production__c");

Proj.Name = "{!Opportunity.Name}";
Proj.Account_Name__c = "{!Opportunity.AccountId}";
Proj.Opportunity_Name__c = "{!Opportunity.Id}";

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

if(result[0].success == 'true'){
    window.location = "/" + result[0].id + "/e";}
else {alert("Error creating Project");}


Thanks,
N.J
This was selected as the best answer
DPhilDPhil
Fantastic, that works, thanks!

A bit cheeky, but there's another thing that doesn't work.

I'm trying to also copy a custom date value from Opportunty to the Project record, but it fails with an error: "[faultcode:'soapenv:Client',
faultstring:"0.0022343594836146973' is not a valid value for the type xsd:date', }".

I've tried all sorts of things to format the date etc., but nothing works.

Any ideas?

Thanks
DPhilDPhil
Sorry, missed the line of code which is added under the field assignments:

Proj.Date_Signed__c = {!Opportunity.Date_Signed__c};
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
hi David,

Chek if this works :

var d = new Date({!Opportunity.Date_Signed__c})
Proj.Date_Signed__c = d ;

If this anwers your question, mark this as best answer.
thanks,
N.J
DPhilDPhil
Hi NJ

Getting there - it no longer errors, but the date inserted is 01/01/1970.

I have checked and there is a valid date in the Opportunity, and the fieldname is correct.

Dave
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Please add it in singel quotes and check.

var d = new Date('{!Opportunity.Date_Signed__c}')

Thanks,
N.J
DPhilDPhil
Tried that, it gave a fault code saying it was not a valid value again, but this time the value was "NaN-NaN... repeated lots.
DPhilDPhil
I've also tried double quotes, but same result :-(

DPhilDPhil
I will post this as a separate question, thanks for your help.
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
It is working fine for me with single quote for different objects.

Thanks,
N.J
DPhilDPhil
It definitely gives me "invalid date" as the value of "d" when I use single quotes.  Without the quotes it gives a long date and time string as the value, but then fails to create the record with that value.