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
prathyusha kolanuprathyusha kolanu 

i could not post a new error post in trailhead comunity for help,

prathyusha kolanuprathyusha kolanu
public with sharing class AccountHandler {
    public static void CreateNewOpportunity(List<Account> accts) {
        for (Account a : accts) {
            Opportunity opp = new Opportunity();
            opp.Name = a.Name + 'Opportunity';
            opp.AccountId = a.Id;
            opp.StageName = 'Prospecting';
            opp.CloseDate = System.Today().addMonths(1);
            insert opp;
        }
    }
}

Hi, I am new to Salesforce... I am trying to follow this Module, https://trailhead.salesforce.com/modules/apex_basics_dotnet/units/execution_context but it throws duplicate error when i execute from Execute anonymous method.
Line: 6, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Use one of these records?: []

I want to post in Apex community but it says it already exists and gives me id number which do not match my question at all and i could not post a new question. Any help is appreciated.


 
trigger AccountTrigger on Account (before insert, before update, before
    delete, after insert, after update, after delete,  after undelete) {
    if (Trigger.isAfter && Trigger.isInsert) {
        AccountHandler.CreateNewOpportunity(Trigger.New);
    }
}
 
Account acct = new Account(
    Name='Test Account 4',
    Phone='(415)555-9090',
    NumberOfEmployees=31,
    BillingCity='San Francisco1');
insert acct;

 
SandhyaSandhya (Salesforce Developers) 
Hi,

Below is the code which worked for me
public class AccountTriggerHandler {
    
     public static void CreateAccounts (List<Account> accList)
     {
         for(Account acc : accList)
         {
             if(acc.ShippingState!=acc.BillingState)
             {
                 acc.ShippingState = acc.BillingState;
             }
         }
     }

}
 
trigger AccountTrigger on Account (before insert) 
{
    if (Trigger.isBefore && Trigger.isInsert) {
			AccountTriggerHandler.CreateAccounts(Trigger.new);
		}	
	}
 
@isTest
public class AccountTriggerTest {
    
    @isTest static void TestCreate200Records(){
        
        List<Account> accts = new List<Account>();
        for(Integer i=0; i < 200; i++) {
            Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA');
            accts.add(acct);
        }
        Test.startTest();
        insert accts;
        Test.stopTest();
        System.assertEquals(200, [SELECT Count() FROM Account WHERE ShippingState = 'CA' ]);
    }
}

https://developer.salesforce.com/forums/?id=906F0000000DBRhIAO
 
If this does not work, I would suggest you try in new trailhead playgrouns as there may be pre existing configuration that is stopping you to check challenge.

Please mark it as solved if my reply was helpful, it will make it available
for others as a proper solution.

Best Regards,
​Sandhya