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
srikanth kambampati 6srikanth kambampati 6 

I want to write a trigger on opportunity, and mu question is

Given I have an Opportunity and there are no open Mandatory sales tasks associated to it When I try and change the opportunity stage
then this shall be allowed by the system. Given I have an Opportunity and there are open Mandatory sales tasks associated to it
When I try and change the opportunity stagetThen the system shall throw a validation error.

trigger Opportunity_Task on Opportunity (before update) {
    List<Task> ts =new List<Task> ();
    List<Opportunity> op =new List<Opportunity>();
    Map<Id,Opportunity> oldMap = trigger.oldMap;
    Map<Id,Opportunity> newMap = trigger.newMap;
    List<id> optyid = new List<id>();
    for(id opid:oldMap.keySet()) {
        Opportunity op =new Opportunity();
        if(oldMap.get(opid).tasks == null && op.stageName=='Closed Won') {
            op.addError('You can create a new task for that Opportunity');
            task t = new task();
            t.WhatId=opid;
            t.Description='kjdkfhsdkf';
            t.Status='open';
            ts.add(t);
        }
        else{
            if(oldMap.get(opid).tasks!=null && op.StageName=='Closed Won')
            op.addError('You cannot modify the opportunity status');
            
        }
    }
    
}
Best Answer chosen by srikanth kambampati 6
Narender Singh(Nads)Narender Singh(Nads)
Hi,

Try this code:
trigger Opportunity_Task on Opportunity (before update) {
    if(trigger.new.size()==1){
        opportunity o=trigger.new[0];
        task[] tasklist=[select id, whatid from task where whatid=:o.id];
        if(tasklist.size()>0 && (o.stagename!=trigger.oldmap.get(o.id).stagename)){
            o.adderror('You cannot change the stage, because you have pending tasks');
        }
    }
}

Note: This code will not work incase you are mass updating opportunity. But it can be easily bulkified.

Let me know if it helps.
Thanks

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi,

Try this code:
trigger Opportunity_Task on Opportunity (before update) {
    if(trigger.new.size()==1){
        opportunity o=trigger.new[0];
        task[] tasklist=[select id, whatid from task where whatid=:o.id];
        if(tasklist.size()>0 && (o.stagename!=trigger.oldmap.get(o.id).stagename)){
            o.adderror('You cannot change the stage, because you have pending tasks');
        }
    }
}

Note: This code will not work incase you are mass updating opportunity. But it can be easily bulkified.

Let me know if it helps.
Thanks
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Srikanth,

Hope you find below code helpful. I have wrote a code with keeping in mind the status field of a task object but however, you can adjust to your own specific field which controls the mandatoriness.

Trigger Code:
Trigger OpportunityTaskValidation on Opportunity (Before Insert, Before Update) {
	
    List<Opportunity> comingOpps = New List<Opportunity>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate){
        For(Opportunity Opp : Trigger.New){
            If(Opp.StageName != Trigger.OldMap.get(Opp.Id).StageName)
            {
            	comingOpps.add(Opp);    
            }
        }
    }
    
    List<Task> NotCompletedTaskRecords = [SELECT Id, Status, WhatId FROM Task WHERE Status != 'Completed' AND WhatId =: comingOpps];
    
    For(Opportunity EveryOpp : comingOpps){
        For(Task EveryTask : NotCompletedTaskRecords){
            If(EveryTask.WhatId == EveryOpp.Id){
                EveryOpp.addError('You cannot change the stage when there is already one open task record');
            }
        }
    }
    
}

If this resolves the puzzle then please mark it as best answer!