You need to sign in to do that
Don't have an account?
DPhil
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!
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!
Thanks,
N.J
All Answers
Thanks,
N.J
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
Proj.Date_Signed__c = {!Opportunity.Date_Signed__c};
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
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
var d = new Date('{!Opportunity.Date_Signed__c}')
Thanks,
N.J
Thanks,
N.J