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
Dhananjay Patil 12Dhananjay Patil 12 

Need a help to create a trigger..

Hello,
My configure with Outlook.when i add an email from outlook to salesforce then the task is created on a perticular contact which is the existing functionlity of application.
there is workflow rule we are using here,
Workflow Rule: ABCUpdate ~ Salesforce - Unlimited Edition

AND( ISPICKVAL(Owner:User.BusinessUnit__c ,"ABC"), $User.BypassWorkflow__c = FALSE, ISPICKVAL( BusinessUnit__c,""))

then we are updating a field "BusinessUnit__c " with value as ABC

this value is updated as soon as task is created.

Now as currently WF rule doesn't trigger when task is craeted via outlook which is a bug in salesforce.
I need to create a trigger for this.
Can someone help me how to create a trigger when i add an email from outlook to salesforce,in the task,it should update the field "BusinessUnit__c " with value as ABC

It would be better if is explain with code example as I am new in apex coding.


 
Best Answer chosen by Dhananjay Patil 12
Kelly KKelly K

Hi Dhananjay,

This is a lot simpler to do than the question here: https://developer.salesforce.com/forums/ForumsMain?id=906F000000095HT as it looks like you do not need to do anything with the who or what Id, you're just trying to update the task if the task comes over from Salesforce for Outlook.

We had a requirement when a email was logged by Salesforce for Outlook and it the subject started with "RE: " we wanted to update another field on the task to show it was a responded email.

You can keep in a simple before insert trigger like this (be sure to add any standard error handling that you need):

trigger TaskTrigger on Task (before insert, after insert, after update) {

    if(trigger.isBefore) {
        system.debug('task isBeforeTrigger');
        if(trigger.isInsert) {
                TaskUpdateActivityInfo.taskEmailConnection(trigger.new);
        }
    } 
}


And the class would look something like this:

public with sharing class TaskUpdateActivityInfo {

	//This method checks for tasks created with a Subject of 'Email:RE:' and marks the task to Email Connection = true
	public static void taskEmailConnection(List<Task> tasksForProcessing) {
		for(Task task : tasksForProcessing) {
			if(String.valueOf(task.Subject).startsWith('Email:RE:'))
				task.Email_Connection__c = true;
		}
	}
}


However, have you considered using a formula field? It looks like you're only trying to pull in information off of the user record. You can use something like this for the formula - but no code involved!

CASESAFEID(Owner:User.UserRole.Id)

Hope this helps,
Kelly

All Answers

ManojjenaManojjena
Hi Dhannajay ,
One thing fiest you need to check what is your Evaluation Criteria .if it is created -then you can write trigger with event before insert .
If it i screated and edited then you need to write trigger for both insert and update .
If it is created and edited and subsequiently meet the criteria the  you need to write in both insert and update and also you need to check old value and new value are not same in update .

After that based on your entry criteri and field update you need to write trigger context .

If you have stil issue post your wf entry criteria and field update detail.
Dhananjay Patil 12Dhananjay Patil 12
Hi Manoj,
Thanks for the quick response.
Criteria is when user create a contact.In our application every user belongs to some business unit.here Business unit is ABC.in "Task" object we have a picklist field called "Business Unit" which contains Business unit value.
Now If a user 'A' whose business unit is 'ABC'  create a contact and provide a valid email address which is configure with outlook.when I add an email from outllok to SFDC,in the perticular contact created by user A,in activity history task is shown.when i drilldown to the task it should display business unit as "ABC" (Because 'A' user belongs to Business Unit ABC )but WF rule is not triiger in case of outlook functionality.but if edit the task and save the task then Business Unit field is getting populated as per the WF rul criteria that I mentioned earlier(Check the bold text).
So here i want to write a trigger in such a way that when I add an email from outlook to SFDC,after creating a task when i drilldown to the task it should populate business unit field automatically..
Kelly KKelly K

Hi Dhananjay,

This is a lot simpler to do than the question here: https://developer.salesforce.com/forums/ForumsMain?id=906F000000095HT as it looks like you do not need to do anything with the who or what Id, you're just trying to update the task if the task comes over from Salesforce for Outlook.

We had a requirement when a email was logged by Salesforce for Outlook and it the subject started with "RE: " we wanted to update another field on the task to show it was a responded email.

You can keep in a simple before insert trigger like this (be sure to add any standard error handling that you need):

trigger TaskTrigger on Task (before insert, after insert, after update) {

    if(trigger.isBefore) {
        system.debug('task isBeforeTrigger');
        if(trigger.isInsert) {
                TaskUpdateActivityInfo.taskEmailConnection(trigger.new);
        }
    } 
}


And the class would look something like this:

public with sharing class TaskUpdateActivityInfo {

	//This method checks for tasks created with a Subject of 'Email:RE:' and marks the task to Email Connection = true
	public static void taskEmailConnection(List<Task> tasksForProcessing) {
		for(Task task : tasksForProcessing) {
			if(String.valueOf(task.Subject).startsWith('Email:RE:'))
				task.Email_Connection__c = true;
		}
	}
}


However, have you considered using a formula field? It looks like you're only trying to pull in information off of the user record. You can use something like this for the formula - but no code involved!

CASESAFEID(Owner:User.UserRole.Id)

Hope this helps,
Kelly

This was selected as the best answer
Dhananjay Patil 12Dhananjay Patil 12
Thank You So So Much Kelly :)
For providing this example.I have modified a code a bit and now this working as expected.