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
Clayto63Clayto63 

Pass record data to class in process builder

hi, the following code creates a csv and sends it attached by email. When I paste this into dev console and execute, it works perfectly.
However, I need to create this as a class and process via a process builder and pass 3 variables of information to the class; strEmailTo, strPhone and strTreatment. I'm not sure how to do this. Any help much appreciated thanks.

string header = 'Telephone;Content \n';
string finalstr = header ;
string mStr;
string strPhone;
string strDate;
string strEmailTo;
string strTreatment;

strDate = '01/09/2019';
strPhone = '12345678';
strEmailTo = 'me@me.com';
strTreatment = 'test123';

mStr='Hi, your '+strTreatment+ ' treatment is scheduled for '+strDate ;

string recordString = +strphone+';'+mstr+'\n';
finalstr = finalstr +recordString;
  
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();

blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'custom.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);

Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();

String[] toAddresses = new list<string> {strEmailTo};
String subject ='Visit';

email.setSubject(subject);
email.setToAddresses( toAddresses );
email.setPlainTextBody('%Content%');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Best Answer chosen by Clayto63
Boss CoffeeBoss Coffee
Here is a quick example of how to set up the process builder with a number of fields. As you can see, str2 is being assigned the Contact's Account's name, so you can do something similar in your scenario between those objects. Once you're inside that invocable method, you can pull and handle the parameters as you like.
User-added image
User-added image

All Answers

Boss CoffeeBoss Coffee
What will be triggering this process builder?
Clayto63Clayto63
a picklist value change
Boss CoffeeBoss Coffee
Will the object be holding the values of EmailTo, Phone, and Treatment? If so, then you can pass the Id of the record that triggers the process builder, which can then call your class to query that record for those specific fields.
Clayto63Clayto63
hi Boss Coffee, the values will come from 2 objects - strPhone from customer account and strTreatment and strDate (not strEmail - correction) from another object. 
 
Clayto63Clayto63
i've read about passing data via invokable variables?
 
Boss CoffeeBoss Coffee
Is the picklist value change on one of those objects? As for the second object, does it have a relationship to the Customer Account (or whichever object that fires the trigger)? You can pass variables into the invocable class, but the question is from what records to pull those fields from.
Clayto63Clayto63
there is a look up between objects
Boss CoffeeBoss Coffee
Which object fires the trigger? Is that the object that looks up to the other one? If so, then you can pass the parameters from the object record that fired the process builder, and you can fetch the fields from the object record it looks up to.
Clayto63Clayto63
Thanks. The communication from process builder and the class is the bit I am struggling with.
Boss CoffeeBoss Coffee
Do you have any screenshots of how you're trying to connect them currently?
Boss CoffeeBoss Coffee
Here is a quick example of how to set up the process builder with a number of fields. As you can see, str2 is being assigned the Contact's Account's name, so you can do something similar in your scenario between those objects. Once you're inside that invocable method, you can pull and handle the parameters as you like.
User-added image
User-added image
This was selected as the best answer
Clayto63Clayto63
thanks very much. Great help