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
Kimberly Wargo 1Kimberly Wargo 1 

I have an Apex Trigger inmy org (written well before me) that is not making sense.

I have an Apex Trigger in my org (written well before me) that is not making sense. It is now causing some of my newer processes to fail.
I disabled it in the sandbox but have not yet pushed the change set over for fear of breaking something unkonw to me.
Here is my trigger:
trigger changeAssignTo on Task (before insert) {
     for (Task t : Trigger.new) {
        if (t.Subject.contains('{!') && t.Subject.contains('}')) {
           String lookup = t.Subject.substring(t.Subject.indexOf('{!') + 2, t.Subject.indexOf('}'));
           String[] lookupSplit = lookup.split('\\.');
           SObject s = Database.query('select ' + lookupSplit[1] + ' from ' + lookupSplit[0] + ' where Id = \'' + t.WhatId + '\'');
           t.OwnerId = (Id)s.get(lookupSplit[1]);
           t.Subject = t.Subject.substring(0, t.Subject.indexOf('{!')) + t.Subject.substring(t.Subject.indexOf('}') + 1); 
        }
     }    
}
If I deploy the change set and something fails, can I undo it?
And does this trigger make sense to anyone for any reason??
I don't want tasks reassinged when created.
Thanks..
 
sean*harrisonsean*harrison
You don't want this trigger.

Not only does it reassign Tasks, which you shared you don't want, I would call this trigger a security risk. It is allowing one to put an expression in the subject line that will dynamically do a DML query for another record. I'm sure intentions were good but this is naughty. Delete it.
karthikeyan perumalkarthikeyan perumal
Hello, 

Use below updated trigger code i added some validation like trigger context varariable and  nulll validation check for lookup string. 

check your test with this code and gohead and deploy this. is anything else kinldy let me know. 
 
trigger changeAssignTo on Task (before insert) {
if (Trigger.isBefore) {
 if (Trigger.isInsert) {
     for (Task t : Trigger.new) {
        if (t.Subject.contains('{!') && t.Subject.contains('}')) {
           String lookup = t.Subject.substring(t.Subject.indexOf('{!') + 2, t.Subject.indexOf('}'));
		   if(!String.isBlank(lookup)){
           String[] lookupSplit = lookup.split('\\.');
           SObject s = Database.query('select ' + lookupSplit[1] + ' from ' + lookupSplit[0] + ' where Id = \'' + t.WhatId + '\'');
           t.OwnerId = (Id)s.get(lookupSplit[1]);
           t.Subject = t.Subject.substring(0, t.Subject.indexOf('{!')) + t.Subject.substring(t.Subject.indexOf('}') + 1); 
		   }
        }
     }
	}
   }  	 
}

Hope this will helps you, 

Thanks
karthik

 
Kimberly Wargo 1Kimberly Wargo 1
Sean, thanks for that insight.. I have seen it and wondered what is what built for. Can you lend some additional info on what it's intent may have been? Also if I disable it, I am fearful of what else it may trigger or un-trigger for that matter. I am not a devit eloper I am an admin so I have disabled in my sandbox but hesitate to push to production. I appreciate any thoughts you have.
Thanks
Kim
sean*harrisonsean*harrison
What it does:
  1. Before a Task is inserted...
  2. look for something like "${Owner.Id}" in the subject line...
  3. if there, get the record related to this Task from the Related To field...
  4. query that related record...
  5. get the value of the field specified in the subject line...
  6. put that SAME value into the Owner field of this Task...
  7. remove the expression ""${Owner.Id}" from the subject line...
  8. done
As you know, the Related To field is a lookup to any of these types of objects: Contract,Campaign,Account,Opportunity,Product,Asset,Case,Solution,Mileage,Position,Candidate,Interviewer,Job Application,Offer,Position Approval Matrix,Review,Salary,Project,Release,Requirement,Ticket,Requirement,Ticket)

This allows a person or process to dynamically reassign the owner of a new task be based on a field set in one of these objects. The magic instruction in the subject line of the Task makes this possible.

So what users and processes in your org can create Tasks and set the subject line at their discretion?

> I am fearful of what else it may trigger or un-trigger for that matter.

It's not going to break anything from a *technical* standpoint, but you and I don't know what other business processes this auto-assignment supports. Opinions are inexpensive for those that give them but if I were your consultant I would tell you NOT to deploy this thing until either 1) you know exactly what process(es) this supports or 2) you've raised the concern with related parties.