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
bheemudu neelibheemudu neeli 

Need a help to create a Task by apex class

Hello,
please help the below code

Underwriting_Opportunity_Owner__c  is lookup from opportunity to user.

public static void TaskFire(List<Opportunity> opps) { 
               if (opps == null) {
            return;
        }
        Set<Id> oppownerIds=new Set<Id>();
         for (Opportunity opp : opps) {
            if (opp.Underwriting_Opportunity_Owner__c != null) {    //we need to get under writing opportunity owner id here
                oppownerIds.add(opp.Underwriting_Opportunity_Owner__c);
            }
         }
            List<user> userlist = [select id, Renewal_Threshold__c from user where id in :oppownerIds]; 
            List<Task> Taskcreation = new list<task>();

            for(Opportunity opp : opps){
            if(userlist!=null && !userlist.isEmpty()) {
            for(User ur :userlist)
            if(opps!=null && !opps.isEmpty()) {
            system.debug('test1');
            If( opp.recordtypeid==CBRenewal && opp.Amount>ur.Renewal_Threshold__c && opp.RenewalDate__c >= System.TODAY()){ 
            system.debug('test2');
                                 Task accTask = new Task();                                                                
                                    accTask.WhatId = ur.Id;                                                    
                                    accTask.ActivityDate = system.today();
                                    accTask.Type = 'Follow Up';
                                    accTask.OwnerId = Underwriting_Opportunity_Owner__c.Id;
                                    accTask.Status = 'Not Started';
                                    accTask.Priority = 'Normal';
                                    accTask.Comments__c = 'test.';
                                    accTask.Subject = 'subjectTest';
                                   Taskcreation.add(accTask);
                                   system.debug('test3'+Taskcreation.Size()); 
                                   
                  }
              }
              }
           }
           Insert Taskcreation;
        }  


Thanks,
Bheem
Vijaya AmarnathVijaya Amarnath
Hi Beem,

You didn't mentione, what is the exact issue. But by seeing your code, I observed that in WhatId field you are passing User Id. Actually WhatId field will not refer to the User. WhoId field will refer to User/Contact/Lead. So, try to pass Opportunity Id in WhatId and User Id in WhoId and check. If this is not the issue, mention the issue which you are facing..