You need to sign in to do that
Don't have an account?
Hi, iam getting an error while creating an account, i have created a trigger named 'AddRelatedRecord' to create an opportunity with its associated account.
trigger AddRelatedRecord on Account (after insert, after update)
{
list<opportunity> opplst = new list<opportunity>();
Map<ID,Account> acctsWithOpps = new Map<ID,Account>([select id,(select id from opportunities) from Account where id IN: Trigger.New]);
for(Account a: Trigger.New)
{
if(acctsWithOpps.get(a.Id).Opportunities.Size()==0)
{
opplst.add(new opportunity(Name=a.Name + 'opportunity',StageName='Prospecting',CloseDate=System.today().addMonths(1),AccountId=a.Id));
}
}
if(opplst.size()>0)
{
insert opplst;
}
}

{
list<opportunity> opplst = new list<opportunity>();
Map<ID,Account> acctsWithOpps = new Map<ID,Account>([select id,(select id from opportunities) from Account where id IN: Trigger.New]);
for(Account a: Trigger.New)
{
if(acctsWithOpps.get(a.Id).Opportunities.Size()==0)
{
opplst.add(new opportunity(Name=a.Name + 'opportunity',StageName='Prospecting',CloseDate=System.today().addMonths(1),AccountId=a.Id));
}
}
if(opplst.size()>0)
{
insert opplst;
}
}
please update your code you miss a required field on opportunity is missing
trigger AddRelatedRecord on Account (after insert, after update)
{
list<opportunity> opplst = new list<opportunity>();
Map<ID,Account> acctsWithOpps = new Map<ID,Account>([select id,(select id from opportunities) from Account where id IN: Trigger.New]);
for(Account a: Trigger.New)
{
if(acctsWithOpps.get(a.Id).Opportunities.Size()==0)
{
opplst.add(new opportunity(
Name=a.Name + 'opportunity',
StageName='Prospecting',
CloseDate=System.today().addMonths(1),
Discount_Percent__c = 10 , // of any number
AccountId=a.Id));
}
}
if(opplst.size()>0)
{
insert opplst;
}
}
thanks,
Please mark it as solved if this helps you so that it will make for others as a proper solution
All Answers
please update your code you miss a required field on opportunity is missing
trigger AddRelatedRecord on Account (after insert, after update)
{
list<opportunity> opplst = new list<opportunity>();
Map<ID,Account> acctsWithOpps = new Map<ID,Account>([select id,(select id from opportunities) from Account where id IN: Trigger.New]);
for(Account a: Trigger.New)
{
if(acctsWithOpps.get(a.Id).Opportunities.Size()==0)
{
opplst.add(new opportunity(
Name=a.Name + 'opportunity',
StageName='Prospecting',
CloseDate=System.today().addMonths(1),
Discount_Percent__c = 10 , // of any number
AccountId=a.Id));
}
}
if(opplst.size()>0)
{
insert opplst;
}
}
thanks,
Please mark it as solved if this helps you so that it will make for others as a proper solution
According to your error message, you have a required field on opportunity called Discount Percent. It's failing because you're not populating that field in the opportunity in your trigger.
opplst.add(new opportunity(Name=a.Name + 'opportunity',StageName='Prospecting',CloseDate=System.today().addMonths(1),AccountId=a.Id));
Right here, just make sure that when you create your new opportunity that you also populate it with the Discount Percent field.
Another option is the make Discount Percent not a required field, which you can do by going to Setup -> Customize -> Opportunities -> Fields, look for the discount percent field and edit the field. There should be a checkbox that reads "Always require a value in this field in order to save a record". Uncheck it, and it will no longer be a required field.