2

I am trying to create a custom button on a custom object which can only be clicked if a date on the parent object is filled in. I am new to SOQL and javascript but have managed to created custom buttons in the past with validation rules which related to the object they are on however I am struggling with cross object rules.

In this instance an Assessment__c can only be submitted if it's parent Referral__c has an accepted date that is not blank.

The SOQL query is fine, the error is when I try to access that field "var accDate = records[0].Referral__r.Accepted_Date__c; " this is coming up as

undefined

.

Any help would be very much appreciated.

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

var ReferraAccepted = sforce.connection.query("SELECT Referral__r.id, Referral__r.Accepted_Date__c from Assessment__c WHERE id ='{!Assessment__c.Id}' LIMIT 1");

var records = ReferraAccepted.getArray("records"); 

var accDate = records[0].Referral__r.Accepted_Date__c; 

alert('::accDate :::'+ records +':::');

if(accDate ==null){alert ("YO IT WORKED!!!! Please submit the completed referral form to the Clearing House as this form cannot be submitted on it's own.")} 


else {

var newRecords = []; 
var c = new sforce.SObject("Assessment__c"); 
c.id ="{! Assessment__c.Id }"; 
c.Submit_for_assessment__c = true; 
newRecords.push(c); 
result = sforce.connection.update(newRecords); 
window.location.reload();

}
1
  • Is the records variable undefined too? Jul 14, 2015 at 14:22

2 Answers 2

1

When you defined var ReferraAccepted as:

var ReferraAccepted = sforce.connection.query("SELECT Referral__r.id, Referral__r.Accepted_Date__c from Assessment__c WHERE id ='{!Assessment__c.Id}' LIMIT 1");

because of the 'limit 1' in your query, you were defining a single object record, not an array.

So when you do the line below, you're returning a record, not an array of records as you often would be with many queries:

var records = ReferraAccepted.getArray("records"); 

Which then leads us to the problematic line:

var accDate = records[0].Referral__r.Accepted_Date__c; 

Since records does not return an array, the above should actually be:

var accDate = records.Referral__r.Accepted_Date__c;`
0

Unfortunately the solution to my question was very unexciting. It was that Refferal__r.Accepted_Date__c should have been Refferal__r.Accepted_date__c. I didn't realise that the field names were case sensitive.

Thanks for everyone's help.

You must log in to answer this question.

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