You need to sign in to do that
Don't have an account?
Mike@Mungos
SOQL query on custom javascript button in order to create a validation rule on related object
Hi,
I am trying to create a custom button on a custom object which can only be clicked if a date on the parent oject 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.
Any help would be very much apprecitated.
Here is the code so far:
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var ReferraAccepted = "SELECT Accepted_date__c FROM Referral__c WHERE Name = '{!Assessment__c.Referral__c}' LIMIT 1";
if (ReferraAccepted == null){alert ("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();
}
I am trying to create a custom button on a custom object which can only be clicked if a date on the parent oject 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.
Any help would be very much apprecitated.
Here is the code so far:
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var ReferraAccepted = "SELECT Accepted_date__c FROM Referral__c WHERE Name = '{!Assessment__c.Referral__c}' LIMIT 1";
if (ReferraAccepted == null){alert ("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();
}
Hi Mike,
Try this code -->>
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var ReferraAccepted = sforce.connection.query("SELECT Referral__r.Accepted_Date__c from Assessment__c where id ='{!Assessment__c.Id}' limit 1");
records = ReferraAccepted.getArray("records");
var accDate = records[0].Referral__r.Accepted_Date__c;
if(accDate==null){
alert("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();
}
Let me know if this works for you..!!
All Answers
Any advice?
You have your button on Assessment__c object so you can simply query from child to parent like -->
"SELECT Referral__r.Accepted_Date__c from Assessment__c where id ='{!Assessment__c.Id}' limit 1";
When we query from child to parent we append '__r'(denotes relationship) after the lookup field name to get access to fields in the parent record.
So, you can modify this query as per your needs.
In addition to this you'll need to use sforce.connection.query.sforce.connection.query request returns an object with a "records" field that is an array of the result SObjects and you can access the fields of the result SObjects using the dot notation.
So you will have to query like this -->>
var ReferraAccepted = sforce.connection.query("SELECT Referral__r.Accepted_Date__c from Assessment__c where id ='{!Assessment__c.Id}' limit 1");
See this link for some asynchronus ajax examples -->>
https://developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/sforce_api_ajax_more_samples.htm
Hope this Helps..!!
The SOQL query seems to work fine in Workbench but I still cannot get the results to work in my if statement. Any ideas?
alert('::ReferraAccepted :::'+ReferraAccepted +':::');
if (ReferraAccepted == null || ReferraAccepted == 'null')
Hi Mike,
Try this code -->>
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var ReferraAccepted = sforce.connection.query("SELECT Referral__r.Accepted_Date__c from Assessment__c where id ='{!Assessment__c.Id}' limit 1");
records = ReferraAccepted.getArray("records");
var accDate = records[0].Referral__r.Accepted_Date__c;
if(accDate==null){
alert("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();
}
Let me know if this works for you..!!
However, it still doesn't work. I attempted to see what is being returned with the var accDate using alert('::accDate :::'+v accDate +':::') and the result is coming up as undefined?
alert('::accDate :::'+accDate +':::') ;
You need to make sure that you are using correct API names.
Cross Check..See if this helps
If I run this check, alert('::records :::'+ records +':::') ; I see the result of my query, however when I check var accDate = records[0].Referral__r.Accepted_Date__c; I'm getting an undefined result.......
Accepted_Date__c:null
That is correct. The complete alert is {type: 'Assessment__c', Id:null, Referral__r:{type:'Referral__c, Id:null, Accepted_date__c:null,},}.
Hi Mike,
If thats returning null then i dont see any reason why it is showing as undefined there.
Ok, just try this code -->>
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var ReferraAccepted = sforce.connection.query("SELECT Referral__r.Accepted_Date__c from Assessment__c where id ='{!Assessment__c.Id}' limit 1");
records = ReferraAccepted.getArray("records");
if(records[0].Referral__r.Accepted_Date__c==null){
alert("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();
}
Please let me know if this works.!!
I'm sure the error is here "records[0].Referral__r.Accepted_Date__c" as code is not finding the field even though it is being found in the query.
I am having the same problem, can you tell me how you fixed this?