You need to sign in to do that
Don't have an account?
Automatically link Task to Account
Our users are constantly forgetting to link tasks to both accounts and contacts. Those that remember hate doubling their work to do it. I've tried to write a trigger that would do the work for them, but I'm VERY new to the language (and object-oriented programming in general) and am having some troubles referencing the proper fields. I've coppied my current failed attempt below...maybe its not all a waste.
trigger AddOrgName on Task (before insert) { List<Task> newTask = [SELECT t.Who.name, t.Who.Account.Name, t.whatid FROM Task t WHERE t.WhatId = '' FOR UPDATE]; for (Task nt: newTask) { if(nt.WhatId == '') { nt.What = nt.Who.Account.Name; } } update newTask;}
Tasks are one of the special types of objects in salesforce which can have one lookup field to multiple types of objects, which can cause confusion and problems when working with Task Triggers, especially for beginners.
However, I'm not sure what your exactly tyring to accomplish.Based on your discreption, I don't see how a Trigger can solve your problem.
If you want to ensure users link both items (I.E. They enter both a Related To: Account and a Name: Contact ), I would suggest using Validation Rules to enforce users to fill in both data fields so they don't forget.
Since 99% of tasks here have a contact connected directly to the organization involved, I wanted to write a trigger that would look up the contact's organization and insert that into the "What" field when left empty. I have very few validation rules and our reps have been told about all of them, but I still get a lot of complaints and requests to give back the Quick Create function (which doesn't work with validation rules). As such, I'm reluctant to add any more validation rules, at least for the time being, so I wanted to just try to eliminate a step for them. One less field to fill out would make a huge difference to several of my not-so-computer savvy users.