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
Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan) 

How to create a custom button on one object that inserts record in other object?

 

I have 2 custom objects Client Versions and QA Release. I would like to create a custom button on QA Release which when clicked creates a new record on Client Versions using data from the fields on QA Release.

Can anyone please give me a sample code for this? It can be URL hacking or Javascript.



Thanks!
Mayank

Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)

 

Creating a master detail relationship between those 2 objects is not an option because the child object has like 100 records and I'll have to populate the lookup field with data for each of those records.

 

Is there a way to do this without creating a master detail relationship?

Avidev9Avidev9
Its not necessay to have a MD relationship that was just a demo
Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)

 

I decided to use Javascript code instead but I see the following error:

 

"A problem with the onclick javascript for this button or link was encountered.

Unexpected identifier"

 

Can anyone please tell me that what's wrong with the following?

 

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

getDate = function(dateObj)
{
var day = dateObj.getDay() < 9 ? '0'+dateObj.getDay() : dateObj.getDay();
var month = dateObj.getMonth() < 9 ? '0'+dateObj.getMonth() : dateObj.getMonth();

return dateObj.getFullYear()+'-'+month+'-'+day;
}

var oppty = new sforce.SObject('Client Version');

oppty.Name ='{!QA_Release__c.Client__c} - {!QA_Release__c.Client_Site_Type__c} - {!QA_Release__c.Release_Version__c}';
oppty.Account= {!QA_Release__c.Client__c};
oppty.Environment= {!QA_Release__c.Environment__c};
oppty.Sentry_Version_Number = {!QA_Release__c.Branch__c};
oppty.Sentry_Branch_Version = {!QA_Release__c.Release_Version__c};
oppty.Installation_Type= {!QA_Release__c.Client_Site_Type__c};
oppty.Delivery_Date= {!QA_Release__c.Delivered_Date__c};
oppty.Install_Date= {!QA_Release__c.Delivered_Date__c};

 

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

if(result[0].success == 'true')
{
alert('A New Client Versions record with Name - ' + oppty.Name + ' was Created Successfully.');
}

Avidev9Avidev9
Enclose all the merge fields in single quotes.
(only for the section where you are doing the assignment to oppty)
Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)

I get the following error message now.

 


salesforce {faultcode:'soapenv:Client', faulstring:"6/6/2013' is not a valid value for the type xsd:date',}

 

Code:

 

{!REQUIRESCRIPT("/soap/ajax/16.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/16.0/apex.js")} 


var oppty = new sforce.SObject('Client_Version__c'); 

oppty.Name ='{!QA_Release__c.Client__c} - {!QA_Release__c.Client_Site_Type__c} - {!QA_Release__c.Release_Version__c}'; 
oppty.Account__c = '{!QA_Release__c.Client__c}'; 
oppty.Environment__c = '{!QA_Release__c.Environment__c}'; 
oppty.Sentry_Version_Number__c = '{!QA_Release__c.Branch__c}'; 
oppty.Sentry_Branch_Version__c = '{!QA_Release__c.Release_Version__c}'; 
oppty.Installation_Type__c = '{!QA_Release__c.Client_Site_Type__c}'; 
oppty.Delivery_Date__c = '{!QA_Release__c.Delivered_Date__c}' 
oppty.Install_Date__c = '{!QA_Release__c.Delivered_Date__c}' 



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

if(result[0].success == 'true') 
{ 
alert('A New Client Versions record with Name - ' + oppty.Name + ' was Created Successfully.'); 
}

Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)

Thanks a lot for that. Atleast now it doesn't throw that error.

 

But the code is not doing what it's intended to do. I'm very new to salesforce and Javascript programming so I have no clue about how to debug the code. I click on the custom button and it does nothing.

 

{!REQUIRESCRIPT("/soap/ajax/16.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/16.0/apex.js")}

 
var oppty = new sforce.SObject('Client_Version__c');
var today=new Date();

oppty.Name ='{!QA_Release__c.Client__c} - {!QA_Release__c.Client_Site_Type__c} - {!QA_Release__c.Release_Version__c}';
oppty.Account__c = '{!QA_Release__c.Client__c}';
oppty.Environment__c = '{!QA_Release__c.Environment__c}';
oppty.Sentry_Version_Number__c = '{!QA_Release__c.Branch__c}';
oppty.Sentry_Branch_Version__c = '{!QA_Release__c.Release_Version__c}';
oppty.Installation_Type__c = '{!QA_Release__c.Client_Site_Type__c}';
oppty.Delivery_Date__c = sforce.internal.dateTimeToString(today);
oppty.Install_Date__c = sforce.internal.dateTimeToString(today);



result = sforce.connection.create([oppty]);
 
if(result[0].success == 'true')
{
    alert('A New Client Versions record with Name - ' + oppty.Name + ' was Created Successfully.');
}

 

Mayank Srivastava (Salesforce fan)Mayank Srivastava (Salesforce fan)

 I guess I'll revert to URL hacking in case this doesn't work.

Avidev9Avidev9
that was not a solution.

You just replaced the stuf with the link I gave. Please adjust the code according to your need.

Remove these lines it will work.
(But it wont be setting the these two fields)
oppty.Delivery_Date__c = sforce.internal.dateTimeToString(today);
oppty.Install_Date__c = sforce.internal.dateTimeToString(today);
Problem is with these two fields

Avidev9Avidev9
URL hacking the best way and the blog a sent you says it all.
Raj Kumar 88Raj Kumar 88
Hi Avidev9,

I am doing the same thing, if i am using Name field only then it is working fine but 
if i put lookup field then it is not working.

Customer_Name__c - Look Up field - Contact Lookup
Requested_By__c- Look Up Field - Contact Lookup

{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/23.0/apex.js")} 


var oppty =  new sforce.SObject('SalesOrder__c');
oppty.Name = '{!LeadGen__c.Name}'; - Working fine if i assign Name field only
oppty.Customer_Name__c = '{!LeadGen__c.Account_NameId__c}'; -- Look up Field 
oppty.Requested_By__c = '{!LeadGen__c.Contact_PersonId__c}'; -- Look up Field
result = sforce.connection.create([oppty]);

I am newbee in this, please help to resolve this issue.

Thanks in advance
Vikash