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
Galeeb SKGaleeb SK 

Factory Trigger in Apex

Hi 
i wrote a factory trigger for insert the records and updating  the records.The class should be calling in Trigger ,it saves.But inserting and updating can't be done.My Code is 

Apex Class:

public with sharing class MultipleTriggers
{
public static void execute()
{

if(trigger.isBefore){

list<Position__c> pos = new list<Position__c>();

for(Position__c p : pos){

        if(p.Priority__c=='Critical'&&
        
           p.Department__c=='IT'){
           
        if(p.Max_Pay__c <500000){
        
           p.Pay_Grade__c='IT-100';
           }
        else if(p.Max_Pay__c>500000 && p.Max_Pay__c<1000000){ 
        
                p.Pay_Grade__c='IT-200';
                }
        else if(p.Max_Pay__c>1000000 &&p.Max_Pay__c<1500000){
        
                p.Pay_Grade__c='IT-300';
                
                }
                
                else{
                
                p.Pay_Grade__c='IT-400';
             }
           }
        }
       }
      
       }
       
       public static void Handler()
       
       {
       
      if(trigger.isAfter) {
      
      list<Position__c> posi=new list<Position__c>();
      
    List<Job_Application__c> jobAppReclistToBeupdated=new List<Job_Application__c>();

    
     for(Position__c posRec:posi){
     
         if(posRec.Status__c=='Closed'){
         
list<Job_Application__c> joblist=new list<Job_Application__c>();

            joblist=[SELECT Id,Status__c,Position__c FROM Job_Application__c];
            
         for(Job_Application__c jobAppRec:joblist){
         
                    jobAppRec.Status__c='Closed';
                    
                 jobAppReclistToBeupdated.add(jobAppRec);
              
                        }
         }
        }
    update jobAppRecListToBeupdated;
}
}
/*public static void galeebs()
{

if(trigger.isDelete)
{

for(Position__c psk : trigger.old)
{
*/

}

Trigger:

trigger Factory on Position__c (before insert,before update,after update) {

if(trigger.isInsert)
{
MultipleTriggers.execute();
}
if(trigger.isUpdate)
{
MultipleTriggers.Handler();
}
}
Tell me the reason why it can't perform action

Regards
Galeeb SK
pconpcon
Your problem is that in your class you create a list of Position__c and then reference that instead of using Trigger.new.  I've taken the liberty of updating your code to do what you think it should as well as fixing some formatting.

Class
public with sharing class MultipleTriggers {
    public static void execute() {
        if (Trigger.isBefore) {
            List<Position__c> pos = new List<Position__c>();

            for (Position__c p : pos) {
                if (
                    p.Priority__c == 'Critical' &&
                    p.Department__c == 'IT'
                ) {
                    if (p.Max_Pay__c < 500000) {
                        p.Pay_Grade__c='IT-100';
                    } else if (
                        p.Max_Pay__c>500000 &&
                        p.Max_Pay__c<1000000
                    ) { 
                        p.Pay_Grade__c='IT-200';
                    } else if (
                        p.Max_Pay__c > 1000000 &&
                        p.Max_Pay__c < 1500000
                    ) { 
                        p.Pay_Grade__c='IT-300';
                    } else {
                        p.Pay_Grade__c='IT-400';
                    }
                }
            }
        }
    }

    public static void Handler() {
        if (Trigger.isAfter) {
            List<Job_Application__c> jobAppReclistToBeupdated = new List<Job_Application__c>();
            Set<Id> posIds = new Set<Id>();
            
            for (Position__c posRec : Trigger.new) {
                if (posRec.Status__c == 'Closed') {
                    posIds.add(posRec.Id);
                }
            }

            if (posIds.isEmpty()) {
                return;
            }

            for (Job_Application__c jobAppRec : [
                select Status__c,
                    Position__c
                from Job_Application__c
            ]) {
                jobAppRec.Status__c = 'Closed';
                
                jobAppReclistToBeupdated.add(jobAppRec);

            }

            if (jobAppReclistToBeupdated.isEmpty()) {
                return;
            }

            update jobAppRecListToBeupdated;
        }   
    }   
}

Trigger
trigger Factory on Position__c (before insert, before update, after update) {
    if (Trigger.isInsert) {
        MultipleTriggers.execute();
    } else if (Trigger.isUpdate) {
        MultipleTriggers.Handler();
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.