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!
P.S. Thanks to @Steph_Herrera for this great idea!!
// 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’;
}
}
// 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;
}
// 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;
}
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;
}
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;
}
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;
}
//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’;
}
}
}
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);
}
}
}
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
trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}
// “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.
}
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.
// 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”;
}
}
trigger CloseCase on Case (before insert) {
for(case c: Trigger.new){
c.status = ‘Closed’;
c.ownerid = ‘1234567890abcx’;
}
}
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;
}
trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = ‘123242353454656’;
}
}
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
// 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;
}
1.semicolon missing after newCases.add(a)
2. Inverted commas
3. Ownerid
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;
}
trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}
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;
}
//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;
}
And missed this c.ownerId = ‘005************’;
trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}
trigger name on Leads(before insert ,before update) {
for(Leads a:Trigger.new)
{
a.status = ‘closed’;
a.Ownerid = ‘0057F0000018aoe’;
}
}
// 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
}
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);
}
}
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’;
}
}
// 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;
}
trigger LazyEmployee on Case (before insert) {
for (Case c : Trigger.new) {
c.Status = ‘Closed’;
c.Ownerid = ‘00528000001szCO’;
}
}
trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “18 digitsId”;
}
}
// 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”;
}
}
// 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’;
}
}
// 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;
}
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.
}
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 ;
}
}
trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}
trigger LazyEmployee on Case (before insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = “Closed”;
a.Owner = “David Liu”;
}
}
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.
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;
}
}
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;
}
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;
}
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;
}
}
Limit 1 does not ensure the correct David Liu is the appropriate user to assign.
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
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;
}
// 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”;
}
}
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.
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;
}
// 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;
}
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;
}
we have to use fir update isUpadate()
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;
}
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
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;
}
}
oops sry here new is equal to closed
trigger LazyEmployee on Case (before insert) {
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
a.OwnerId = “David Liu”;
newCases.add(a)
}
}
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;
}
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;
}
1. String should enclosed in single quotes. 2. If we are using List then we can add an element to it not update
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.
I’ll have the answers with explanations hopefully this week!
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;
}
}
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”;
}
}
And make the trigger as before insert!!
Hi all,
Create a instance of the case inside the for loop so it will work fine!!!!! <3 <3
// 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;
}
}
}
}
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;
}
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;
}
}
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
1. after insert ?? something wrong
2. double quote for string value, it should be single quote
3. semi column @ add list
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’
Guys…… I would go for a workflow for such a simple requirement and never write a trigger
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;
}
trigger LazyEmployee on Case (after insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
newCases.add(a);
}
update newCases;
}
trigger LazyEmployee on Case (after insert) {
List newCases = new List();
for (Case a : Trigger.new) {
a.Status = ‘Closed’;
newCases.add(a);
}
update newCases;
}
// 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;
}
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
Sorry i got it there is available ‘Closed’ value of picklist.
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 )
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;
}
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;
}
soryy i missed new List out there
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!
Also some double quotes going on. Lastly — I think a.Owner may not work but I would have to confirm that.
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!
// 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;
}
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
simple and neat :-) plus one like for mohith kumar for his debugging skills ;-)
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
I saw your gist and not sure how you will get Case Id in before insert trigger ?
Great catch, my bad you need to insert record first to get Id, fixed my gist :-)
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;
}
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;
}
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;
}
}
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.
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);
}
}
// 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;
}
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;
}
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’);
}
}
}