function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dave Wolfe 11Dave Wolfe 11 

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.

User-added image
This script calls this apex class.
User-added image

Which in turn calls this class.

User-added image

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.
Best Answer chosen by Dave Wolfe 11
Raj VakatiRaj Vakati
Use this code
 
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
 
trigger Interview_QuestionsTrigger on Interview_Questions__c( before insert , before update){
	new WV_InterviewAnswersHandler( trigger.new[0].Id) ;
}

All Answers

Raj VakatiRaj Vakati
Can you share the code ..not as images  i will be able to write the code for you 
Dave Wolfe 11Dave Wolfe 11
Certainly.

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'];
  }
}

 
Raj VakatiRaj Vakati
Use this code
 
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
 
trigger Interview_QuestionsTrigger on Interview_Questions__c( before insert , before update){
	new WV_InterviewAnswersHandler( trigger.new[0].Id) ;
}

This was selected as the best answer
Dave Wolfe 11Dave Wolfe 11
Tweaked it a bit and it worked perfectly.

Thank you very much!