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
SteveA101SteveA101 

Workflow to create a record

Hi All,

 

Is it possible to have a WFR create a record on a different object? My gutt tells me this is a trigger, but thought I'd check with the experts first! 

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Satish_SFDCSatish_SFDC
trigger CandidateTrigger on Candidate__c (before insert) {
List<Partner_Account__c> lstPartnerAccounts = new List<Partner_Account__c>();
for(Candidate__c candidate : Trigger.new){
if(candidate.Hired__c == true){
Partner_Account__c p = new Partner_Account__c();
p.Name = candidate.Name;
//Populate other fields for partner account here

lstPartnerAccounts.add(p);
}
}
insert lstPartnerAccounts;
}

Create a Trigger on Candidate Object. When the Candidate record is updated by setting the Hired checkbox to true, you create a new Partner Account record. You are initially adding all the partner accounts created to a list and then bulk adding them and the end.

 

Hope this helps.

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

All Answers

IspitaIspita
Well the whole object might not be created using WFR.
I think the route is "Trigger"
In case you want to minimize custom codeyou can put some logic for firing of trigger in WFR and put code of creating object in trigger.
But one can urgue that when one is writing a trigger why then write a WFR too, may be to reduce coding and build a more robust system :)
Do let me know if this answers your question.
Have a great day!!
Thanks and regards,
Satish_SFDCSatish_SFDC
Unfortunately workflow rules do not allow you to create a new record. You are right, triggers is the solution here.


Hope this helps!
Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
SteveA101SteveA101

Hi.  Thanks for the quick response.  The object that I would like the record created on will already exist.  Basically we are looking to use a Recruiting app, and when the Partner gets 'hired' (the Partner record is on the Candidate object), create a record for them on the Partner Account.

Satish_SFDCSatish_SFDC
Even in this case, there is a new record in the partner Account object which has to be created. And because WF rules do not allow creation of new records, i would suggest you take the help of a trigger.
The trigger would be a simple before insert trigger.

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
SteveA101SteveA101

Understood.  Thanks!

 

Anyone happen to have any sampe code for something like this to get me started?

Satish_SFDCSatish_SFDC
trigger CandidateTrigger on Candidate__c (before insert) {
List<Partner_Account__c> lstPartnerAccounts = new List<Partner_Account__c>();
for(Candidate__c candidate : Trigger.new){
if(candidate.Hired__c == true){
Partner_Account__c p = new Partner_Account__c();
p.Name = candidate.Name;
//Populate other fields for partner account here

lstPartnerAccounts.add(p);
}
}
insert lstPartnerAccounts;
}

Create a Trigger on Candidate Object. When the Candidate record is updated by setting the Hired checkbox to true, you create a new Partner Account record. You are initially adding all the partner accounts created to a list and then bulk adding them and the end.

 

Hope this helps.

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

This was selected as the best answer
SteveA101SteveA101

Fantastic.  Thahks so much Satish!!

IspitaIspita
Hi Steve,
trigger HelloWorldTrigger on Book__c (before insert) {

Book__c[] books__c = Trigger.new;
Copy[] Copy__c = new Copy__c();
integer a=0;
Copy[] Copy__c = new Copy__c();
for(b Books__c : Book__c)
if (b.status=true)
{
SingleCopy Copy__c = new Copy__c();
SingleCopy.name="test";
SingleCopy.status="true";
}
Copy[a]=SingleCopy;
a++;
insert Copy;
}
Hope this helps
Thanks