0

I am trying to build an onClick JS button on a custom Object called Application.

The button is designed to update 2 fields on the the Application, and a related record's field on the Opportunity object, based on the following logic:

if Opportunity.Date_Application_Signed__c is null, 
OR, 
Application.Payment_Option__c = "Fee Help" && Opportunity.Date_ECAF_Signed__c is null

Then update...

Application__c.Status__c = 'Assessed'
Application__c.Application_Outcome__c = 'Provisional (Awaiting Certification)'
Opportunity.StageName = 'Won'

Although the syntax is sound (or at least doesn't error!) i'm not too sure if what i am trying to achieve is possible without calling a class? I have tried to return the variables through the alert, but with no success...

Any help would be appreciated!

OnClick JS Button:

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

var app = new sforce.SObject("Application__c"); 
app.id = "{!Application__c.Id}"; 
var payOption = app.Payment_Option__c; 
app.Status__c = 'Assessed'; 
app.Application_Outcome__c = 'Provisional (Awaiting Certification)'; 

var apln = sforce.connection.query("SELECT id,ownerId,Opportunity__r.Date_Application_Signed__c,Opportunity__r.Date_ECAF_Signed__c,Opportunity__c,contact__c from Application__c where id ='{!Application__c.Id}' limit 1"); 

records = apln.getArray("records"); 

var opp = new sforce.SObject("Opportunity"); 
opp.id = records[0].Opportunity__c; 
var dateAppSin = opp.Date_Application_Signed__c; 
var dateECAFSin = opp.Date_ECAF_Signed__c; 
opp.StageName = 'Won'; 

if( dateAppSin != '' || (payOption == "FEE Help" && dateECAFSin != '') ) 
{ 
result = sforce.connection.update([app]); 
resultofUpdate = sforce.connection.update([opp]); 
window.location.reload(); 
} 
else 
{ 
alert("You can not do this"); 
}
2

Your entire code is correct, the only thing you are missing is you are not getting opportunity Id in your query. You have to explicitly get the Opportunity id. Use below query. Everything else is fine. I hope this will resolve the issue.

var apln = sforce.connection.query("SELECT id,ownerId,Opportunity__r.Id,Opportunity__r.Date_Application_Signed__c,Opportunity__r.Date_ECAF_Signed__c,Opportunity__c,contact__c from Application__c where id ='{!Application__c.Id}' limit 1");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.