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 

Help: generating this task via apex code please

The below apex class creates a task when the field Ready_to_Generate_a_Firm_Quote__c == true, and attached the quote to the related Opportunity. he code currently does not send any task. How do I correct the problem?

Here is the 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 List<General_Spec_Sheet__c> gList {get; set;}
    public General_Spec_Sheet__c genss;
    public static General_Spec_Sheet__c gs {get; set;}
   
    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;
       System.Debug ('********' + oid );
      
       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)Controller.getRecord();
    }
   
    public List<Customer_Product_Line_Item__c> getresults() {
        if(cplist == null) {
            cplist = new List<Customer_Product_Line_Item__c>();
            System.Debug('&&&&&&&&&&&' + oid );
            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 list<General_Spec_Sheet__c> getgess(){
        if(glist == null) {
            glist = new List<General_Spec_Sheet__c>();
            System.Debug('&&&&&&&&&&&' + oid );
            for(General_Spec_Sheet__c c: [select id, name, Opportunity__c, Customer_Account__c, Seismic_Zone__c, Layout__c, PO_Number__c, App_Test__c,
                    Invoice_to_Street__c, Ship_To_Street__c, Invoice_to_City__c, Ship_to_City__c, Invoice_to_State__c, Ship_to_State__c, Invoice_to_Country__c,
                    Ship_to_Country__c, Invoice_to_Zip_Postal_Code__c, Ship_to_Zip_Postal_Code__c, Update_Invoice_Billing_Address__c, Update__c, Commercial_Contact__c,
                    Commercial_Contact_Phone__c, Commercial_Contact_Street__c, Commercial_Contact_Fax__c, Commercial_Contact_City__c, Commercial_Contact_Title__c,
                    Commercial_Contact_State__c, Commercial_Contact_Email__c, Commercial_Contact_Country__c, Update_Commercial_Contact_Record__c,Ready_to_Generate_a_Firm_Quote__c,
                    Commercial_Contact_Zip_Postal_Code__c, Technical_Contact__c, Phone__c, Update_Technical_Contact_Record__c, Fax__c, Title__c, E_mail__c, File_Type__c,
                    Drawing_Title__c, Customer_Specific_Standards__c, List_Standards__c, Ready_to_Generate_a_Budget_Quote__c, Requested_date_to_complete_Budget_Quote__c,
                    Requested_date_to_complete_Firm_Quote__c from General_Spec_Sheet__c where id =: gid limit 1]) {

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

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

    return pageRef;
    }


    public PageReference SendUpdatesandTask(){
   
    try{
   
    List<General_Spec_Sheet__c> g = [select id from General_Spec_Sheet__c where id =: gid];
   
    list<Account> acct = [select id from account where id =: aid];
   
    id eotm = [select id from user where Beusichem_Sales_Operations_Manager__c = true].id;
    List<OpportunityTeamMember> uotm = [SELECT Id, OpportunityId, TeamMemberRole, UserId FROM OpportunityTeamMember where TeamMemberRole='Product Specialist' and OpportunityID =: oid];

     update cplist;

//       try{
    update glist;
  //  }
//   catch(Exception e){
       
//   }


     If(glist[0].Update_Invoice_Billing_Address__c == 'Yes'){
         acct[0].BillingStreet = genss.Invoice_to_Street__c;
         acct[0].BillingCity = genss.Invoice_to_City__c;
         acct[0].BillingState = genss.Invoice_to_State__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';

     }
     System.debug('UOTM LIST VALUES :' + uotm);

     If(glist[0].Update__c == 'yes'){
         acct[0].ShippingStreet = genss.Ship_To_Street__c;
         acct[0].ShippingCity = genss.Ship_to_City__c;
         acct[0].ShippingState = genss.Ship_to_State__c;
         acct[0].ShippingCountry = genss.Ship_to_Country__c;
         acct[0].ShippingPostalCode = genss.Ship_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(glist[0].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;

        }

     If(glist[0].Ready_to_Generate_a_Budget_Quote__c == true ){
    
         Task ta = new Task();
          ta.whatID = oid;
          ta.subject='Generate a Budget Quote - U.S./Canada';
          ta.ActivityDate = genss.Requested_date_to_complete_Budget_Quote__c;
          ta.ownerid= uotm[0].userid;
              System.debug('2222' + ta.ownerid);
              
        Database.DMLOptions dmlo1 = new Database.DMLOptions();
        dmlo1.EmailHeader.triggerUserEmail = true;
        database.insert(ta, dmlo1);
   
     }
       
   If(glist[0].Ready_to_Generate_a_Firm_Quote__c == true){
   
         Task ta = new Task();
           ta.whatID = oid;        
           ta.subject='Generate a Firm Quote - U.S./Canada';
           ta.ActivityDate= genss.Requested_date_to_complete_Firm_Quote__c;
           ta.ownerid= uotm[0].userid;
          System.debug('2222' + ta.ownerid);
              
        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;
    }
    catch(Exception e){
       
    }
    return null;
    }

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

    return pageRef;
    } 
  
}

Thank you
Sonam_SFDCSonam_SFDC
When you execute this - are you seeing the Task ownerID which you are trying to print using system.debug: 
System.debug('2222' + ta.ownerid);

Wish to understand if the execution is going inside the IF loop.Also, what is the defalt value of Ready_to_Generate_a_Firm_Quote__c field?
ckellieckellie
No, I am not seeing the task owner Id. The debug log is not showing either if statements.