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
JodieMJodieM 

Querying data from one object to update on another

Hi, I'm new to Salesforce development and don't know enough about Javascript.

 

I have taken an example of Javascript to update a Case Record and it works with a text value. I would like to update the case with a value queried from another object (there is no relationships between the two objects so it can't be done via a workflow).

 

I am pretty sure the error in my code is something to do with Javascript Arrays because I can verify that the Query on my lookup Object is returning a value.

 

Could someone please help me out and point me in the right direction with how to get the value out of the query to use in the update of the Case.

 

Thanks

 

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

var updateRecord = new Array(); 
var myquery = "SELECT Id FROM Case WHERE Id = '{!Case.Id}' limit 1";

result = sforce.connection.query(myquery);
records = result.getArray("records");

var lkupquery = "Select LookupValue__c from CustomObject__c where OwnerId = '{!$User.Id}' and Value__c < 10 limit 1";
var lkupresult = sforce.connection.query(lkupquery);
var lkuprecords = lkupresult.getArray("lkuprecords");

if (lkuprecords.length == 1) {
  var LkupVal = lkuprecords[0];
} else {
  alert("Something went wrong here");
}

if(records[0])
{
    var update_Case = records[0];
    update_Case.MyTextField__c = LkupVal;
    updateRecord.push(update_Case);
}

result = sforce.connection.update(updateRecord);
parent.location.href = parent.location.href;

My problem starts with the line "var lkuprecords = lkupresult.getArray("lkuprecords");" 

 

I am trying to update the case field MyTextField__c with the LookupValue__c from the CustomObject__c

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

Hey , 

I just twik the variable declaration,

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

var updateRecord = new Array(); 
var myquery = "SELECT Id FROM Case WHERE Id = '{!Case.Id}' limit 1";

result = sforce.connection.query(myquery);
records = result.getArray("records");
var LkupVal = '';
var lkupquery = "Select LookupValue__c from CustomObject__c where OwnerId = '{!$User.Id}' and Value__c < 10 limit 1";
var lkupresult = sforce.connection.query(lkupquery);
var lkuprecords = lkupresult.getArray("lkuprecords");

if (lkuprecords.length == 1) 
{
  LkupVal = lkuprecords[0];
} else {
  alert("Something went wrong here");
}

if(records[0])
{
    var update_Case = records[0];
    update_Case.MyTextField__c = LkupVal;
    updateRecord.push(update_Case);
}

result = sforce.connection.update(updateRecord);
parent.location.href = parent.location.href;

 

 

Thanks,

bForce

All Answers

b-Forceb-Force

Hey , 

I just twik the variable declaration,

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

var updateRecord = new Array(); 
var myquery = "SELECT Id FROM Case WHERE Id = '{!Case.Id}' limit 1";

result = sforce.connection.query(myquery);
records = result.getArray("records");
var LkupVal = '';
var lkupquery = "Select LookupValue__c from CustomObject__c where OwnerId = '{!$User.Id}' and Value__c < 10 limit 1";
var lkupresult = sforce.connection.query(lkupquery);
var lkuprecords = lkupresult.getArray("lkuprecords");

if (lkuprecords.length == 1) 
{
  LkupVal = lkuprecords[0];
} else {
  alert("Something went wrong here");
}

if(records[0])
{
    var update_Case = records[0];
    update_Case.MyTextField__c = LkupVal;
    updateRecord.push(update_Case);
}

result = sforce.connection.update(updateRecord);
parent.location.href = parent.location.href;

 

 

Thanks,

bForce

This was selected as the best answer
JodieMJodieM

Thanks, I'm not sure if it was the variable naming, or something else, but I finally worked through it and got it working. 

 

Here is what I did. 

 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
var LkupVal = '';
var updateRecord = new Array(); 
result = sforce.connection.query("Select LookupValue__c from CustomObject__c where OwnerId = '{!$User.Id}' and Value__c < 10 limit 1");
records = result.getArray("records");

if (records.length == 1) {
    var record = records[0];
    LkupVal = (record.LookupValue__c);
} else {
  alert("Something went wrong here");
}

var myquery = "SELECT Id FROM Case WHERE Id = '{!Case.Id}' limit 1";
result = sforce.connection.query(myquery);
records = result.getArray("records");
if(records[0])
{
    var update_Case = records[0];
    update_Case.MyTextField__c = LkupVal;
    updateRecord.push(update_Case);
}
else
{
alert("No current case");
}

result = sforce.connection.update(updateRecord);
parent.location.href = parent.location.href;

 I'm sure it is terrible code, but it does the job.