You need to sign in to do that
Don't have an account?
Suneel B 9
create opportunity for an account record if activate field in account is YES and if its blank or no than it has to throw an error.
Hello guys, i am trying to create an opportunity for an account record only if the acoount record active is YES and if active is no or the field is blank it has to throw an error. Below is the code, can you pelase help me out,
trigger oppactive on Opportunity (before insert)
{
set<id> accid = new set<id>();
List<Opportunity> opp = trigger.new;
Integer i;
for(opportunity opp1:opp)
{
accid.add(opp1.Accountid);
system.debug('Test 1'+accid);
}
List<opportunity> opp3 = [select id, Name, opportunity.Accountid, opportunity.Account.Active__c from Opportunity where Accountid in: accid];
for(opportunity opp2:opp3)
{
system.debug('Enter loop'+opp2);
if(opp2.Account.Active__c =='No')
{
Trigger.new[0].adderror('Account must be active before creating an account');
system.debug('test3'+opp2);
}
}
}
trigger oppactive on Opportunity (before insert)
{
set<id> accid = new set<id>();
List<Opportunity> opp = trigger.new;
Integer i;
for(opportunity opp1:opp)
{
accid.add(opp1.Accountid);
system.debug('Test 1'+accid);
}
List<opportunity> opp3 = [select id, Name, opportunity.Accountid, opportunity.Account.Active__c from Opportunity where Accountid in: accid];
for(opportunity opp2:opp3)
{
system.debug('Enter loop'+opp2);
if(opp2.Account.Active__c =='No')
{
Trigger.new[0].adderror('Account must be active before creating an account');
system.debug('test3'+opp2);
}
}
}
Hi Suneel,
You are querying on Opportunity, but the triggers fires on before insert, so it will not return the Opportunity which is not yet inserted. Use the below code: If this post helps, mark this as best answer so will help others in future.
Thanks,
Neetu