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

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');
}
}
}
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');
}
}
}
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
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
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:
If this resolves the puzzle then please mark it as best answer!