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
Bhyrava prasadBhyrava prasad 

process builder - apex class

hi

From process builder can we call an apex class which has a method to create a record in a object ?
Best Answer chosen by Bhyrava prasad
Shaik Naga janiShaik Naga jani
Hi Bhyrava,
check below sample code once,
public class InsertStudentMaster {
    
    @InvocableMethod
    public static void insertNewStudentMaster(List<ID> ids) {
        
        List<Student_master__c> StudentMasterNew = [SELECT name,Age__c, City__c, Comments__c, India__c, Email__c from student_master__c WHERE Id=:ids];
        System.debug('StudentMasterNew =====> '+StudentMasterNew);
        List<Student_master__c> lstStudentToClone = new List<Student_master__c>();
        if(!StudentMasterNew.IsEmpty()) {
            for(Student_master__c smIterator : StudentMasterNew) {
                Student_master__c objStudent = new Student_master__c();
                objStudent = smIterator.clone(false, false, false, false));
                lstStudentToClone.add(objStudent);
            }
        }
        if(!lstStudentToClone.isEmpty()) {
            Database.insert(lstStudentToClone, false);
        }
    }
}
Thanks
Shaik

All Answers

Shaik Naga janiShaik Naga jani
HI Bhyrava,
yes, you can call that method, but declare that method as @InvocableMethod Annotation.
Example:
public class ProcessBuilderDemo {
    @InvocableMethod
    public static void createOpps(list<Id> lstIds) {
        // logic here
    }
}

Kindly mark this as solved if the reply was helpful.
Thanks
Shaik
 
Bhyrava prasadBhyrava prasad
thank you for the reply Shaik...Yes i have declared the method as InvocableMethod. I am pass the record Id as the input from the process builder through the field reference. Using the record id , im trying to clone the record and create a new record, which is not happening. Im pasting the code for your reference, and if you can have a look at it and help me with the corrective measure. 


public class InsertStudentMaster
{

@InvocableMethod
public static void insertNewStudentMaster(List<ID> ids)
 {
    
   List<Student_master__c> StudentMasterNew = [SELECT name,Age__c, City__c, Comments__c, India__c, Email__c from student_master__c WHERE Id=:ids];
     if(!StudentMasterNew.IsEmpty())
     {
             List<Student_master__c> cloneList = StudentMasterNew.Clone();
             insert cloneList;
     }
 }
}
Shaik Naga janiShaik Naga jani
Hi Bhyrava,
check below sample code once,
public class InsertStudentMaster {
    
    @InvocableMethod
    public static void insertNewStudentMaster(List<ID> ids) {
        
        List<Student_master__c> StudentMasterNew = [SELECT name,Age__c, City__c, Comments__c, India__c, Email__c from student_master__c WHERE Id=:ids];
        System.debug('StudentMasterNew =====> '+StudentMasterNew);
        List<Student_master__c> lstStudentToClone = new List<Student_master__c>();
        if(!StudentMasterNew.IsEmpty()) {
            for(Student_master__c smIterator : StudentMasterNew) {
                Student_master__c objStudent = new Student_master__c();
                objStudent = smIterator.clone(false, false, false, false));
                lstStudentToClone.add(objStudent);
            }
        }
        if(!lstStudentToClone.isEmpty()) {
            Database.insert(lstStudentToClone, false);
        }
    }
}
Thanks
Shaik
This was selected as the best answer
Bhyrava prasadBhyrava prasad
awesome stuff Shaik....You are a champ ! Working perfectly ! thank you very much. I am just trying to understanding as to where i was going wrong,