I am trying to get the ID of a lookup field so I can populate a field on a new record that is created when an Onclick Javascript button is clicked. Basically I need the ID of Birth_Record_c.Dame_c instead of the value so I don't get the Malformed ID error. The line that's causing the error is

dog.Dame__c = "{!Birth_Record__c.Dame__c}";. 



var pupIn = "{!Birth_Record__c.dog_Name2__c}";    
if (pupIn != "") {    
  alert("A dog record has already been created for this birth record. Please click the link next to Puppy Name to view that record.")    
  var dog = new sforce.SObject("Dog__c");    
  dog.Name = "{!Birth_Record__c.Name}";    
  dog.sex__c = "{!Birth_Record__c.Gender__c}";    
  dog.Dame__c = "{!Birth_Record__c.Dame__c}";    
  var by = "{!YEAR( DATEVALUE(Birth_Record__c.Birth__c) )}";    
  var bm = "{!MONTH( DATEVALUE(Birth_Record__c.Birth__c) )}";    

  if (bm.length == 1){    
    bm = "0"+bm;    

  var bd = "{!DAY( DATEVALUE(Birth_Record__c.Birth__c) )}";    
  if (bd.length == 1){    
    bd = "0"+bd;    

  var b = by+"-"+bm+"-"+bd;    
  dog.birthdate__c = b;    
  var result = sforce.connection.create([dog]);

    var nextPg = result[0].id;    
    var newRecords = [];    
    var c = new sforce.SObject("Birth_Record__c");    
    c.id ="{!Birth_Record__c.Id}";    
    c.dog_Name2__c = result[0].id;    
    result = sforce.connection.update(newRecords);    
    window.location = "/" + nextPg;    
    alert('Could not create record '+result);    
  • 2
    Did you try: "{!Birth_Record__c.Dame__r.Id}"
    – mulveling
    Commented Sep 25, 2013 at 18:05
  • That should work, or maybe "{!Birth_Record__c.DameId__c}" Commented Sep 25, 2013 at 21:43
  • Adding Id to either place came back with a syntax error saying the field does not exist.
    – Jennifer
    Commented Sep 26, 2013 at 14:21
  • wtf wtf wtf wtf Commented Oct 3, 2017 at 6:15

2 Answers 2


I figured it out, you need to add 'Id' to the end of your field before '__c'. Here an example of my code (each of these fields is a lookup field other than the project ID):


var newsurvey = new sforce.SObject("Custom_Object__c"); 

newsurvey.Project__c = "{!Proj__c.Id}"; 

newsurvey.Opportunity__c = "{!Proj__c.OpportunityId__c}"; 

newsurvey.Region__c = "{!Proj__c.RegionId__c}"; 

newsurvey.Contact__c = "{!Proj__c.Survey_ContactId__c}"; 

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

  • Your solution worked for me - i do get to know with each passing day something new about Salesforce ! Thanx
    – owsata
    Commented Nov 11, 2016 at 9:28

So I came up with a quick work around. I just made a formula text field and inserted the field Birth Record > Dame > Record ID. Then I referred to that formula field to populate the lookup field on the other object.

So the code ended up being dog.Dame__c = "{!Birth_Record__c.Dame_ID__c}";.

Thanks for the help!

  • This does not work for everyone as some people may have reached limits on their object.
    – Chris
    Commented Mar 10, 2014 at 16:44
  • I still look for the "correct" solution to this problem every couple of months and have yet to find an answer.
    – Jennifer
    Commented Oct 12, 2015 at 18:41

You must log in to answer this question.

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