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();
}