Salesforce coding lessons for the 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
  • Beginner Tutorials
    • Apex
    • Certifications
    • Career Info
    • Technical Architect
    • Visualforce
    • Videos
  • Apex Academy
  • Success Stories
  • About Me
  • Misc
    • Mailbag
    • Challenges
    • Links
    • Login to my Org
Follow @dvdkliuor SUBSCRIBE!

Fix this trigger: Lazy Employee

July 7, 2014

Preface: this post is part of the SFDC99 Challenges series.

Can you find everything wrong with this trigger?!

Fix this trigger, paste it in the comments, and I’ll let you know how you did!

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (after insert) {
  List<Case> newCases = new List<Case>();
  for (Case a : Trigger.new) {
    a.Status = "Closed";
    a.Owner  = "David Liu";
    newCases.add(a)
  }
  update newCases;
}

There’s at least three things wrong with it!

Post your fixed trigger in the comments and I’ll tell you how you’re doing! Remember, just because code works doesn’t mean that it can’t be improved!

View the solution here!

P.S. Thanks to @Steph_Herrera for this great idea!!

101 Comments
shubham shahu
November 14, 2022 @ 11:52 pm

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (before insert) {

for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.Owner = ‘David Liu’;
}

}

Reply
Aman Garg
September 27, 2022 @ 2:53 am

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (after insert) {
List newCases = new List();
newCases = [Select id, Owner, Status from Case where Owner = ‘David Liu’]
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
newCases.add(a)
}
update newCases;
}

Reply
Jayni
February 12, 2022 @ 11:23 pm

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (after insert) {
List newCases = new List();
Id OwnerId = [SELECT Id,Name FROM User WHERE Name = ‘David Liu’ LIMIT 1].Id;
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.Owner = OwnerId;
newCases.add(a);
}
update newCases;
}

Reply
Neha Bhatia
January 21, 2022 @ 1:59 pm

trigger LazyEmployee on Case (before insert) { //1. After triggers are read only, so it needs to be a before trigger
List newCases = new List();

newCases = [SELECT Id FROM Case WHERE Id IN Trigger.New AND
Status!=’Closed’ ];
for (Case a : newCases) { // 2.The for loop can be more efficient by looping over only new and open cases
a.Status = “Closed”;
a.Owner = “David Liu”;
newCases.add(a); // 3.This line missed the ending semi-colon.
}
update newCases;
}

Reply
Neha Bhatia
January 21, 2022 @ 1:55 pm

trigger LazyEmployee on Case (before insert) { //1. After triggers are read only, so it needs to be a before trigger
List newCases = new List();
newCases = [SELECT Id FROM Case WHERE Id IN Trigger.New AND
Status!=’Closed’ ];
for (Case a : newCases) { // 2.The for loop can be more efficient by looping over only new and open cases
a.Status = “Closed”;
a.Owner = “David Liu”;
newCases.add(a); // 3.This line missed the ending semi-colon.
}
update newCases;
}

Reply
    Neha Bhatia
    January 21, 2022 @ 2:09 pm

    trigger LazyEmployee on Case (before insert) { //1. After triggers are read only, so it needs to be a before trigger
    List newCases = new List();

    newCases = [SELECT Id FROM Case WHERE Id IN Trigger.New AND
    Status!=’Closed’ ];
    for (Case a : newCases) { // 2.The for loop can be more efficient by looping over only new and open cases
    a.Status = “Closed”;
    a.Owner = “David Liu”;
    newCases.add(a); // 3.This line missed the ending semi-colon.
    }
    update newCases;
    }

    Reply
Arsalan Kazmi
December 2, 2021 @ 5:59 am

//used the best Practise for Trigger
trigger CaseTrg on Case (before insert) {

if(trigger.before && trigger.isInsert)
{
CaseTrgHandler.BeforeInsert(trigger.new);
}

}

public static class CaseTrgHandler{
public static void BeforeInsert(List listOfCases)
{

for (Case a : listOfCases) {
a.Status = ‘Closed’;
a.OwnerId = ‘Describe call to get the ownerId’;
}
}
}

Reply
Art
November 24, 2021 @ 5:47 am

trigger LazyEmployee on Case (before update) {

List newCases = new List();
User adminId = [SELECT Id FROM User WHERE Name = ‘Arturas Sedleckas’];

for (Case a : Trigger.new) {
if(a.Status == ‘Closed’){
a.OwnerId = adminId.Id;
newCases.add(a);
}

}

}

Reply
vishwas
May 26, 2021 @ 10:18 am

The three mistakes i found in the below apex code are:

List newCases = new List();
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
newCases.add(a)
}
update newCases;

1.Dont require to create a case list, Trigger.new will give the current case record.
2.String should be enclosed in single cotations.
3.We cannot change Owner field directly as there is not field called Owner on Case Object.Its a lookup field of User Object

If there are any mistakes, please post here
Thanks,
Vishwas kumar

Reply
Chakra Gadige
May 3, 2021 @ 5:03 pm

trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;

}

}

Reply
Sireesha
March 24, 2021 @ 10:30 pm

// “after insert” cannot update the values because the record is still not
// saved to the database and is in ReadOnly Mode. For Updating use Before Trigger//
trigger LazyEmployee on Case (before update) {
List newCases= new List();
for(Case a:Trigger.new)
{
//Assignment variables should be enclosed in single quotes//
a.Status = ‘Closed’;
//There’s no Owner field in Case object – OwnerId is a lookup from User object
User u = [Select id from user where name =’David Liu’];
a.OwnerId=u.Id;
newCases.add(a);
}
//No need to explicitly provide a DML statement for before update.values will be updated when trigger ends.
}

Reply
Tom
March 4, 2021 @ 4:18 pm

This is a great series, well done. For some constructive feedback, these challenges should not just be, “can you spot a problem”. There’s so many different ways of solving problems with tech, especially Salesforce, and having context of the business case to begin with helps frame the potential solutions. For example if you wrote, “what’s wrong with the code that was written to solve X problem” it would get more people interested in solving the problem. Again well done, just giving some positive feedback.

Reply
Bharatkumar Kakani
February 18, 2021 @ 3:12 pm

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (beforeinsert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}

Reply
Anshul Bhasin
July 6, 2020 @ 8:01 pm

trigger CloseCase on Case (before insert) {
for(case c: Trigger.new){
c.status = ‘Closed’;
c.ownerid = ‘1234567890abcx’;
}
}

Reply
Suan
April 14, 2020 @ 1:55 pm

trigger LazyEmployee on Case (before insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.Owner.Name = ‘David Liu’;
newCases.add(a);
}
//update newCases;
}

Reply
Prateek Srivastava
March 12, 2020 @ 10:40 pm

trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = ‘123242353454656’;
}
}

Reply
anjali K
February 27, 2020 @ 9:18 am

trigger LazyEmployee on Case (after insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
newCases.add(a)
}
//update newCases;
insert newCases;
}
Error : first you are using insert event and you are updating a list.ID should be specify error will occur

Reply
Siddharth Kumar Jain
December 17, 2019 @ 3:12 am

// Please see line number 4,5,6 which also have comments

trigger LazyEmployee on Case (after insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’; // single inverted comma
a.OwnerId= ‘David Liu’; // single inverted comma and OwnerId
newCases.add(a); //semi-colon
}
update newCases;
}

Reply
Devesh S Rao
December 10, 2019 @ 5:28 am

1.semicolon missing after newCases.add(a)
2. Inverted commas
3. Ownerid

Reply
Siva Telluri
November 22, 2019 @ 8:05 pm

trigger testCase on Case (before insert) {
// we cannot update the same record which has been just inserted.
// List cc = new List();
// This List is not needed as for before insert it itself will perform the
// DML Operation
for(case a:Trigger.New)
{
a.status = ‘Closed’;
a.Owner = ‘David Liu’;

// cc.add(aa);
}
// update cc;
}

Reply
Diwakar Rajput
October 10, 2019 @ 8:36 am

trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}

Reply
Alex
October 4, 2019 @ 12:34 pm

trigger LazyEmployee on Case (after insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = UserInfo.getUserId();
newCases.add(a);
}
update newCases;
}

Reply
Sai Kethu
March 8, 2019 @ 5:03 am

//I dont think system will allow us update the same record in after insert block as per the order of execution. below code might work.
trigger LazyEmployee on Case (After insert) {

List newCases = new List();
for (Case a : Trigger.new) {
Case c = a.id;
c.Status = “Closed”;
c.Owner = “David Liu”;
newCases.add(c);
}

update newCases;

}

Reply
    Sai Kethu
    March 8, 2019 @ 5:05 am

    And missed this c.ownerId = ‘005************’;

    Reply
Subham
February 13, 2019 @ 1:32 am

trigger LazyEmployee on Case (before insert) {

for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;

}

}

Reply
smriti sharan
January 17, 2019 @ 9:01 am

trigger name on Leads(before insert ,before update) {

for(Leads a:Trigger.new)
{
a.status = ‘closed’;
a.Ownerid = ‘0057F0000018aoe’;

}

}

Reply
vimal
August 10, 2018 @ 5:49 am

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (before insert) { // should be before
// List newCases = new List(); :No need of extra list
List usr = [select id from User where name=’David Liu’ limit 1];
String userId=null;
if(usr!=null && usr.size()>0)
userId=usr.get(0).id;
for (Case a : Trigger.new) {
a.Status = ‘Closed’; // single quotes
a.Owner = userId; // id needed
//newCases.add(a) : No need of extra list
}
//update newCases; : no need of DML
}

Reply
Kishore Bandanadam
May 26, 2018 @ 6:09 am

trigger LazyEmployee on Case (before insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = Userinfo.getUserId();
newCases.add(a);
}

}

Reply
Ananya
May 12, 2018 @ 11:09 am

1> Error: Compile Error: Unrecognized symbol ‘”‘, which is not a valid Apex identifier. at line 4 column 16

2> Error: Compile Error: Missing ‘;’ at ‘}’ at line 7 column 3

3> Error: Compile Error: Field is not writeable: Case.Owner at line 5 column 7

trigger LazyEmployee on Case (before insert,before update) {

for (Case a : trigger.new) {

a.Status = ‘Closed’;

}

}

Reply
Trina Ghosh
April 28, 2018 @ 6:52 am

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (after insert) {
List newCases = new List();
User usr=[Select ID from user where name=’David Liu’ LIMIT 1];
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerID = usr.id;
newCases.add(a);
}
update newCases;
}

Reply
Shrishti
April 18, 2018 @ 5:53 am

trigger LazyEmployee on Case (before insert) {
for (Case c : Trigger.new) {
c.Status = ‘Closed’;
c.Ownerid = ‘00528000001szCO’;
}
}

Reply
Thirupathi Aeneni
March 24, 2018 @ 9:59 am

trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “18 digitsId”;

}

}

Reply
Thirupathi Aeneni
March 24, 2018 @ 9:25 am

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (after insert) {

for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;

}

}

Reply
Utkarsh
February 1, 2018 @ 10:16 pm

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new)
{
a.Status = ‘Closed’;
a.Ownerid = ‘0057F0000019aoe’;
}
}

Reply
ritesh yadav
January 24, 2018 @ 12:34 am

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (after insert) {
List newCases = new List();
user u=[select id from user where name=’David Liu’ limit 1];
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = u!=null?u.id:null;
newCases.add(a);
}
update newCases;
}

Reply
Suri
December 21, 2017 @ 1:48 am

trigger LazyEmployee on Case(before insert){//as after insert will give final exception
List newCases = new List(); //not required
Id ownerId;
OwnerId = System.Label.OwnerId;//as there can be more People named David we need to save that value somewhere custom setting or Custom label
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.OwnerId = OwnerId;//need to provide id instead of a String and there is no field on case as owner its always OwnerId
newCases.add(a)//semi colon missing but we do not need this statement as we are doing in trigger rather than any other class
}
update newCases;// Not required.
}

Reply
Ankur Gupta
November 16, 2017 @ 7:06 am

trigger LazyEmployee on Case (before insert) {
ID owner_ID = [Select ID from user where Name =”David Liu”];
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.OwnerID = owner_ID ;
}
}

Reply
Tapeshwar Kumar
October 5, 2017 @ 6:52 pm

trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}

Reply
Siddharth
September 13, 2017 @ 10:31 pm

trigger LazyEmployee on Case (before insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}

Reply
Lahari Rao
July 19, 2017 @ 2:38 am

trigger LazyEmployee on Case (before insert) {
// this should be a before trigger as we are updating the values of a new case, in after trigger we get an exception as ‘the record is read-only’
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’; //this should be in single quotes instead of double
a.OwnerID = userinfo.getUserId(); //here we should fetch the current user id and Assign to OwnerID
newCases.add(a); //semi-Colon;
}
//update newCases; –>This is not required as we are updating values before committing to the object
}

=======================================================

Please correct me if I am wrong.

Reply
Prabhu
July 16, 2017 @ 1:51 am

trigger lazyemployee_customsetting on Case (before insert) {

WorkflowOwnerId__c settings = WorkflowOwnerId__c.getinstance(‘roId’);
String OId = settings.OwnerId_cs__c;

for (Case newcase : Trigger.new) {
newcase.status = ‘Closed’;
newcase.OwnerId = OId;
}
}

Reply
Dushyant Srivastava
May 5, 2017 @ 8:11 am

trigger LazyEmployee on Case (after insert) {
List newCases = new List();
ID userID = [Select id from user Where Name = ‘David Liu’].ID;
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.Owner = ID;
newCases.add(a)
}
update newCases;
}

Reply
Nisha
April 14, 2017 @ 9:54 pm

trigger LazyEmployee on Case (before insert) {
List newCases = new List();
User userId = [select id from User where Name = ‘David Liu’];
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = userId.id;
newCases.add(a)
}
update newCases;
}

Reply
Km
August 22, 2016 @ 1:01 pm

trigger LazyEmployee on Case (before insert,before update) {
List u=[select Id, Name from User where Name=’David Liu’ limit 1];
system.debug(‘user is this’+u);
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = u[0].Id;

}

}

Reply
    Darrell Gallegos
    April 27, 2017 @ 6:59 am

    Limit 1 does not ensure the correct David Liu is the appropriate user to assign.

    Reply
Dar Wright
April 21, 2016 @ 12:06 am

trigger LazyEmployee on Case (before insert, before update) {

List newCases = new List();
User u = [Select ID from User where Name = ‘David Liu’];
for (Case a : Trigger.new){
if(a.OwnerId == u.Id && a.Status ‘Closed’){
a.Status=’Closed’;
newCases.add(a);
}//end if
}// for
}//end class

Reply
Vishal Shelar
February 2, 2016 @ 10:55 pm

trigger LazyEmployee on Case (after insert) {

List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.Owner = ‘David Liu’; // it will still give the “Illegal assignment from String to Name” error
newCases.add(a);
}
update newCases;
}

Reply
naveenkumarbh
December 26, 2015 @ 2:39 am

// This lazy dude wrote code to auto-accept and close all cases! : and me corrected it…can you say something about it?
trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}

Reply
Endrit Sino
December 17, 2015 @ 7:35 am

Hello,

I guess what’s wrong with this trigger are sintactic problems like the usage of the double quotes and the missing semi-colon.

In addition to that, the developer is trying to update a set of records that were just inserted. Instead, he can use a before trigger. In Before, we can do the necessary operations(set status closed & set owner to David Liu).

I think the code should look something, like this:

trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.Owner = ‘David Liu’;
}
}

Cheers,
E.

Reply
Anonymous
December 10, 2015 @ 1:56 pm

trigger LazyEmployee on Case (after insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = “Closed”; // Single quotes ‘ Closed’
a.Owner = “David Liu”; // Single quotes ‘David Liu ‘

newCases.add(a) // Add a semi Colon ;
}
update newCases;
}

Reply
Sujit
June 26, 2015 @ 8:48 am

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (before insert) {
//List newCases = new List();
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
//newCases.add(a)
}
//update newCases;
}

Reply
Anonymous
May 13, 2015 @ 8:29 am

trigger LazyEmployee on Case (before insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
newCases.add(a)
}
update newCases;
}

Reply
    Anonymous
    June 23, 2015 @ 11:37 am

    we have to use fir update isUpadate()

    Reply
sujata
April 2, 2015 @ 5:37 am

Trigger LazyEmployeeTrigger on Case (after insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
newCases.add(a);
}
update newCases;
}

Reply
    Anonymous
    August 21, 2015 @ 4:55 am

    hi sujatha
    this program is working???
    in this program one error came : Compile Error: unexpected token: ‘List’ at line 2 column 2
    can u reply to me

    Reply
siddhu
January 26, 2015 @ 11:36 am

trigger autoupdate on Case (before insert,after insert,before update ) {
List field=[SELECT id,status from case ];
if(trigger.isbefore )
{
for(case c :trigger.new){
c.status=’New’;
}

}
if(trigger.isafter )
{
for(case f:field){
f.status=’New’;
}

upsert field;
}

}

Reply
    siddhu
    January 26, 2015 @ 11:37 am

    oops sry here new is equal to closed

    Reply
Marky
January 6, 2015 @ 9:11 pm

trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = “David Liu”;
newCases.add(a)
}
}

Reply
Anonymous
September 24, 2014 @ 10:47 am

trigger LazyEmployee on Case (after insert) {

User user = [Select Id from User where Name = ‘David Liu’]; //username would be preferable
List newCases = new List();
Case newCase;

for (Case a : Trigger.new) {
newCase = new Case(Id = a.Id);
newCase.Status = “Closed”;
newCase.OwnerId = user.Id;
newCases.add(newCase);
}

//this acts like an upsert operation (after insert trigger)
update newCases;
}

Reply
kaveri
August 1, 2014 @ 4:02 pm

Hi David ,
This is what I ended with …. Pls let me know if I m correct…

trigger LazyEmployee on Case (after insert) {

List newCases = new List();
User u = [select id from User where Name = ‘David Liu’];
for (Case a : Trigger.new) {
a.Status = ‘Closed’; // Single Quotes
a.OwnerID = u.id; // Comparing with same type
newCases.add(a); // Semicolon Missing
}
update newCases;
}

Reply
Kt
July 31, 2014 @ 2:01 am

1. String should enclosed in single quotes. 2. If we are using List then we can add an element to it not update

Reply
Michael
July 29, 2014 @ 9:13 am

I am seeing many attempts, but I don’t know who is correct. I’d like to do something similar where I do a lookup/insert for a parent id onto a child record based on a name.(So, I know I need to have a little SOQL in it.) It looks like this thread could do what I need.

Reply
    David Liu
    July 29, 2014 @ 9:44 pm

    I’ll have the answers with explanations hopefully this week!

    Reply
Zach
July 17, 2014 @ 8:24 am

trigger LazyEmployee on Case (before insert) {
User u = [SELECT Id
FROM User
WHERE Name = ‘David Liu’
LIMIT 1];
for(Case c: Trigger.new){
c.Status = ‘Closed’;
c.OwnerId = u.Id;
}
}

Reply
Saravanan
July 14, 2014 @ 6:54 am

Hi ,
We have to remove the DML operation so this works fine i think

trigger LazyEmployee on Case (after insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
}
}

Reply
    Saravanan
    July 14, 2014 @ 6:59 am

    And make the trigger as before insert!!

    Reply
Saravanan
July 14, 2014 @ 6:52 am

Hi all,
Create a instance of the case inside the for loop so it will work fine!!!!! <3 <3

Reply
Jenny Bennett
July 12, 2014 @ 6:53 am

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (after insert) {
if (Trigger.isAfter && Trigger.isInsert) {
User davidLiu = new User();
davidLiu = [SELECT Id, isActive FROM User WHERE Name = ‘David Liu’ LIMIT 1];
if (davidLiu != null && davidLiu.isActive) {
List newCases = new List();
for (Case a : Trigger.new) {
if (UserInfo.getUserId() == davidLiu.Id) {
newCases.add(new Case(Id = a.Id, Status=’Closed’, OwnerId = davidLiu.Id));
}
}
if (newCases.size()>0){
update newCases;
}
}
}
}

Reply
Sidharth
July 11, 2014 @ 4:31 am

trigger LazyEmployee on Case (before insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerID = [Select name from User where name =’Sidharth Panda’].ID;
newCases.add(a);
}
update newCases;
}

Reply
    Sidharth
    July 11, 2014 @ 7:22 am

    trigger LazyEmployee on Case (before insert) {
    for (Case a : Trigger.new) {
    a.Status = ‘Closed’;
    a.OwnerID = [Select name from User where name =’Sidharth Panda’].ID;
    }
    }

    Reply
Elizabethiko
July 11, 2014 @ 2:32 am

Besides the ‘cosmetic’ mistakes, I would define following design mistakes:
1. best practice: use 1 trigger per object (so I would add all events related to case) and rename the trigger accordingly to CaseTrigger;
2. no business logic in trigger (use helper classes). For smaller project makes sense to use dispatcher method in helper class, for bigger objects it would make sense to use dispatcher class
3. I would avoid usage of string literals inside methods: constants or better custom settings. If the requirement changes to assign to another user then it’s just the matter of changing the custom setting and not re-deploying whole class.
4. also make sure that user should be active

Reply
Bhavik
July 10, 2014 @ 9:56 pm

1. after insert ?? something wrong
2. double quote for string value, it should be single quote
3. semi column @ add list

Reply
    Bhavik
    July 10, 2014 @ 10:04 pm

    1. after insert ?? something wrong
    2. double quote for string value, it should be single quote
    3. semi column @ add list
    4. Case owner will be auto populate
    5. No Case status like ‘Closed’

    Reply
Saketh
July 10, 2014 @ 11:09 am

Guys…… I would go for a workflow for such a simple requirement and never write a trigger

Reply
Balaji S
July 9, 2014 @ 2:17 pm

I see the following four errors

1. BEFORE insert instead of AFTER Insert – Both would do the job but using purpose would be more efficient and would avoid extra database calls depending on how much records are in the trigger

2. Use Single Quotes instead of Double Quotes when doing string assignment

3. Owner ID can’t be assigned as-is like a string. Need to get the SF ID and assign it to the Owner ID

4. Missing semi-colon at the end of newcases.add(a)

trigger LazyEmployee on Case (BEFORE insert) {
List newCases = new List();

//Assuming only one person David Liu exists in the org and picking the first one

User u1 = [Select ID from USER where name = ‘David Liu’ Limit 1];

for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.Owner = u1.ID;
newCases.add(a);
}
update newCases;
}

Reply
ramesh
July 9, 2014 @ 4:23 am

trigger LazyEmployee on Case (after insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
newCases.add(a);
}
update newCases;
}

Reply
    Anonymous
    July 25, 2014 @ 11:39 pm

    trigger LazyEmployee on Case (after insert) {
    List newCases = new List();
    for (Case a : Trigger.new) {
    a.Status = ‘Closed’;
    newCases.add(a);
    }
    update newCases;
    }

    Reply
Nitesh Gadkari
July 9, 2014 @ 2:32 am

// This lazy dude wrote code to auto-accept and close all cases!
trigger LazyEmployee on Case (after insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’; //single quotes should be used instead of double
a.Owner = ‘David Liu’; //single quotes should be used instead of double. Also not sure if owner can be written like this in
//the code

//this is required field so include this
a.Reason=’performance’;
newCases.add(a) ;// semi colon was missing
}
update newCases;
}

Reply
Viru
July 9, 2014 @ 12:42 am

Three things–

1. We have to use ‘ Before insert ‘ instead of ‘ After insert ‘, Because we don’t need Id of the case record and we just modify the record. So in case of ‘ before insert ‘ the record automatically gets inserted.

2. The status field of case object don’t have any value like ‘Status’, it has New, Working and Escalated.

3. Owner field is lookup to user object, we have to assign an id type of data not of string type.So first we fire a query on User object and get the id of a user which have name i.e. ‘David Liu ‘.

And the string is passed on single braces not double.

The code is –

trigger LazyEmployee on Case (before insert) {

User userName = [Select name from User where name =’Virendra Chouhan’];

for(case a : trigger.new){
a.Status = ‘New’;
a.ownerId = userName.id;

}

}

Viru

Reply
    Viru
    July 9, 2014 @ 3:20 am

    Sorry i got it there is available ‘Closed’ value of picklist.

    Reply
Stupid Dreamer
July 8, 2014 @ 10:27 pm

a.Status = “Closed”; ( should be comman not semi colon )
a.Owner = “David Liu”; ( should be comman not semi colon )
newCases.add(a) ( need a semi colon )

Reply
Janardhana Reddy
July 8, 2014 @ 11:31 am

trigger LazyEmployee on Case (before insert) {

//List newCases = new List();
if(Trigger.isinsert){
for(Case c : Trigger.new){
if(c.status != ‘closed’ && c.status != ”){
c.status = ‘closed’;
//c.Ownerid = u.id;
//newCases.add(c);
}
}
//update newCases;
}

Reply
mudasser
July 8, 2014 @ 10:25 am

trigger Lazy Employee on Case(before insert) //chosen before insert because we can’t perform write operations on after events
{
List cases=new List();//this list is to accept new cases which are going to insert

for(case c:Trigger.new)
{
if(c.ownerId==’David Lu’ && Trigger.isInsert)//if the trigger is going to be isinsert on insertion of a new record it is going to put status as closed and automatically gets updated by adding to the list
{
a.status=’closed’;
cases.add(a);
}
}
update cases;
}

Reply
    mudasser
    July 8, 2014 @ 10:59 am

    soryy i missed new List out there

    Reply
Tom B
July 8, 2014 @ 5:51 am

I see the map declared incorrectly, missing semi colon after the add method and update DML but the trigger is an Insert.

Just a note to mention about the site vs. email I received on this topic: The email showed everything I identified above but on the website the add method and update dml statement does not show as I am looking at it right now!

Reply
    Tom B
    July 8, 2014 @ 5:58 am

    Also some double quotes going on. Lastly — I think a.Owner may not work but I would have to confirm that.

    Reply
sethmpayne
July 8, 2014 @ 4:46 am

trigger LazyEmployee on Case (before insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.Owner = user.id;
newCases.add(a);
}
update newCases;
}

big fan of this….would love to see one every week if possible!

Reply
Kanav Khurana
July 8, 2014 @ 12:34 am

// This lazy dude wrote code to auto-accept and close all cases!
//Change 1: Change ‘after’ insert to ‘before’ insert to avoid a DML at the end
trigger LazyEmployee on Case (before insert) {
//List newCases = new List();

//Change 2: Query for the ID
Id userId = [SELECT Id from User where Name = ‘David Liu’ LIMIT 1].Id;
for (Case a : Trigger.new) {
a.Status = “Closed”;
//Change 3: OwnerId to be specified instead of Owner
//a.Owner = “David Liu”;
a.OwnerId = userId;

//newCases.add(a)
}
//update newCases;
}

Reply
Harshit Pandey (@OyeCode)
July 7, 2014 @ 11:33 pm

Here goes my code

trigger LazyEmployee on Case(before insert)
{
//In a standard Salesforce Scenario Case Owner is actually ownerID which ID field
// As per the requirement, I am assuming Owner as Text Field, basically a custom field

//Clearly Trigger needs to bulkified, I would have created bunch of Utilitiy Class to check for null values
//To handle Map values and set value , I mean repeted check, very easy to use and maintain (Good engineering)

//Make set of id to hold unique id, avoiding duplicates of case owners
Set ownerIds = new Set();

//populate set with unique values,
for(Case c : Trigger.New)
ownerIds.add(c.Id);

//Make a map that hold cases matching Id in the set we just populated
//This map will hold all data we need, we can query map and avoid querying salesforce
Map userToIdMap = new Map([Select Id, Owner, Status From Case Where Id in : ownerIds])

//Make a list of cases needs to updaed
List caseList = new List();

//Now we have map, we can avoid SOQL from here and we are getting awesomer now
//Loop through each case coming in
for(Case c : Trigger.New)
{
//close only if status is not closed, get status from Map , yay we avoid SOQL here
//but check to see if Map is not empty
if(userToIdMap.isNotEmpty())
{

//Close only if status is anything but closed
if(userToIdMap(c.Id).status != ‘Closed’)

{
c.Status=’Closed’;
//Check if ownerID is David Liu Id since in sample this is text field so it’s easy

// since requirement says close all cases and set owner as David regardless
if(userToIdMap(c.Id).Owner !=’David Liu’)

//set the Owner (if custom textfield, remember Id is not writeable Field)
c.Owner=’David Liu’;
//add only qualified values to list, this will keep list limited in size

caseList.add(c);
}

//User Try-Catch log the update, in case if exception occurs
try
{
//check if list is not empty or null
if(caseList.isNotEmpty() || caseList.Size()!=0 || caseList!=null)
update caseList;
}Catch(DMLException e)
{
System.Debug(‘Error Updating Records in LazyEmployee Trigger ‘+e.getMessage());
}
}
}

}

See gist for formatted code here : https://gist.github.com/mailtoharshit/e2ece54080e6ce12d195

Reply
    Dinesh dk
    July 30, 2015 @ 12:31 am

    simple and neat :-) plus one like for mohith kumar for his debugging skills ;-)

    Reply
Harshit Pandey (@OyeCode)
July 7, 2014 @ 11:24 pm

Few things I would have checked

1. Query the user, check if user exist (one query +1 that’s all we need)
2. If yes, dump the value in the map with Set to that unique values is thrown in (avoid duplicate +1 point)
3. Populate map with set values, boom trigger is bulkified ( +1 point)
4. Loop in for each value in map, update the value and add element to empty list
5. update list, if its not empty

Game over

Reply
    Mohith Kumar
    July 14, 2014 @ 10:39 pm

    I saw your gist and not sure how you will get Case Id in before insert trigger ?

    Reply
      Harshit Pandey (@OyeCode)
      July 31, 2014 @ 1:58 pm

      Great catch, my bad you need to insert record first to get Id, fixed my gist :-)

      Reply
Atul Gupta
July 7, 2014 @ 11:18 pm

trigger LazyEmployee on Case (after insert) {
List newCases = new List();
User userVar = [select id from User where Name = ‘David Liu’];
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = userVar.id;
newCases.add(a)
}
update newCases;
}

Reply
    Atul Gupta
    July 7, 2014 @ 11:19 pm

    trigger LazyEmployee on Case (after insert) {
    List newCases = new List();
    User userVar = [select id from User where Name = ‘David Liu’];
    for (Case a : Trigger.new) {
    a.Status = “Closed”;
    a.OwnerId = userVar.id;
    newCases.add(a)
    }
    update newCases;
    }

    Reply
Mahesh
July 7, 2014 @ 10:39 pm

trigger LazyEmployee on Case (before insert) {
User u = [SELECT Id FROM User WHERE Name = ‘David Liu’];
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = u.Id;
}
}

Reply
    Felix N
    July 8, 2014 @ 10:05 am

    Hi Mahesh,

    I think you have the right solution. I had retained the “List” in my attempt, but thinking about it, it does nothing in the code, and amounts to two unnecessary lines of code wasting memory space.

    Thanks.

    Reply
Felix N
July 7, 2014 @ 9:46 pm

trigger LazyEmployee on Case (before insert) {
User u = [SELECT Id FROM User WHERE Name = ‘David Liu’];
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = u.Id;
newCases.add(a);
}
}

Reply
Nitin
July 7, 2014 @ 9:23 pm

// Used – instead of ” in List command to avoid deletion

trigger LazyEmployee on Case (before insert) {
List-Case- newCases = new List-Case-();
User abc = [SELECT Id FROM User WHERE Name = ‘David Liu’];
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = abc.id;
newCases.add(a);
}
update newCases;
}

Reply
Raghvendra Rathore
July 7, 2014 @ 8:28 pm

trigger LazyEmployee on Case (before insert) {
User u = [SELECT Id FROM User where name = ‘David Liu’];
//List newCases = new List();
for (Case a : Trigger.new) {
a.Status = “Closed”;
//a.Owner = “David Liu”;
a.OwnerId = u.Id;
//newCases.add(a)
}
//update newCases;
}

Reply
Joey Chan
July 7, 2014 @ 8:27 pm

Please delete the other post.

trigger LazyEmployee on Case (before insert) {
Map usernameToIdMap = new Map();
for(User u : [Select Id, Name From User Where Name = ‘David Liu’]){
usernameToIdMap.put(u.Name, u.Id);
}

List newCases = new List();
for (Case a : Trigger.new) {
if(usernameToIdMap.containsKey(‘David Liu’)){
a.Status = ‘Closed’;
a.OwnerId = usernameToIdMap.get(‘David Liu’);
}
}
}

Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


*

*

Theme: Simple Style by Fimply