0

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.

8
  • what error you are getting?
    – Ratan Paul
    Commented Feb 23, 2016 at 8:58
  • @Ratan Hi,Please find the error message in updated question. Commented Feb 23, 2016 at 9:02
  • 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}'";
    – Ratan Paul
    Commented Feb 23, 2016 at 9:07
  • This was throwing errors due to line breaks Commented Feb 23, 2016 at 9:11
  • 1
    I suggest first execute your query in developer console.. then created SOQL in javascript
    – Ratan Paul
    Commented Feb 23, 2016 at 9:18

1 Answer 1

1

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}'; 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .