You need to sign in to do that
Don't have an account?

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