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
IZavalunIZavalun 

Error Updating the lookup field in Account object

I am trying to update the custom lookup field 'Primary_Distributor_c' in Account object and first of all my '_id' is null but when I run the query I have always 1 record back with Id
 
If you will see my problem here please support.
 
Here is my code:
 
==============================================
function pd_update(_name){
alert("Got Here '"+_name+"'");
var qrdr = sforce.connection.query("select Id from Account where RecordTypeId='012600000004w6TAAQ' and Name = '"+_name + "'");
var records = qrdr.getArray("records");
alert("Found " + records.length + " Records...");
if (records.length > 0){
        _id=qrdr.records[0].Id;
 }
if (_id) {
  var _IsMatch = new sforce.SObject("Account");
  _IsMatch.Id = _id;
  _IsMatch.Primary_Distributor__c = _name;
   
  var updateIsMatch = sforce.connection.update([_IsMatch]); //actually perform update
  if (updateIsMatch[0].getBoolean("success")) { //if successful update of existing record
   alert("PD Successfully updated!"); //indicate that the update was successful
     } else {
   alert("Error:\n"+updateIsMatch[0]); //display the error
  }
 }
}
DevAngelDevAngel
Ids must be ids, not names.  CustomObject__c field is an Id.  Try using the account Id that you returned from the query.
AhlomonAhlomon
Would you please show how to update the code...
IZavalunIZavalun

Thanks.

Using the '_id' instead of '_name' did the job.

HarmpieHarmpie

The old code would probably have worked fine if you would have replaced

Code:

 _id=qrdr.records[0].Id;


 

with

Code:

 _id=records[0].Id;