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
viv1viv1 

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..

AmitSahuAmitSahu
What error ? Is it about Id cannot be updated ?
kiranmutturukiranmutturu
whats the error u r getting and which edition that u are using ?
viv1viv1

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 ??

kiranmutturukiranmutturu
try this

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;
}
}
}
AmitSahuAmitSahu
Profile p = [select id from Profile where name='Standard Platform'];

Please check if the above query gives you any records..
AmitSahuAmitSahu
Use : "Standard User" for your query.. might work if you dont have any Standard Platform profile.
viv1viv1

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

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.FinalException: Record is read-only: Trigger.createUser: line 8, column 1
AmitSahuAmitSahu
Remove the update command
viv1viv1

but i want to assign that user to student...

student is lookup to user !!

AmitSahuAmitSahu
You cannot update the same record in the after trigger. You can use a method in class with future annotations.
Vinit_KumarVinit_Kumar

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;

}
}

Goku ZeusGoku Zeus
We give proficient jack of all trades administrations across West London — and we can be at your entryway in practically no time. For quick, proficient and amicable property fix and upkeep administrations, we basically can't be bested. https://onegoodhandyman.co.uk/west-london/