I am very new to Salesforce (have no experince at all), so please excuse if I haven't explained the scenario correctly. I have "Send Email" button which has OnClick Javascript in Classic. The javascript code is as shown below. I am calling Page_Object which reference to the page (as Account or Contact or some custom object page) on which the user clicks the button
{!REQUIRESCRIPT('/soap/ajax/41.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/41.0/apex.js')}
sforce.connection.sessionId = '{!$Api.Session_ID}';
var queryVal = sforce.connection.query("SELECT Id FROM Custom_Object__c WHERE (Custom_Object1.Name = 'val1' AND userId = '{!$User.Id}')");
var queryValrecord = queryVal.getArray("records");
var objectValue = "{!Page_Object.column1}";
if(queryValrecord.length === 1) {
if(objectValue == 'some value'){
var str;
var str = sforce.apex.execute("ApexClass", "ApexMethod",{ID:'{!Page_Object.Id}'});
if(str == 'success')
parent.location.href='/'+'{!Page_Object.Id}';
else
alert(str);
}
else {
alert('Warning message1');
}
}
else {
alert('Warning message2');
}
Below is the Apex Method which is being called from apex.execute
WebService static String SendEmail(String Id) {
List<Messaging.SingleEmailMessage> email = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String sendTo;
Object obj = [SELECT Id,Name FROM Object Where Id=:Id LIMIT 1];
sendTo = "[email protected]";
email.setToAddresses(sendTo);
email.setSenderDisplayName('Some Name');
email.setSubject(obj.Name);
String body = obj.Name;
email.setHtmlBody(body);
email.saveAsActivity = false;
emails.add(email);
Messaging.sendEmail(emails);
return ('Email has been sent successfully');
}
While converting to Lightning component, I am sure we can use force:recordId to pass to controller method, but not sure how to handle the query and related conditions in the javascript while using component. How to access Page_Object.Column1 (which is nothing but some field value on that page) in Component? Can someone please guide me.