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
Deepak Sharma 184Deepak Sharma 184 

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;
    }
}


User-added image
Best Answer chosen by Deepak Sharma 184
sfdcMonkey.comsfdcMonkey.com
hi Deepak Sharma 184,
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

sfdcMonkey.comsfdcMonkey.com
hi Deepak Sharma 184,
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
This was selected as the best answer
UC InnovationUC Innovation
Hi Deepak,

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.