6

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}";. 

Code:

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

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.")    
}else{    
  var dog = new sforce.SObject("Dog__c");    
  dog.Name = "{!Birth_Record__c.Name}";    
  dog.sex__c = "{!Birth_Record__c.Gender__c}";    
  alert("{!Birth_Record__c.Dame__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]);

  if(result[0].getBoolean("success")){    
    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;    
    newRecords.push(c);    
    result = sforce.connection.update(newRecords);    
    window.location = "/" + nextPg;    
  }else{    
    alert('Could not create record '+result);    
  }    
}
4
  • 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

7

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):

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

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]); 

alert(result);
1
  • 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
1

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!

2
  • 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 .