3

We are currently trying to populate a field on the opportunity based the opportunity having multiple tasks within which certain criteria are fulfilled.

The idea is: if more than one task has a certain field filled, we need a field on the opp to take that value.

The problem is: the field on each task, from which we need to take the value, could have a few different values across the tasks, and there will be more than one task on each opportunity. The field on each task will be a lookup.

We only want the opportunity field to be populated when two or more tasks have the same values in the field.

Does anyone have any ideas as to how this could be done? Thanks in advance for any help.

1
  • could you please update your question with the all values of the task that the criteria is going to depend
    – Suresh
    Apr 27 '15 at 14:34
0

Here I wrote most of the code , you may need to modify as I stated in the code.

Before going into the code let me brief what this is going to do. we are adding trigger on the task object. So when ever a new task added or updated and if that task is related to the opportunity this trigger fires and updates the opportunity.

Post comments if you need any assistance. This can also implemented using Maps as mentioned in another answer.

trigger updateOpportunity on Task(after insert, after update){

List<Opportunity> optyList= new List<Opportunity>();

if(trigger.isAfter && trigger.isInsert){
    string str; // to capture the value you want to populate on opportunity.

    //To keep track of how many tasks have the same value       
    integer countvalue1= 0;
    integer countvalue2= 0;
    integer countvalue3= 0;

    for(Task t:trigger.new){

        if(whatId == (yourOptyId field on Task){
            if(t.compareFieldAPIName ='Value1 in the picklist you want to compare' 
                && countvalue1 >= countvalue2 && countvalue1 >= countvalue3){
                countvalue1 ++;
                str = Value1 in the picklist you want to compare;
            }
            if(t.compareFieldAPIName ='Value2 in the picklist you want to compare'
                && countvalue2 >= countvalue1 && countvalue2 >= countvalue3){
                countvalue2++;
                str = Value2 in the picklist you want to compare;
            }
            if(t.compareFieldAPIName ='Value3 in the picklist you want to compare'
                && countvalue3 >= countvalue1 && countvalue3 >= countvalue2){
                countvalue2++;
                str = Value3 in the picklist you want to compare;
            }
            Opportunity op=new Opportunity();
            op.Id = yourOptyId field on Task;

            op.Field = str;             
        }
        optyList.add(op);
    }
    if(!optyList.isEmpty()){
        Insert optyList;
    }
}
if(trigger.isAfter && trigger.isUpdate){
    for(Task t:trigger.new){
        if(whatId == (yourOptyId field on Task && yourLookupValue != trigger.oldMap.get(t.Id).yourLookupValue){
            string str; // to capture the value you want to populate on opportunity.

            //To keep track of how many tasks have the same value       
            integer countvalue1= 0;
            integer countvalue2= 0;
            integer countvalue3= 0;

            if(t.compareFieldAPIName ='Value1 in the picklist you want to compare' 
                && countvalue1 >= countvalue2 && && countvalue1 >= countvalue3){
                countvalue1 ++;
                str = Value1 in the picklist you want to compare;
            }
            if(t.compareFieldAPIName ='Value2 in the picklist you want to compare'
                && countvalue2 >= countvalue1 && countvalue2 >= countvalue3){
                countvalue2++;
                str = Value2 in the picklist you want to compare;
            }
            if(t.compareFieldAPIName ='Value3 in the picklist you want to compare'
                && countvalue3 >= countvalue1 && countvalue3 >= countvalue2){
                countvalue2++;
                str = Value3 in the picklist you want to compare;
            }
            Opportunity op=new Opportunity();
            op.Id = yourOptyId field on Task;
            op.Field = str;             
        }
        optyList.add(op);
    }
    if(!optyList.isEmpty()){
        Update optyList;
    }
}

}

2
  • Suri, when you paste code into a post, please select the code afterward and click on the closed brackets icon {} for preformatted text so all of your code will be visible and legible. I've edited your post for you to make that happen. Also, @Obol doesn't have the reputation to comment on other posts and will only be able to post comments his post. Finally, I'm not certain how this answers his question as your code is on Task and not a solution for populating a roll-up type field for the max value.
    – crmprogdev
    Apr 27 '15 at 14:14
  • @Crmprogdev: I am going to modify my code to meet the statements you made, thank you.
    – Suresh
    Apr 27 '15 at 14:23
0

It sounds as though you need to create a trigger that's operates like a roll-up trigger. What your trigger would do is look for the criteria you specify (your post is unclear what those criteria are). The trigger would first collect the criteria values for the field(s) you specify and create a map using the collected values for the map key.

If there's more than one task that has the same value, the trigger would detect that when creating the keys (use a set) as it assembles the map. Depending on the task field value types, the map would likely be of type <string, integer>.

For each key in the map, the trigger would count the number of tasks that match each key's value and put the total into the map. The map key with the highest value that's >= 2 would be used to populate your custom field on Opportunity. If none are <= 2, no value is assigned to the field in Opportunity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.