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
jsacpt24jsacpt24 

custom button to pull from Lead to custom object

I am trying to create a custom button that works for multiple different objects to all come back to my Data_Feasibility_Report__c custom object. I was trying to create the button to pull from the Lead standard object but I keep gettting an error that I am not sure how to fix. Any help would be appreciated. Here is the code that I am trying to write. I am trying to pull the Name from the Lead and the record type as well as a couple custom fields. When I go test the button I get "Invalid or unexpected token".
 
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
 
var DFR = new sforce.SObject("Data_Feasibility_Request__c"); 
DFR.Related_To__c = 'Lead';
DFR.Related_Lead__c = '{!Lead.Name}';
DFR.RecordID=0125C00000005jd;

var result = sforce.connection.create([DFR]);
 
if(result[0].getBoolean("success")){
   alert('New record updated successfully');
}
else{
  alert('Error : '+result);
}

I tried this another way but I am getting an error the second way I tried as well. Any help on either of these would be appreciated. 
 
{!REQUIRESCRIPT('/soap/ajax/37.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('Data_Feasibility_Report__c'); 

oppty.Related_Lead__c = '{!Lead.Name}; 
oppty.Related_To__c = 'Lead'; 
RecordType=0125C00000005jd; 

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

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

 
Tejas KardileTejas Kardile
Hi,

Here is your working code:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var DFR = new sforce.SObject("Data_Feasibility_Request__c");  
DFR.Related_To__c = 'Lead';
DFR.Related_Lead__c = '{!Lead.Name}';
DFR.RecordID__c='0125C00000005jd'; 
result = sforce.connection.create([DFR]);
if(result[0].getBoolean("success")){
   alert('New record updated successfully');
}
else{
  alert('Error : '+result);
}

let me know if your issue is not resolve

Thanks
jsacpt24jsacpt24
I put that code in and now I am getting this error. 
 
A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'sf:INVALID_FIELD', faultstring:'INVALID_FIELD: No such column 'RecordID__c' on entity 'Data_Feasibility_Request__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.', detail:{InvalidFieldFault:{exceptionCode:'INVALID_FIELD', exceptionMessage:'No such column 'RecordID__c' on entity 'Data_Feasibility_Request__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.', row:'-1', column:'-1', }, }, }

 
Tejas KardileTejas Kardile
Hi, 

Put below code and if its woking then mark it as a best answer
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var DFR = new sforce.SObject("Data_Feasibility_Request__c");  
DFR.Related_To__c = 'Lead';
DFR.Related_Lead__c = '{!Lead.Name}';
DFR.RecordID ='0125C00000005jd'; 
result = sforce.connection.create([DFR]);
if(result[0].getBoolean("success")){
   alert('New record updated successfully');
}
else{
  alert('Error : '+result);
}

Thanks
jsacpt24jsacpt24
I was still getting some errors on it so I removed the RecordID as well as the DFR.Related_Lead__c = '{!Lead.Name}'; . It allowed me to create but I was hoping for it to do something a little different. I was hoping that once you clicked the JS button it would open the edit page. Is there something that I am missing to make that happen? 
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 
var DFR = new sforce.SObject("Data_Feasibility_Request__c"); 
DFR.Related_To__c = 'Lead'; 
result = sforce.connection.create([DFR]); 
if(result[0].getBoolean("success")){ 
alert('New record updated successfully'); 
} 
else{ 
alert('Error : '+result); 
}

 
Tejas KardileTejas Kardile
Hi,

I have created same scenario in my dev org and its working fine for me, below is source code:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var DFR = new sforce.SObject("Data_Feasibility_Request__c");  
DFR.Related_To__c = 'Lead';
DFR.Related_Lead__c = '{!Lead.Name}';
DFR.RecordTypeID='012900000016O3hAAE';  //Please make this record type ID should be present in Data_Feasibility_Request__c object
result = sforce.connection.create([DFR]);
if(result[0].getBoolean("success")){
   alert('New record updated successfully');
}
else{
  alert('Error : '+result);
}

If its work for you then mark it best solution.

Thanks,
Tejas
Tejas KardileTejas Kardile
Correcting typo,
**Please make sure this record type ID should be present in Data_Feasibility_Request__c object
Tejas KardileTejas Kardile
Hi,

Just wanted to check, is your issue resolved?

Thanks,
jsacpt24jsacpt24
If you refer back to the comment that I had listed last the code worked but is not exactly what I am looking for. Do you know a way to have the button open the edit page in the new object?