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
santusfdc1.3926196430109492E12santusfdc1.3926196430109492E12 

need help with the trigger

Hi members ,

i have written a trigger on the oppportunity in the object if the stage is "closed won " i need to insert a record in the account.and i m getting the error and the error is (Error:Apex trigger opptrigger caused an unexpected exception, contact your administrator: opptrigger: execution of AfterUpdate caused by: System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account Name]: [Account Name]: Trigger.opptrigger: line 23, column 1)




trigger opptrigger on Opportunity (after insert,after update)
{
      //Create a list to hold all new records  
   List<Account> newRecords = new List<Account>();
  
    //Loop around all records in the trigger transaction 
    for(Opportunity theRecord : Trigger.new)
{
          //Evaluate the record against our critieria    
     if(theRecord.StageName == 'Closed Won')
{
              //The line below creates a new Account record and adds it to our list of new records. Add your field assigments (examples below). Make sure to assign all required fields.          
   Account newRecord = new Account();           
  //newRecord.Name = theRecord.name;        
     //newRecord.Description = 'My New Record';           
  newRecords.add(newRecord);       
   }   
  }     
//Insert the new records if any exist   
  if(newRecords.size() > 0)    
     upsert newRecords;
}
Best Answer chosen by santusfdc1.3926196430109492E12
Sameer PrasonnSameer Prasonn
Hi,

Since This code snippit show that you haven't assigned the required field for Account object. which is name. add newRecord.Name=[value] then everything works fine. hope this will help

All Answers

Sameer PrasonnSameer Prasonn
Hi,

Since This code snippit show that you haven't assigned the required field for Account object. which is name. add newRecord.Name=[value] then everything works fine. hope this will help
This was selected as the best answer
Sameer PrasonnSameer Prasonn
and yes it would be better if we can use insert inspite of upsert. since we are going to insert new record on opportunity stage. Hope this would resolve your query.
AneeshaAneesha
The error message typically means that you are not populating the required fields in the Account object while creating your new record.I see that you have commented out newRecord.Name. Name is a required field. Uncomment it and it should work.
santusfdc1.3926196430109492E12santusfdc1.3926196430109492E12
Hi SMRPRNBRTH & Aneesha,


Thanks for your valuable time in solving my issue . and the issue got solved.

Thanks once again. hope you ill be giving me the same assistance in future too.


regards,
Santosh
santusfdc1.3926196430109492E12santusfdc1.3926196430109492E12
please help me to write test class...for the above code.

Sameer PrasonnSameer Prasonn
for this, we need to use amend the oppportunityTest Class since this code is being execute once you update the oppportunity state to "closed won " and fire update. Hope this resolve your query