You need to sign in to do that
Don't have an account?
Trouble Creating Bulk Trigger...
Hi. We're trying to bulkify a trigger that uses 2 fields of the inserted object to query and update fields on a Contact and Case. All of the examples that I've seen are very different and we don't understand how this would be done in a bulk manner.
Here is the non-bulk example of what we're trying to accomplish, but are getting the too many SOQL query errors when we test.
Any ideas?
trigger OrderLineTrigger on Order_Line_Item__c (after insert) { Case[] c; List<Case> casesToUpdate = new List<Case>(); for(Order_Line_Item__c orderLine : Trigger.new) { // Get contact info c = [Select Id, ContactId From Case Where Item_Id__c = :orderLine.Item_Id__c and User_Id__c = :[Select User_Id__c From Contact Where Id = :orderLine.Contact__c LIMIT 1].User_Id__c LIMIT 200]; if(c!=null && c.size()>0) { for(Integer j=0; j<c.size(); j++) { c[j].Related_To_Order__c = orderLine.Order__c; // Order lookup value c[j].Related_To_Order_Line__c = orderLine.Id; // Order Line lookup value c[j].SKU__c = orderLine.Product_Code__c; c[j].ContactId = orderLine.Infopia__Contact__c; // Contact Lookup value casesToUpdate.add(c[j]); } update casesToUpdate; } } }
Thanks,
Jon
Still bad guys
try this:
Set<Id> itemIds = new Set<Id>();
for(Order o : trigger.new)
{
itemIds.add(o.Item_id__c);
}
List<Case> caseList = new List<Case>([Select Id, Item_id__c, Related_order__c, Related_to_Order_Line__c, SKu__c,
contactId__c, Contact.Id, Contact.User_Id__c From Case Where Item_id__c In : itemIds ]);
for(Order o : trigger.new)
{
for(Case a : caseList)
{
if(a.id == o.Item_id__c && a.Contact.User_Id__c && o.User_Id__)
{
a.Related_order__c = o.order__c;
a.Related_to_Order_Line__c = o.Id;
a.SKu__c = o.Product_code__c;
a.contactId__c = o.infopia_contact__c;
}
}
}
please change where appropriate :0
All Answers
I am not sure if you could structure the SOQL to create a single query, but I do think you could probably create two maps, and reference them to avoid Governor limits...
Take a look at this example,or look at my other posts on maps - you should be able to achieve what you're after...
Hey
I haven't tested this but it's more or less what you want
trigger OrderLineTrigger on Order_Line_Item__c (after insert) { List<Case> cases = [Select Id, ContactId From Case];
List<Contact> contacts = [Select User_Id__c From Contact];
List<Case> casesToUpdate = new List<Case>(); for(Order_Line_Item__c orderLine : Trigger.new) {
contact con;
for(contact co: contacts){
if(co.id=orderLine.contact__c){
con = co;
break;
}
}
// Get contact info
List<Case> c = new List<Case>();
Integer count = 0;
for(Case ca: cases){
if(ca. Item_Id__c = orderLine.Item_Id__c && ca.User_Id__c= con.id){
c.add(ca);
count++;
}
if(count>199)
break;
} if(c!=null && c.size()>0) { for(Integer j=0; j<c.size(); j++) { c[j].Related_To_Order__c = orderLine.Order__c; // Order lookup value c[j].Related_To_Order_Line__c = orderLine.Id; // Order Line lookup value c[j].SKU__c = orderLine.Product_Code__c; c[j].ContactId = orderLine.Infopia__Contact__c; // Contact Lookup value casesToUpdate.add(c[j]); } update casesToUpdate; } }}
Cheers,
Wes
Still bad guys
try this:
Set<Id> itemIds = new Set<Id>();
for(Order o : trigger.new)
{
itemIds.add(o.Item_id__c);
}
List<Case> caseList = new List<Case>([Select Id, Item_id__c, Related_order__c, Related_to_Order_Line__c, SKu__c,
contactId__c, Contact.Id, Contact.User_Id__c From Case Where Item_id__c In : itemIds ]);
for(Order o : trigger.new)
{
for(Case a : caseList)
{
if(a.id == o.Item_id__c && a.Contact.User_Id__c && o.User_Id__)
{
a.Related_order__c = o.order__c;
a.Related_to_Order_Line__c = o.Id;
a.SKu__c = o.Product_code__c;
a.contactId__c = o.infopia_contact__c;
}
}
}
please change where appropriate :0
Great, thanks guys! I had to change it up a bit and add a Map for the Contacts, but everything is working great now.
Thanks,
Jon