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
designdesign 

Bulk Trigger: Too Many SOQL

Good day, 
first sorry for my english. 
I am a newbie and ask the council for writing bulk trigger.

I created a trigger, which should create a contact, account and assets for imports through the Data Loader.

 

Now I get an error: Too Many SOQL queries

 

I know that all queries need to move out of for


I request your assistance in this matter. 

 

Here is my trigger code:

 

trigger ImportAsset on Asset (before insert) {

 

List<Asset> asset = Trigger.new;

String accId;

for (Asset a : asset){

if (a.Import_AccountName__c != null){

List<Account> acc = [Select Id from Account where Name = :a.Import_AccountName__c];

for (Account account: acc) {

if (acc.size() == 0){

Account accnew;

accnew = new Account();

accnew.Name = a.Import_AccountName__c;

accnew.BillingCity = a.Import_AccountCity__c;

accnew.BillingStreet = a.Import_AccountAddress__c;

accnew.Phone = a.Import_AccountPhone__c;

accnew.Tel2__c = a.Import_AccountPhone2__c;

accnew.Website = a.Import_AccountUrl__c;

insert accnew;

accId = accnew.Id;

} else {

accId = account.Id;

}

}

List<Contact> contactLines = new List<Contact>();

if (a.Import_FirstName1__c != null){

Contact cnt1 = new Contact();

cnt1.FirstName = a.Import_FirstName1__c;

cnt1.LastName = a.Import_LastName1__c;

cnt1.Title = a.Import_Title1__c;

cnt1.Phone = a.Import_MobilePhone1__c;

cnt1.AccountId = accId;

contactLines.add(cnt1);

}

if (a.Import_FirstName2__c != null){

Contact cnt2 = new Contact();

cnt2.FirstName = a.Import_FirstName2__c;

cnt2.LastName = a.Import_LastName2__c;

cnt2.Title = a.Import_Title2__c;

cnt2.Phone = a.Import_MobilePhone2__c;

cnt2.AccountId = accId;

contactLines.add(cnt2);

}

if (a.Import_FirstName3__c != null){

Contact cnt3 = new Contact();

cnt3.FirstName = a.Import_FirstName3__c;

cnt3.LastName = a.Import_LastName3__c;

cnt3.Title = a.Import_Title3__c;

cnt3.Phone = a.Import_MobilePhone3__c;

cnt3.AccountId = accId;

contactLines.add(cnt3);

}

insert contactLines;

}

if (a.Import_ProductCode__c != null){

List<Product2> prod = [Select Id, Name from Product2 where ProductCode = :a.Import_ProductCode__c];

for (Product2 p: prod){

a.Name = p.Name;

a.Product2Id = p.Id;

a.AccountId = accId;

a.Status = 'Setup';

}

}

}

}

 

 I'm grateful for any help..

 

kamlesh_chauhankamlesh_chauhan
Hi,

You can use MAP and List object for bulk insert. if you don't know about it, let me know, I can write the code behalf of you.

Thanks,
Kamlesh
designdesign

Thanks for your reply,

 

I tried to use MAP, but without success.

 

And your code example would be a great way 

 

jeffdonthemicjeffdonthemic

Take a look at my blog post below. It should help you out. It references your error specifically with examples on how to prevent the error along with test cases.

 

Writing Bulk Triggers for Salesforce.com

 

Jeff Douglas

Appirio

http://blog.jeffdouglas.com 

designdesign
Thanks for the link, very informative! 

My problem is the search for an existing accounts. Can do this task by the trigger, if account quantity >200-300? 

Also the idea:
all assets temporarily imported for temporary account "Account For Import",  
 
next go to visualforce page with a controller:
 
1. select all of the assets created by today, owned by temporary account
2. runs through the "import" fields in assets and check/creating account, creating contacts for account
3. map this asset to new account
4. Delete all assets on temporary account
 
What solution do you think better? trigger or controller?
 
Message Edited by design on 07-24-2009 01:01 AM