Stack Exchange
Error to populate lookup field onclick javascript
I have below code which throws an error to populate lookup field ID value from one object to another. It unexpectedly tries to assign Name of Account object instead of ID. So it throws error. Can someone tell me why?
Javascript Code
 {!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 
//identify parent record

var FormR= new sforce.SObject("Form_Requests_abv__c");
FormR.id = '{!Form_Requests_abv__c.Id }';
FormR.name = '{!Form_Requests_abv__c.Name}';


// insert Sample Form transactions
var SampleFormTrans= new sforce.SObject("Sample_Order_Form_abv__c");

//SampleFormTrans.Account_abv__c = "{!Form_Requests_abv__c.Account_abv__c}";
var s= document.getElementById('{!Form_Requests_abv__c.Account_abv__c}');
SampleFormTrans.Account_abv__c = s;
var add= document.getElementById('{!Form_Requests_abv__c.Address_abv__c}');
//SampleFormTrans.Address_abv__c= '{!Form_Requests_abv__c.Address_abv__c}';
SampleFormTrans.Address_abv__c= add;
var FR= document.getElementById('{!Form_Requests_abv__c.Id}');
SampleFormTrans.Form_Request_abv__c= FR;
//SampleFormTrans.Form_Request_abv__c= '{!Form_Requests_abv__c.Id}';
SampleFormTrans.OwnerID= '{!Form_Requests_abv__c.OwnerId}';
SampleFormTrans.Quantity_abv__c= '{!Form_Requests_abv__c.Quantity_abv__c}';
//SampleFormTrans.Product_abv__c='{!Form_Requests_abv__c.Product_ID_abv__c}';
//SampleFormTrans.Form_template_abv__c='{!Form_Requests_abv__c.Form_Template_Name_abv__c}';
var FT= document.getElementById('{!Form_Requests_abv__c.Form_Template_Name_abv__c}');
SampleFormTrans.Form_Template_Name_abv__c= FT;

//get query
var query = "SELECT  Account_abv__c ,Address_abv__c,Form_Request_abv__c, "+
"OwnerID, Quantity_abv__c, Product_abv__c, Form_template_abv__c, Id FROM "+
"Sample_Order_Form_abv__c WHERE Form_Request_abv__c='{!Form_Requests_abv__c.Id}'"; 
var records = sforce.connection.query(query); 
alert(records);
var records1 = records.getArray('records');
alert(records1);
if(records[0]==null)
    {alert('New record to insert');
var result = sforce.connection.create([SampleFormTrans]);}
if(result[0].getBoolean("success"))
{
   alert('Sample Form Transaction inserted successfully');
   window.location.reload();
}
else{
  alert('Error : '+result);
}

var formId = '{!Sample_Order_Form_abv__c.Id}'; 
var expired = '{!Sample_Order_Form_abv__c.Expired_abv__c}'; 
var templateId = '{!Sample_Order_Form_abv__c.Form_template_Id_abv__c}'; 
if(expired == '1'){ 
alert("Form expired. Print option not available."); 
}else{ window.open('/apex/FormTemplatePrinter?ids='+formId+'&TemplateId='+templateId,'_blank'); 
}

Error
Successfully created but all lookup field values are blank.

what error you are getting?

@Ratan Hi,Please find the error message in updated question.

try this  var query = "SELECT  Account_abv__c ,Address_abv__c,Form_Request_abv__c, OwnerID, Quantity_abv__c, Product_abv__c, Form_template_abv__c, Id FROM Sample_Order_Form_abv__c WHERE Form_Request_abv__c='{!Form_Requests_abv__c.Id}'";

This was throwing errors due to line breaks

@Ratan  If you see the error message it throws because it is retrieving Account Name instead of Account ID by Account_abv__c field.

I suggest first execute your query in developer console.. then created SOQL in javascript

Please find my updated code it is not throwing any error , allowing records to get created. But Lookup IDs are not getting stamped and showing up blank for newly inserted records.

Let us continue this discussion in chat.

Remove all document.getElementById('{!Form_Requests_abv__c.Form_Template_Name_abv__c}');
and add like this
SampleFormTrans.Product_abv__c='{!Form_Requests_abv__c.Product_ID_abv__c}';

this will contain your id
So just assign the Id
Like this
var SampleFormTrans= new sforce.SObject("Sample_Order_Form_abv__c");

SampleFormTrans.Account_abv__c = "{!Form_Requests_abv__c.Account_abv__c}";

SampleFormTrans.Address_abv__c= '{!Form_Requests_abv__c.Address_abv__c}';


SampleFormTrans.Form_Request_abv__c= '{!Form_Requests_abv__c.Id}';

SampleFormTrans.OwnerID= '{!Form_Requests_abv__c.OwnerId}';

SampleFormTrans.Quantity_abv__c= '{!Form_Requests_abv__c.Quantity_abv__c}';
SampleFormTrans.Product_abv__c= '{!Form_Requests_abv__c.Product_ID_abv__c}';
SampleFormTrans.Form_template_abv__c='{!Form_Requests_abv__c.Form_Template_Name_abv__c}';

After discussion with OP Found he was using wrong Field API Name..
var SampleFormTrans= new sforce.SObject("Sample_Order_Form_abv__c"); 

SampleFormTrans.Account_abv__c = '{!Form_Requests_abv__c.Account_abvId__c}'; 

SampleFormTrans.Address_abv__c= '{!Form_Requests_abv__c.Address_abvId__c}'; 


SampleFormTrans.Form_Request_abv__c= '{!Form_Requests_abv__c.Id}'; 

SampleFormTrans.OwnerID= '{!Form_Requests_abv__c.OwnerId}'; 

SampleFormTrans.Quantity_abv__c= '{!Form_Requests_abv__c.Quantity_abv__c}'; 

SampleFormTrans.Form_template_abv__c='{!Form_Requests_abv__c.Form_Template_Name_abvId__c}'; 

