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
Irene SlessIrene Sless 

I need a link to the Opportunity when I create a task from a trigger - linked to Outlook

When I create an Opportunity Team Member with a specific role, I have a trigger that creates a Task for the Member who is assigned that role (see below). I also have Salesforce for Outlook set up and everything is working nicely in that the Task is created for the user, and the sync between SF and Outlook is working nicely. The only problem is the Outlook Task does not have a link back to the SF Opportunity. I need to somehow get the Opp Name in the Subject or some link back to the Opportunity so the person knows which one the task links to!

The Opp Id is not a very friendly value and the user receiving the notice wouldn't know what to do with it. Does my trigger have to get really complicated and need to do a lookup on the Opp in order to get that info? Is there any way I can insert a url link from the Outlook task back to the Opp in SF?


trigger OpportunityTeamMemberAfterInsert on OpportunityTeamMember (after insert, after update) {

    list<Task> taskNewTask= new list<Task>();
   
    if(trigger.IsInsert)
        {
        for(OpportunityTeamMember oppTeam : trigger.new){
            if (oppTeam.TeamMemberRole == 'Follow-up'){
                Task tasksInsert = new Task();
                tasksInsert.WhatId = OppTeam.Opportunityid;
                tasksInsert.OwnerId = oppTeam.Userid;
                tasksInsert.Subject = 'Follow-Up on Quote: ';
                tasksInsert.ActivityDate = date.today()+7;
                tasksInsert.Priority = 'Normal';
                tasksInsert.ReminderDateTime = date.today()+6;
                tasksInsert.IsReminderSet = true;

                taskNewTask.add(tasksInsert);
                }
            }
        }
    Database.insert(taskNewTask);
 }
Best Answer chosen by Irene Sless
BalajiRanganathanBalajiRanganathan
For first, use tasksInsert instead of taskNewTask

tasksInsert.Description = 'Opportunity Url : https://cs5.salesforce.com/' + OppTeam.Opportunityid;

Sorry for second option try with this

Map<Id, OpportunityTeamMember > oppMap = new Map<Id, OpportunityTeamMember>([select Opportunity.name  from OpportunityTeamMember  where id in :trigger.new ]);

tasksInsert.Description = 'Opportunity Name' + oppMap.get(OppTeam.id).Opportunity.name;

All Answers

BalajiRanganathanBalajiRanganathan
1) you can try setting the description for the task

taskNewTask.Description = 'Opportunity Url : https://<yourinstance>.salesforce.com/' + OppTeam.Opportunityid;

2) Or you can get the opportunity details and set it in the description
Add this line before for loop (for(OpportunityTeamMember oppTeam : trigger.new){)
Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([select name from Opportunity where id in :trigger.new ]);

and set the description as inside for loop
taskNewTask.Description = 'Opportunity Name' + oppMap.get(OppTeam.Opportunityid).name;
Irene SlessIrene Sless
Hi Balaji
Thanks - I've tried both but get errors - see below. What is wrong please?

1) [Error] Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Task> at line 17 column 17

               taskNewTask.Description = 'Opportunity Url : https://cs5.salesforce.com/' + OppTeam.Opportunityid;

2) [Error] Error: Compile Error: Invalid bind expression type of SOBJECT:OpportunityTeamMember for Id field of SObject Opportunity at line 7 column 107

    if(trigger.IsInsert)
        {
        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([select name from Opportunity where id in :trigger.new ]);
        for(OpportunityTeamMember oppTeam : trigger.new)
            {
            if (oppTeam.TeamMemberRole == 'Follow-up')
                {
           
                Task tasksInsert = new Task();
                tasksInsert.WhatId = OppTeam.Opportunityid;
                tasksInsert.OwnerId = oppTeam.Userid;
                tasksInsert.Subject = 'Follow-Up on Quote:' ;
                taskNewTask.Description = 'Opportunity Name' + oppMap.get(OppTeam.Opportunityid).name;
//                taskNewTask.Description = 'Opportunity Url : https://cs5.salesforce.com/' + OppTeam.Opportunityid;
                tasksInsert.ActivityDate = date.today()+7;

 
BalajiRanganathanBalajiRanganathan
For first, use tasksInsert instead of taskNewTask

tasksInsert.Description = 'Opportunity Url : https://cs5.salesforce.com/' + OppTeam.Opportunityid;

Sorry for second option try with this

Map<Id, OpportunityTeamMember > oppMap = new Map<Id, OpportunityTeamMember>([select Opportunity.name  from OpportunityTeamMember  where id in :trigger.new ]);

tasksInsert.Description = 'Opportunity Name' + oppMap.get(OppTeam.id).Opportunity.name;
This was selected as the best answer
Irene SlessIrene Sless
Thank you so much - works perfectly :)