You need to sign in to do that
Don't have an account?
Replace Javascript button with Apex Trigger
We currently have a javascript button that calls an apex class. As we move to Lightning we want to replace that button and the user would like (if possible) to simple have the apex method called automatically since we preform this task on every record in this custom object.
My thought was to call the existing apex from a new apex trigger fired after insert.
Here is the existing javascript code.

This script calls this apex class.

Which in turn calls this class.

I'm not reapply an apex developer and my initial attempts at the trigger failed.
Do I need to call the middle program at all if it is from a trigger? And how do I renmae the 'Candidate_Profile__c'id' to ID in the trigger as it was done in the javascript?
Or am I completely off base on how i am approaching this problem?
Thanks in advance.
My thought was to call the existing apex from a new apex trigger fired after insert.
Here is the existing javascript code.
This script calls this apex class.
Which in turn calls this class.
I'm not reapply an apex developer and my initial attempts at the trigger failed.
Do I need to call the middle program at all if it is from a trigger? And how do I renmae the 'Candidate_Profile__c'id' to ID in the trigger as it was done in the javascript?
Or am I completely off base on how i am approaching this problem?
Thanks in advance.
Trigger
All Answers
Here is the original Javascript.
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
sforce.apex.execute("WV_InterviewAnswer_UI_Launch","InitAnswers", {ID:"{!Candidate_Profile__c.Id}"});
window.alert("Refresh profile to view questions." );
Which calls this apex class.
global class WV_InterviewAnswer_UI_Launch {
WebService static void InitAnswers(ID SourceID){
WV_InterviewAnswersHandler launchvar = new WV_InterviewAnswersHandler(SourceID);
}
}
Whihc calls this...
public with sharing class WV_InterviewAnswersHandler {
public static list<Interview_Questions__c> questions;
public static list<Interview_Answers__c> answers;
public WV_InterviewAnswersHandler(ID acctID){
initialize();
AddAllQuestions(acctID);
UpdateAll();
}
private static void AddAllQuestions(ID acctID){
for (Interview_Questions__c q : questions){
/*
answers.add(New Interview_Answers__c(Interview_Questions__c=q.Id, Account__c=acctId));
*/
answers.add(new Interview_Answers__c(Interview_Questions__c=q.id, Candidate_Profile__c=acctId));
}
}
private static void UpdateAll(){
system.debug('COUNT:' + answers.size());
insert answers;
}
private static void initialize(){
answers = new list<Interview_Answers__c>();
questions = [Select i.Inactive__c, i.Id From Interview_Questions__c i WHERE i.Inactive__c = false AND Type__c = 'Coaching'];
}
}
Trigger
Thank you very much!