+ Start a Discussion
bheemudu neelibheemudu neeli 

Need a help to create a Task by apex class

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) {
        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
            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()) {
            If( opp.recordtypeid==CBRenewal && opp.Amount>ur.Renewal_Threshold__c && opp.RenewalDate__c >= System.TODAY()){ 
                                 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';
           Insert Taskcreation;

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..