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
mdangondmdangond 

Trigger to notify user about Opportunity Sharing via an email message

Is there a way to send an email message to a user notifying them that they have been added to an opportunity sales team or that an specific opportunity is now being shared with them?

 

I tried doing this via workflow and was not able to. I then tried to do it via a trigger and later found out that the trigger would not work since it can only fire when the opportunity is being updated and it seems that the act of "sharing" is not considered an update on the opportunity object.

 

Here's my Trigger's code:

 

trigger NotifyOpportunitySharing on Opportunity (after insert, after update) {

 

EmailTemplate template = [ Select id From EmailTemplate WHERE DeveloperName = 'OpportunitySharingNotification' LIMIT 1 ];

//For Old Opportunity

 Set<Id> oldOppId = new Set<Id>(); for (Opportunity oldOpp : trigger.old){

oldOppId.add(oldOpp.Id);

}

List<OpportunityTeamMember> oldOtms = [Select Id, OpportunityId, UserId FROM OpportunityTeamMember WHERE OpportunityId in : oldOppId ];

 

//For new oppotunity

 Set<Id> oppId = new Set<Id>(); for (Opportunity opp : trigger.new){

oppId.add(opp.Id);

}

List<OpportunityTeamMember> Otms = [Select Id, OpportunityId, UserId FROM OpportunityTeamMember WHERE OpportunityId in : oppId ];

If (Otms.size() > 0){

for (OpportunityTeamMember oldOppTeam : oldOtms) {

for (OpportunityTeamMember oppTeam : otms) {System.debug(

'***oppTeam.UserId: '+oppTeam.UserId+ '****oldOppTeam.UserId :'+oldOppTeam.UserId);

if (oppTeam.UserId != oldOppTeam.UserId) {

//Send an email alert

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailmessage();

//User salesRep = [SELECT Email FROM User WHERE Id = :trigger.new[i].OpportunityTeamMember.UserId LIMIT 1];

//User salesRep = [SELECT Email FROM User WHERE Id = Otms.get(i).Id LIMIT 1];

//mail.templateId = template.Id;

//mail.targetObjectId = salesRep.Id;

//mail.targetObjectId = oppTeam.Id;

mail.setSubject('Trigger testing');

mail.setPlainTextBody('We are testing....please ignore....');String[] toAddress =

new String[] {'manuel.dangond@qlogic.com'};

mail.setToAddresses(toAddress);

mail.setSaveAsActivity(false);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

 

}

 

}

}

 

Any ideas or suggestions would be very much appreciated!

 

Thanks!

Message Edited by mdangond on 02-26-2010 08:35 AM