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
randombardrandombard 

Insert Multiple Records

Hi All,

 

when an opportunity is closed won I want to insert on the account multiple records based on the number selected from a dropdown,

 

IE. Opp1 closed won Multi Select = 15

15 Account suport token records inserted on the account record.

 

the account tokens have auto number for the name. in the past I would have used do until and then just counted down until the insert = 0 but I expect that there is a better way to do this with apex.

 

Thanks in advance.

R

 

SFFSFF

I don't really understand why you would create Accounts from an Opportunity, but what you are looking for is a for-loop.

 

 

Integer counter = o.Counter;
list<Account> insertAccounts = new list<Account>();
for (Integer i = 0; i < counter; i++)
{
  insertAccounts.add(new Account(Name='Account ' + i.format()));
}
insert insertAccounts;

You would embed this in an after-insert trigger on the Opportunity, and verify that the Stage is correct and that a value has been entered for the "Counter" field before calling this code.

 

Hope this helps,

 

randombardrandombard

Hi Sorry my bad,

 

We are not creating accounts but the unique support tokens, they get a limited amount of support calles baed on their purchase and when they call in they need to submit that token at the same time.

 

However your post is really helpful and with minor tweeks will de exactly what i want thank  :)

 

R

randombardrandombard

I spoke too soon keep getting unexpected token at line 5

 

trigger CreateSupportToken on Opportunity (after insert, after update) {
     for(Opportunity o: trigger.new){ 
      if(o.isWon == true && o.How_Many_Tokens__c > 0){
Integer counter = o.How_Many_Tokens__c;
list<Support_Token__c> insert Support_Token__c = new list<Support_Token__c>();
for (Integer i = 0; i < counter; i++)
{
  insertSupport_Token__c.add(new Support_Token__c(Account__c= o.accountID));
}
insert insertSupport_Token__c;
}
}
}

 

 

Noam.dganiNoam.dgani

instead of:

insert Support_Token__c

 

do:

 

insertSupport_Token__c

Noam.dganiNoam.dgani

there is a problem with you trigger - you perform a DML inside a for loop, which will cuase you to hit governer limits quickly.

 

try this instead:

trigger CreateSupportToken on Opportunity (after insert, after update) {
     
	 map<Id,Integer> accId_to_numOfTokens = new map<Id,Integer>();
	 
	 for(Opportunity o: trigger.new)
	 { 
		if((trigger.isInsert || (trigger.isUpdate && o.isWon != trigger.oldMap.get(o.Id).isWon)) &&  
		o.isWon == true && o.How_Many_Tokens__c > 0)
		{
			if(accId_to_numOfTokens.containsKey(o.AccountId))
			{
				integer current_num_of_tokens_for_account = accId_to_numOfTokens.get(o.AccountId);
				current_num_of_tokens_for_account += o.How_Many_Tokens__c;
				accId_to_numOfTokens.remove(o.AccountId);
				accId_to_numOfTokens.put(o.AccountId,current_num_of_tokens_for_account);
			}
			else
			{
				accId_to_numOfTokens.put(o.AccountId,current_num_of_tokens_for_account);
			}
		}
	}			
	 
	 if(!accId_to_numOfTokens.isEmpty())
	 {
		list<Support_Token__c> tokens = new List<Support_Token__c>();
		
		for(Id accId : accId_to_numOfTokens.keyset())
		{
			tokens.add(new Support_Token__c(Account__c = accIds);
		}
		
		insert tokens;
	 }
}

 

it should work for you - but i do get the feeling that "isWon" is a formula field - which doesnt fire triggers (problem).

am i right?

randombardrandombard

wow that looks the buisness but

 

Variable does not exist: current_num_of_tokens_for_account at line 19 column 54

 


Do I need to declare it as an integer?

randombardrandombard

Ok so close with some modification i have got this to at least save however it only created one token no matter how may are set to be created

 

trigger CreateSupportToken on Opportunity (after insert, after update) {
     
     map<Id,Integer> accId_to_numOfTokens = new map<Id,Integer>();
     
     for(Opportunity o: trigger.new)
     { 
        if((trigger.isInsert || (trigger.isUpdate && o.isWon != trigger.oldMap.get(o.Id).isWon)) &&  
        o.isWon == true && integer.valueof(o.How_Many_Tokens__c) > 0)
        {
            if(accId_to_numOfTokens.containsKey(o.AccountId))
            {
                integer current_num_of_tokens_for_account = accId_to_numOfTokens.get(o.AccountId);
                current_num_of_tokens_for_account += integer.valueof(o.How_Many_Tokens__c);
                accId_to_numOfTokens.remove(o.AccountId);
                accId_to_numOfTokens.put(o.AccountId,current_num_of_tokens_for_account);
            }
            else
            {
                integer current_num_of_tokens_for_account = accId_to_numOfTokens.get(o.AccountId);
                accId_to_numOfTokens.put(o.AccountId,current_num_of_tokens_for_account);
            }
        }
    }           
     
     if(!accId_to_numOfTokens.isEmpty())
     {
        list<Support_Token__c> tokens = new List<Support_Token__c>();
        
        for(Id accId : accId_to_numOfTokens.keyset())
        {
            tokens.add(new Support_Token__c(Account__c = accId));
        }
        
        insert tokens;
     }
}