0

My Requirement is When Risk Form is Approved, Based on the Risk Score I want to create a task.

Scenario 1: If Task Score is less than 50 & More than 25 then Only 1 task (Subject Name: High Risk Review) should be created.

Scenario 2: If Risk Score is less than or equal to 25, then 2 Task should be created(Subject name: High Risk Review & Subject Name: Very High Risk Review)

If you can observe Scenario 2 then I am creating Scenario 1 Task and additional Very High Review Task.

Below is the code that I have written on After Update Event of Risk Form:

public  void createTaskAfterApproved(List<SObject> newItems ,Map<Id,SObject> oldItems){
boolean isHighRiskTaskCreated=false;
boolean isVeryHighRiskTaskCreated=false;
Map<Id,Risk_Form__c> riskAssessmentMap = new Map<Id,Risk_Form__c>();
Map<Id,Opportunity> opportunityMap=new Map<Id,Opportunity> ();
List<Task> taskList = new List<Task>();
set<string> optId=new Set<String>();
User genericUser=[Select Id,Name from User where Name='Test Admin'];
for(SObject newObj :newItems){
    Risk_Form__c rskasmnt = (Risk_Form__c )newObj;
    Risk_Form__c oldrskasmnt = (Risk_Form__c )oldItems.get(newObj.Id);
    if(rskasmnt.Status__c=='Approved' && rskasmnt.Final_Score__c<50){
        riskAssessmentMap.put(rskasmnt.Id,rskasmnt);
        optId.add(rskasmnt.Related_Opportunity__c);
    }
}
for(Opportunity opt:[Select Id,CloseDate from Opportunity where Id IN:optId]){
    if(opt.Id!=null){
        opportunityMap.put(opt.Id,opt);
    }
}
for(SObject newObj:riskAssessmentMap.values()){
    Date dueDate;
    Risk_Form__c rskassmnt = (Risk_Form__c )newObj;
    Risk_Form__c oldrskassmnt = (Risk_Form__c )oldItems.get(newObj.Id);
    if(rskassmnt.Final_Score__c<50){
        if(String.valueOf(opportunityMap.get(rskassmnt.Related_Opportunity__c).CloseDate)>String.ValueOf(opportunityMap.get(rskassmnt.Related_Opportunity__c).CloseDate.addDays(-21))){
            Task tsk = new Task();      
            //less than 3 weeks
            system.debug('less than 3 weeks');
            dueDate= System.today()+1;
            system.debug('dueDate less than 3 weeks'+dueDate);
        }
        else{
            system.debug('more than 3 weeks');
            dueDate = System.today()+21;
            system.debug('dueDate more than 3 weeks'+dueDate);
        }
        if(rskassmnt.Final_Score__c<50 && rskassmnt.Final_Score__c>25 && isHighRiskTaskCreated==false){              
            system.debug('High Risk Creating');
            isHighRiskTaskCreated=true;             
            tsk.OwnerID = genericUser.Id;
            tsk.ActivityDate=dueDate;
            tsk.Subject = 'High Risk  Review';
            tsk.WhatId=rskassmnt.Id;            
            taskList.add(tsk);

        }
        else if(rskassmnt.Final_Score__c<=25 && isVeryHighRiskTaskCreated==false){
            isVeryHighRiskTaskCreated=true
                system.debug('Very High Risk Creating');
            taskList.add(tsk);
            Task tsk2 = tsk.Clone();
            tsk2.Subject = 'Very High Risk Review';
            taskList.add(tsk2);
        }
    }
}
if(isHighRiskTaskCreated && isVeryHighRiskTaskCreated){
    system.debug('Inserting Task'); 
    insert taskList;
}

}

Above Code Failed in Below Scnerios:

Test Failed Scenario 1: If Risk Score is less than 50 and greater than 25,1 is created which fine but if I change the score and now if the score is 40 say,it creates duplicate Task(Subject Name:High Risk Review) but High Risk Review Task is already created so it should not created duplicate task.

Test Failed Scenario 2: If Risk Score is less than or equal 25, then 2 task(High Risk and Very High risk) created which is fine but of i change the score from 25 to 35 say, it creates duplicate task (High Review).

If you see the above code I am trying to restrict the scenario by using boolean flag but it is not working.I dont have any perticular condition where I can user Old object field value as I am changiong the score to.

My expectation is : If Score is less than 50 and greater than 25 then only 1 task and if I change the score and now score is less than or equal 25, then only Very High Risk task should be created as High Risk Task has already been created earlier.

Can someone please help what needs to be modified in above code?

9
  • Here you first need to fetch all Tasks related to Risk_Form__c present in your Trigger.new List having expected subject. Store this data in appropriate map structure i.e map of Risk_Form__c Id vs list of tasks details. While creating task do check if it is already present. Commented Feb 14, 2018 at 10:08
  • 1
    Additionally, as per your business requirement, you may need to delete the Task with subject Very High Risk task in case someone changes the score from 25 to 35. Commented Feb 14, 2018 at 10:11
  • Well when I put the debug to get the no of Task for Risk form then I get the zero result even after Task existed. system.debug('Task Count'+Risk_Form__c.Tasks.size()); one of the syntax that i used
    – SFDCDJ
    Commented Feb 14, 2018 at 10:11
  • Currently our client is not looking for to delete the task they just want to insert the task.
    – SFDCDJ
    Commented Feb 14, 2018 at 10:12
  • 2
    Boolean variables that you have used i.e isHighRiskTaskCreated and isVeryHighRiskTaskCreated won't help. As these are context-specific values more on it will break the logic when you'll be processing in Bulk. Commented Feb 14, 2018 at 11:07

1 Answer 1

1

Looking at your scenarios, Boolean variables that you have used i.e isHighRiskTaskCreated and isVeryHighRiskTaskCreated won't help. As these are context-specific values won't help to track the Task created in any previous transactions. More on it will break the logic when you'll be processing in Bulk as it will only create the task for the first record meeting criteria.

A possible solution for the same is, you first need to fetch all Tasks related to the Risk_Form__c's record present in your Trigger.new List having expected subject and Score is less than 50( your threshold value). Store this data in appropriate map structure i.e map of Risk_Form__c's Id vs list of tasks details. While creating task do check if it is already present. And create tasks accordingly.

Additionally, as per your business requirement, you may need to delete the Task with subject Very High Risk task in case someone changes the score from 25 to 35.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .