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
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12 

i want to update records if newly entered name is consist in the previous records else insert 10 records with newly entered name

hai friends 
for this i wrote like this 


trigger upserttest on Account (before insert) {
    if(trigger.isInsert)
    {
        account acc1;
        List<Account> lisAcc=trigger.new;
        list<Account> incAcc=new List<Account>(); 
        for(Account acc:lisAcc)
        {
            List<Account> preacc=[select name from account where name=:acc.name];
            system.debug('333333333333'+preacc.size());
       
            if(preacc.size()>0)
            {
                system.debug('555555555'+preacc[0].name);
   
                for(Account prea:preacc)
                {
                    prea.name=prea.name+'ing';
                    update prea;
                }
            }
           
            else
            {
             if(trigger.isBefore)
                for(integer i=0;i<10;i++)
                {
                acc1=new account(name=acc.name);
                //acc.name=acc.name;
                insert acc1;
                }
                }
            }   
   
   
    }
    }

it is updating previous records but if newly entered name is not there it is not inserting 10 record of new name
please help me for this
rohitsfdcrohitsfdc
Ashwani,
You are trying to insert 10 accounts in before insert trigger. This will make this code go into recursive loop.

Also, the code that you have written is not the best way to write a code. Reason being:

1) You are using SOQL query in a loop
2) You are doing DML in for loop.

Please follow the salesforce best practices for coding provided in the link below:

https://developer.salesforce.com/page/Apex_Code_Best_Practices (https://developer.salesforce.com/page/Apex_Code_Best_Practices" target="_blank)
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
else
            {
             if(trigger.isBefore)
                for(integer i=0;i<10;i++)
                {
                acc1=new account(name=acc.name);
                //acc.name=acc.name;
                insert acc1;
                }
                }
            }
this is not working

then tell me how to insert 10 account in before insert with newly entered name
rohitsfdcrohitsfdc
Put them in a list and insert it. And use a static variable to prevent recursion