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
ckellieckellie 

Assign a task to an OpportunityTeamMember with apex code

In this class, i am updating the related opportunity record and sending a task to the product specialist listed on the opportunity team.

This list is not returning any records although I have confirmed a record is created that meets the criteria:

List<OpportunityTeamMember> uotm = [SELECT Id, OpportunityId, TeamMemberRole, UserId FROM OpportunityTeamMember where TeamMemberRole = 'Product Specialist' and OpportunityID =: oid];

How do I correct the query to capture the record in the list?


The complete class:

public with sharing class CreateGeneralSpecSheetListEdit{

    public CreateGeneralSpecSheetListEdit() {

    }

    ApexPages.StandardController GstdController;
    id oid;
    id aid;
    id gid;
    id eid;
    id tid;
    id cid;
    public List<Customer_Product_Line_Item__c> cpList {get; set;}
    public General_Spec_Sheet__c genss;
   
    public CreateGeneralSpecSheetListEdit(ApexPages.StandardController controller) {

       gid = [select id from General_Spec_Sheet__c Where id =:ApexPages.currentPage().getParameters().get('genid') or id =:ApexPages.currentPage().getParameters().get('id')].id;
       tid = [select Technical_Contact__r.id from General_Spec_Sheet__c Where id =:ApexPages.currentPage().getParameters().get('genid') or id =:ApexPages.currentPage().getParameters().get('id')].Technical_Contact__r.id;
       cid = [select Commercial_Contact__r.id from General_Spec_Sheet__c Where id =:ApexPages.currentPage().getParameters().get('genid') or id =:ApexPages.currentPage().getParameters().get('id')].Commercial_Contact__r.id;
       oid = [select Opportunity__r.id, Opportunity__r.Accountid from General_Spec_Sheet__c Where id =:ApexPages.currentPage().getParameters().get('genid') or id =:ApexPages.currentPage().getParameters().get('id')].Opportunity__r.id;
       aid = [select Opportunity__r.Accountid from General_Spec_Sheet__c Where id =:ApexPages.currentPage().getParameters().get('genid') or id =:ApexPages.currentPage().getParameters().get('id')].Opportunity__r.Accountid;
        GstdController = controller;
  
    this.genss = (General_Spec_Sheet__c)GstdController.getRecord();

    }

    public List<Customer_Product_Line_Item__c> getresults() {
        if(cplist == null) {
            cplist = new List<Customer_Product_Line_Item__c>();
            for(Customer_Product_Line_Item__c c: [select id, Name, Opportunity__c, Opportunity__r.SFDC_for_Quote__c,Opportunity__r.AIS__c,
                    Condition_1__c, Condition_2__c, Condition_3__c, Temperature_deg_f__c, Capacity__c, General_Application__c, Shape_1__c,
                    Shape_2__c, Defect_Description__c, Environment_temp_f__c, Capacity_per_lane__c, bulk_density__c, Opportunity__r.PS__c from
                    Customer_Product_Line_Item__c where Opportunity__c =: oid]) {

                cplist.add(c);
            }
        }
        return cplist;
    }

    public PageReference EditGS(){
     
   
   PageReference pageRef = new PageReference('/apex/GeneralSpecSheetEdit?genid=' + gid + '&sfdc.override=1');
         pageRef.setRedirect(true);

    return pageRef;
    }


    List<Opportunity> opp = [select id, Ready_to_Generate_new_Firm_Quote__c, Ready_to_Generate_a_Budget_Quote__c,Requested_date_to_complete_Firm_Quote__c,
                            sfdc_for_quote__c, Opportunity_Region__c, Requested_date_to_complete_Budget_Quote__c from Opportunity where id=:oid];
   
    list<Account> acct = [select id from account where id =: aid];
   
    id eotm = [select id from user where name = 'Sander van Heumann'].id;
    List<OpportunityTeamMember> uotm = [SELECT Id, OpportunityId, TeamMemberRole, UserId FROM OpportunityTeamMember where OpportunityID =: oid];

    public PageReference SaveList(){

        update cplist;

    PageReference pr = GstdController.save();

     If(genss.Update_Invoice_Billing_Address__c == 'Yes'){
         acct[0].BillingStreet = genss.Invoice_to_Street__c;
         acct[0].BillingCity = genss.Invoice_to_City__c;
         acct[0].BillingCountry = genss.Invoice_to_Country__c;
         acct[0].BillingPostalCode = genss.Invoice_to_Zip_Postal_Code__c;

        genss.Update_Invoice_Billing_Address__c = 'Updated';

     }

     If(genss.Update__c == 'yes'){
         acct[0].ShippingStreet = genss.Ship_To_Street__c;
         acct[0].ShippingCity = genss.Ship_to_City__c;
         acct[0].ShippingState = genss.Invoice_to_State__c;
         acct[0].ShippingCountry = genss.Invoice_to_Country__c;
         acct[0].ShippingPostalCode = genss.Invoice_to_Zip_Postal_Code__c;
       
        genss.Update__c = 'Updated';
    
        }

     update acct;
    
    List<Contact> com = new List<Contact>([select id from contact where id =: cid]);
    for(Contact co : com){
       if(genss.Update_Commercial_Contact_Record__c == 'Yes'){
         co.mailingstreet = genss.commercial_contact_street__c;
         co.mailingcity = genss.commercial_contact_city__c;
         co.mailingstate = genss.commercial_contact_state__c;
         co.mailingcountry = genss.commercial_contact_country__c;
         co.mailingpostalcode = genss.commercial_contact_zip_postal_code__c;
         co.phone = genss.phone__c;
         co.email = genss.e_mail__c;
         co.title = genss.title__c;
         co.fax = genss.fax__c;
        
         genss.Update_Technical_Contact_Record__c = 'Updated';
      

     update co;
        }
        }
    for(Opportunity o : [select id, Opportunity_Region__c, Ready_to_Generate_new_Firm_Quote__c, Requested_date_to_complete_Budget_Quote__c,
                            Ready_to_generate_a_Budget_Quote__c, Requested_date_to_complete_Firm_Quote__c from Opportunity where id =:oid]){
     o.Ready_to_Generate_a_Budget_Quote__c = genss.opportunity__r.Ready_to_Generate_a_Budget_Quote__c;
     o.Requested_date_to_complete_Budget_Quote__c = genss.opportunity__r.Requested_date_to_complete_Budget_Quote__c;
     o.Ready_to_Generate_new_Firm_Quote__c = genss.opportunity__r.Ready_to_Generate_new_Firm_Quote__c;
     o.Requested_date_to_complete_Firm_Quote__c = genss.opportunity__r.Requested_date_to_complete_Firm_Quote__c;
         

       
         Task ta = new Task();
           ta.whatID = o.id;
           ta.ActivityDate= o.Requested_date_to_complete_Firm_Quote__c;
               If(o.Opportunity_region__c == 'Europe' && o.Ready_to_Generate_new_Firm_Quote__c == true ){
                   ta.subject='Generate a Firm Quote - Europe';}
               If(o.Opportunity_region__c == 'Europe' && o.Ready_to_Generate_a_Budget_Quote__c == true ){
                   ta.subject='Generate a Budget Quote - Europe';}
               If(o.Opportunity_region__c <> 'Europe' && o.Ready_to_Generate_new_Firm_Quote__c == true ){
                   ta.subject='Generate a Firm Quote - U.S./Canada';}
               If(o.Opportunity_region__c <> 'Europe' && o.Ready_to_Generate_a_Budget_Quote__c == true ){
                   ta.subject='Generate a Budget Quote - U.S./Canada';}
           ta.Agreed_due_date__c = o.Requested_date_to_complete_Firm_Quote__c;
               If(o.Opportunity_region__c == 'Europe'){
                   ta.ownerid= eotm;}
               else{ta.ownerid= uotm[0].id;}
        Database.DMLOptions dmlo1 = new Database.DMLOptions();
        dmlo1.EmailHeader.triggerUserEmail = true;
        database.insert(ta, dmlo1);
        }
      
   PageReference pageRef = new PageReference('/apex/GeneralSpecSheetView?id='+gid+'&genid='+gid);
         pageRef.setRedirect(true);

    return pageRef;
    }

 
    public PageReference CanceltoOpp(){
   
   PageReference pageRef = new PageReference('/'+oid);
         pageRef.setRedirect(true);

    return pageRef;
    }   
 
    public PageReference CanceltoManage(){
   
   PageReference pageRef = new PageReference('/apex/GeneralSpecSheetView?id='+gid+'&genid='+gid);
         pageRef.setRedirect(true);

    return pageRef;
    }   
}

Thank you
Ashish_SFDCAshish_SFDC
Hi , 


Enable debug logs to see if you can find any clue where exactly the code is breaking. 

Reply back if you find the line and the code with the error on the debug logs to further investigate.

Also refer to the sample code to assign a task to an opportunity team member, 

Below is the sample code you can modify it according to your requirement.

trigger TaskOnOppTeamMember on OpportunityTeamMember (after insert) {
list<Task> NewTask = new list<Task>();
if(trigger.IsInsert)
{
for(OpportunityTeamMember oppTeam : trigger.new)
{
Task tasksInsert = new Task();
tasksInsert.WhatId = OppTeam.Opportunityid;
tasksInsert.OwnerId = oppTeam.Userid;
tasksInsert.Subject = ‘Great Daddy’;
tasksInsert.ActivityDate = date.today();
tasksInsert.Priority = ‘Normal’;
NewTask.add(tasksInsert);

}
}
Database.insert(NewTask);
}

http://rakeshistom.wordpress.com/2013/09/19/trigger-on-opportunity-team-member/


Regards,
Ashish
ckellieckellie
I have found and corrected the problem. Thank you