You need to sign in to do that
Don't have an account?
problem with trigger ??
my code is :
trigger createUser on Student__c (after insert) {
Profile p = [select id from Profile where name='Standard Platform'];
for(Student__c stud: Trigger.new)
{
User u = new User(alias = 'student', email=stud.Email_ID__c, emailencodingkey='UTF-8', firstName=stud.First_Name__c, lastname=stud.Last_Name__c, languagelocalekey='en_US', localesidkey='en_US', profileid = p.id, timezonesidkey='Europe/London', username=stud.Enrollment_No__c+'@user.com');
insert u;
stud.User__c=u.ID;
}
}
whenever i am creating student record it should create user and associate user with student
but its i s giving an error..
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger createUser caused an unexpected exception, contact your administrator: createUser: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.createUser: line 2, column 1
this is the error..
i am having problem how to use trigger.new ??
trigger createUser on Student__c (after insert) {
Profile p = [select id from Profile where name='Standard Platform' limit 1];
if(p != null){
for(Student__c stud: Trigger.new)
{
User u = new User(alias = 'student', email=stud.Email_ID__c, emailencodingkey='UTF-8', firstName=stud.First_Name__c, lastname=stud.Last_Name__c, languagelocalekey='en_US', localesidkey='en_US', profileid = p.id, timezonesidkey='Europe/London', username=stud.Enrollment_No__c+'@user.com');
insert u;//remove this and add to a list and insert the same after the loop
stud.User__c=u.ID;
}
}
}
Please check if the above query gives you any records..
now i have updated the code
trigger createUser on Student__c (after insert) {
Profile p = [select id from Profile where name='Student'];
for(Student__c stud: Trigger.new)
{
User u = new User(alias = 'student', CommunityNickname=stud.Enrollment_No__c,email=stud.Email_ID__c, emailencodingkey='UTF-8', firstName=stud.First_Name__c, lastname=stud.Last_Name__c, languagelocalekey='en_US', localesidkey='en_US', profileid = p.id, timezonesidkey='Europe/London', username=stud.Enrollment_No__c+'@abcinstitute.com');
insert u;
System.resetPassword(u.Id, true);
stud.User__c=u.id;
update stud;
}
}
and error is
but i want to assign that user to student...
student is lookup to user !!
Hi Viv,
Try below code :-
trigger createUser on Student__c (before insert) {
Profile p = [select id from Profile where name='Student'];
for(Student__c stud: Trigger.new)
{
User u = new User(alias = 'student', CommunityNickname=stud.Enrollment_No__c,email=stud.Email_ID__c, emailencodingkey='UTF-8', firstName=stud.First_Name__c, lastname=stud.Last_Name__c, languagelocalekey='en_US', localesidkey='en_US', profileid = p.id, timezonesidkey='Europe/London', username=stud.Enrollment_No__c+'@abcinstitute.com');
insert u;
System.resetPassword(u.Id, true);
stud.User__c=u.id;
}
}