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
34213421 

Can Process Builder get the id of record that triggered the process


I have a process builder which invokes the Apex class. This Apex sends out an email to the Contact. The attachment of the Email is a VF page which gets the list of line items.
The issue here is that I would like to capture the record id of the record invoked by Process Builder and pass it on all the way to the VF page.
Any sample code or ideas would greatly help
Best Answer chosen by 3421
sachin kadian 5sachin kadian 5
Ok its not showing you because your  send() method is not accepting any parameter.

you can use send(List<Id> listOfRecordId) 

then i am sure it will show you in process builder.

All Answers

sachin kadian 5sachin kadian 5
You can send the list of Ids to apex class using this -

User-added image

from the apex class, you can pass it further.

Let me know if it helps you..
34213421
I do not see anything like setting a field variableUser-added image
Is there somethign I am missing here. I have a full copy sandbox in EE 
sachin kadian 5sachin kadian 5
are you sure this  the class you choose exists? is it showing you in the list when you start typing? if yes, when you will click on class name, it will automatially ask for parameters.
 
public class AccountQueryAction {
  @InvocableMethod(label='Get Account Names' description='Returns the list of account names corresponding to the specified account IDs.')
  public static List<String> getAccountNames(List<ID> ids) {
    List<String> accountNames = new List<String>();
    List<Account> accounts = [SELECT Name FROM Account WHERE Id in :ids];
    for (Account account : accounts) {
      accountNames.add(account.Name);
    }
    return accountNames;
  }
}

Make sure you have @InvocableMethod(label='Get Account Names'  in the class. Label is what appears in process builder.
34213421
This is the code that I have written
public SendEmailInvoice()

    {

    }
    public class MyInvocableVariable {
    @InvocableVariable(label='Id' required=true)
    public Id recId;

    }    
   @InvocableMethod(label='Send an email from apex class'
 description='sends an email')
  
    public static void send()
	
    {
        List<MyInvocableVariable> myInvocableVariableList = new List<MyInvocableVariable>();
 for(MyInvocableVariable myInvocableVariable : myInvocableVariableList) {
   
     
       system.debug('recId=' + myInvocableVariable.recId);
 }
         
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        PageReference pdf = Page.Sende;
        pdf.setRedirect(true);
        // Take the PDF content
        Blob b = pdf.getContent();
 List<String> mail = new List<String>();
        mail.add(Test@tes.com');
       
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('Attachement');
        efa.setBody(b);


      	email.setSubject('Testing');
        email.setToAddresses(mail);

        email.setPlainTextBody('Test');

        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // Sends the email

        Messaging.SendEmailResult [] r =

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  


    }

 
sachin kadian 5sachin kadian 5
Ok its not showing you because your  send() method is not accepting any parameter.

you can use send(List<Id> listOfRecordId) 

then i am sure it will show you in process builder.
This was selected as the best answer
sachin kadian 5sachin kadian 5
You can do something like this-- 
 
public SendEmailInvoice(){
    
    
    @InvocableMethod(label='Send an email from apex class'
                     description='sends an email')
    
    public static void send(List<Id> listOfRecordId){
        
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        
        PageReference pdf = Page.Sende;
        pdf.getParameters().put('parameterName',listOfRecordId[0]);
        pdf.setRedirect(true);
        // Take the PDF content
        Blob b = pdf.getContent();
        List<String> mail = new List<String>();
        mail.add('Test@tes.com');
        
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('Attachement');
        efa.setBody(b);
        
        
        email.setSubject('Testing');
        email.setToAddresses(mail);
        
        email.setPlainTextBody('Test');
        
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // Sends the email
        
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
        
        
    }
}

 
34213421
Awesome. That worked. Can I pass this record id to the VF page again? I did try something like this
String recordId = ApexPages.currentPage().getParameters().get('listOfRecordId');
 
String recordId = SendEmail.get('listOfRecordId');

But this did not work. I know that currentpage dosent work since the page is not getting invoked directly
sachin kadian 5sachin kadian 5
In you VF page controller, you can use 
 
String recordId = ApexPages.currentPage().getParameters().get('recordId');



and from your process builder controller , you can pass the id like this

 
PageReference pdf = Page.Sende;
pdf.getParameters().put('recordId',listOfRecordId[0]);
pdf.setRedirect(true);

Apart from this, whatever you are doing looks fine to me.
sachin kadian 5sachin kadian 5
enen you dont need to use 
pdf.setRedirect(true);

when you do pdf.getContent() , it automatically hits the page and its controller gets executed.
34213421
Oops yeah missed that statement of putting recordid. Thanks a ton