You need to sign in to do that
Don't have an account?
Create a custom object record when user create in salesforce.
Hi there,
When i created a user in salesforce, it will create a record on another object with same name. How do i write a trigger for it.
Greetings to you!
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Also, please refer to the below link which might help you further.
https://crmdev.blog/2017/07/05/an-employee-contact-record-for-every-salesforce-user/
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
I appriciate your help!
when i create a user it shows me an error " Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa)".
Thanks
DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org. You must insert or update these types of sObjects in a different transaction to prevent operations from happening with incorrect access-level permissions.
The solution for this issue is to use a future method to perform the DML operations.
Try this:
Class:
Trigger:
Regards,
Khan Anas
Try this code:
trigger:
trigger CreateRecordBasedOnUser on User (after insert) {
if(trigger.isInsert && trigger.IsAftre) {
CreateRecord.insertRecord(trigger.new);
}
}
trigger handler:
public class CreateRecord {
public static void insertRecord(List<User> userList) {
try {
if(userList.size() > 0) {
CustomObject__c cObj = new CustomObject__c ();
// here put all required fields.
insert cObj;
}
}
catch(Exception ex){
system.debug('Exception---ofLine--->'+ex.getLineNumber());
system.debug('Exception---Message--->'+ex.getMessage());
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi