• Anthony Zirilli 10
  • NEWBIE
  • 40 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 13
    Replies
I have this trigger below that works to update a field with the active opportunity, but when I do a mass upload and I go to the account the field is blank. When I click edit and save the field is populated again with the active opportunity. The mass upload does not throw any errors and I belive the code is bulkified. Any ideas of why this is happening and how to fix it?. Thank you!
trigger act_opp_membership on Account (before insert, before update) {
    List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Name,AccountId FROM Opportunity WHERE AccountID in : trigger.new 
AND ((RecordType.Name = 'Membership Renewal' AND Active_Membership__c = 'Active') OR (RecordType.Name = 'Membership Prospect' AND Web_Expire_Date__c != null)
OR (RecordType.Name = 'Hill Site License' AND Membership_Web_Access__c=TRUE AND Membership_Start_Date__c<=today AND Membership_Expire_Date__c>=today)
OR (RecordType.Name = 'Complimentary Access' AND Membership_Web_Access__c=TRUE AND Membership_Start_Date__c<=today AND Membership_Expire_Date__c>=today))
ORDER BY Web_Expire_Date__c DESC NULLS LAST LIMIT 1]);
    Map<Id,Opportunity> accIdwithOpp = new Map<Id,Opportunity>();
    
    for(Opportunity opp : oppList){
        if(!accIdwithOpp.containsKey(opp.AccountId)){
            accIdwithOpp.put(opp.AccountId,opp);
        }
    }
    
    for(Account acc : trigger.new){
        if(accIdwithOpp.containsKey(acc.id)){
            acc.Active_Membership_Opportunity__c = accIdwithOpp.get(acc.id).id;
        }
        else{
            acc.Active_Membership_Opportunity__c = null;
        }
    }
}

 
Hi there, 

I made 3 seperate triggers to update 3 different fields on the account level. How do I combine them into one trigger for best practices. Code is below. Thanks for the help.
trigger act_opp_subscrip on Account (before insert, before update) {
    List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Name,AccountId FROM Opportunity WHERE AccountID in : trigger.new 
AND ((RecordType.Name = 'Subscription Renewal' AND Subscription_Status__c = 'Active') OR (RecordType.Name = 'Subscription Prospect' AND Web_Expire_Date__c != null))]);
    Map<Id,Opportunity> accIdwithOpp = new Map<Id,Opportunity>();
    
    for(Opportunity opp : oppList){
        if(!accIdwithOpp.containsKey(opp.AccountId)){
            accIdwithOpp.put(opp.AccountId,opp);
        }
    }
    
    for(Account acc : trigger.new){
        if(accIdwithOpp.containsKey(acc.id)){
            acc.Active_Subscription_Opportunity__c = accIdwithOpp.get(acc.id).id;
        }
        else{
            acc.Active_Subscription_Opportunity__c = null;
        }
    }
}
 
trigger act_opp on Account (before insert, before update) {
    List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Name,AccountId FROM Opportunity WHERE AccountID in : trigger.new 
AND ((RecordType.Name = 'Membership Renewal' AND Active_Membership__c = 'Active') OR (RecordType.Name = 'Membership Prospect' AND Web_Expire_Date__c != null))]);
    Map<Id,Opportunity> accIdwithOpp = new Map<Id,Opportunity>();
    
    for(Opportunity opp : oppList){
        if(!accIdwithOpp.containsKey(opp.AccountId)){
            accIdwithOpp.put(opp.AccountId,opp);
        }
    }
    
    for(Account acc : trigger.new){
        if(accIdwithOpp.containsKey(acc.id)){
            acc.Active_Membership_Opportunity__c = accIdwithOpp.get(acc.id).id;
        }
        else{
            acc.Active_Membership_Opportunity__c = null;
        }
    }
}
 
trigger act_opp_comms on Account (before insert, before update) {
    List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Name,AccountId FROM Opportunity WHERE AccountID in : trigger.new 
AND ((RecordType.Name = 'Communications Council Renewal' AND Comms_Status__c = 'Active') OR (RecordType.Name = 'Communications Council Prospect' AND Web_Expire_Date__c != null))]);
    Map<Id,Opportunity> accIdwithOpp = new Map<Id,Opportunity>();
    
    for(Opportunity opp : oppList){
        if(!accIdwithOpp.containsKey(opp.AccountId)){
            accIdwithOpp.put(opp.AccountId,opp);
        }
    }
    
    for(Account acc : trigger.new){
        if(accIdwithOpp.containsKey(acc.id)){
            acc.Active_Comms_Opportunity__c = accIdwithOpp.get(acc.id).id;
        }
        else{
            acc.Active_Comms_Opportunity__c = null;
        }
    }
}

 
Hi there, 

I desperatly need help tweaking this code. It works to pull in the active opportunity on the account level, but does not work when doing mass updates/uploads. I have the understading that the querry needs to be moved outside the loop but don't know how to do that exactly.
trigger act_opp on Account (before insert, before update) {

map<Id, Account> acctmap = new map<Id, Account>();
for(Account a : trigger.new){
    acctmap.putAll([SELECT Id, (SELECT Id, Name FROM Opportunities WHERE (RecordType.Name = 'Membership Renewal' AND Active_Membership__c = 'Active') OR (RecordType.Name = 'Membership Prospect' AND Web_Expire_Date__c != null) LIMIT 1) FROM Account WHERE Id =: a.Id]);}
    
for(Account a : trigger.new){
    if(acctmap.get(a.Id).Opportunities.size() != 0){
        Opportunity opp = acctmap.get(a.Id).Opportunities[0];
        a.Active_Membership_Opportunity__c = opp.Id;} 
    else {a.Active_Membership_Opportunity__c = null;}
}
}

Any help would be greatly appreciated. Thank you!
Hi there, 

I've finished my code, but am trying to delpoy it into production and am a little stuck on how to do that as this is my first time. Could someone help me write a test class for the 2 triggers below. Thanks!

Trigger #1
trigger CreatePrintCopyLineItem on Print_Copy__c (after insert,after update) 
{

    List <Print_Copy_Line_Item__c> LineItemToInsert = new List <Print_Copy_Line_Item__c> ();
    if(TriggerHandler.isFirstTime)
    {
        TriggerHandler.isFirstTime = false;
        
        for (Print_Copy__c PrintCopy : Trigger.new) 
        {
            if(PrintCopy.Copy_Count__c > PrintCopy.Total_Number_of_Line_Items__c) 
            {
                Decimal count = PrintCopy.Copy_Count__c - PrintCopy.Total_Number_of_Line_Items__c;

                for(integer i=0;i < count; i++)
                {
                    Print_Copy_Line_Item__c LineItemToAdd = new Print_Copy_Line_Item__c ();
                    LineItemToAdd.Print_Copy__c=PrintCopy.Id;
                    LineItemToInsert.add(LineItemToAdd);
                }
            }      
        }
        
        if(LineItemToInsert.size() > 0)
        {
            insert LineItemToInsert;
        }
    }   
}
Trigger #2

trigger DeletePrintCopyLineItem on Print_Copy__c (after update,after delete) {
    set<Id>setID=new set<ID>();
    for(Print_Copy__c PrintCopy : Trigger.old)
    {
        setID.add(PrintCopy.id);
    
    }
    if(setId.size()>0)
    {
        List<Print_Copy_Line_Item__c> existinglineitems = [Select Id from Print_Copy_Line_Item__c Where Print_Copy__r.Delete_Line_Items__c=True and Print_Copy__c IN :setId];
        if(existinglineitems !=null && !existinglineitems.isEmpty())
        {
         delete existinglineitems;
        }
    }
    }
Hi there, 

I just wrote this simple code to delete line item records when a checkbox "Delete Line Items" is true. The only problem is that the querry goes over the governor limits when doing bulk uploads. How would I rework this so that all Print Copy Line items associated with the Print Copy object are deleted when the "Delete Line Items" checkbox is true (which is located on the Print Copy Object). Thank you in advance. 

Best,
Anthony

trigger DeletePrintCopyLineItem on Print_Copy__c (after update,after delete) {
    List<Id>LstID=new List<ID>();
    for(Print_Copy__c PrintCopy : Trigger.old){
    List<Print_Copy_Line_Item__c> existinglineitems = [Select Id from Print_Copy_Line_Item__c Where Print_Copy__r.Delete_Line_Items__c=True];
    delete existinglineitems;
    }
    }
Hi there, 

I've been trying to create line items on a parent object after the parent obect is created or edited. I got this far with the code and it works great. The only problem is that when using data loader to insert records it throws out this erros: "Too many SOQL queries: 101 Status code: 16" . How would I bulkify this code to make it work with large uploads? 

Additional details. The code runs so that the roll up field "Total Number of Line Items" equals "Copy Count" as shown below. Thanks in advance! 
-Anthony
User-added image

trigger CreatePrintCopyLineItem on Print_Copy__c (after insert,after update) {

    List <Print_Copy_Line_Item__c> LineItemToInsert = new List <Print_Copy_Line_Item__c> ();
    
    for (Print_Copy__c PrintCopy : Trigger.new) {

        if(PrintCopy.Copy_Count__c > PrintCopy.Total_Number_of_Line_Items__c) {
            Decimal count = PrintCopy.Copy_Count__c - PrintCopy.Total_Number_of_Line_Items__c;

            for(integer i=0;i < count; i++)
            {
                Print_Copy_Line_Item__c LineItemToAdd = new Print_Copy_Line_Item__c ();
                LineItemToAdd.Print_Copy__c=PrintCopy.Id;
                LineItemToInsert.add(LineItemToAdd);
            }
         }      
    }
    
    if(LineItemToInsert.size() > 0)
        insert LineItemToInsert;

Hi there, 

I'm a little stuck on how to create a number of new child records on a parent object. I've been able to do it below a little half haphazardly by having the trigger fire multiple times until my two fields, “copy count” and a roll up field “Total Number of Line Items” are equal. Any insight on how to approach this to create the multiple line item records when Copy Count is less than the Total Number of Line Items so that copy count is equal to total number of line items. Thank you in advance.
Anthony
 
Current trigger below (When copy count increases by 15, "maximum trigger depth excedded" error is tripped):

trigger CreatePrintCopyLineItem on Print_Copy__c (before insert,after update) {

    List <Print_Copy_Line_Item__c> LineItemToInsert = new List <Print_Copy_Line_Item__c> ();
    
    for (Print_Copy__c PrintCopy : Trigger.new) {
    
       
       if (PrintCopy.Copy_Count__c>PrintCopy.Total_Number_of_Line_Items__c) {
       
       Print_Copy_Line_Item__c LineItemToAdd = new Print_Copy_Line_Item__c ();
       
       LineItemToAdd.Print_Copy__c=PrintCopy.Id;
       
       
       LineItemToInsert.add(LineItemToAdd);
       
       
       }
       
    }
    
    insert LineItemToInsert;
    
    
    }

I have this trigger below that works to update a field with the active opportunity, but when I do a mass upload and I go to the account the field is blank. When I click edit and save the field is populated again with the active opportunity. The mass upload does not throw any errors and I belive the code is bulkified. Any ideas of why this is happening and how to fix it?. Thank you!
trigger act_opp_membership on Account (before insert, before update) {
    List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Name,AccountId FROM Opportunity WHERE AccountID in : trigger.new 
AND ((RecordType.Name = 'Membership Renewal' AND Active_Membership__c = 'Active') OR (RecordType.Name = 'Membership Prospect' AND Web_Expire_Date__c != null)
OR (RecordType.Name = 'Hill Site License' AND Membership_Web_Access__c=TRUE AND Membership_Start_Date__c<=today AND Membership_Expire_Date__c>=today)
OR (RecordType.Name = 'Complimentary Access' AND Membership_Web_Access__c=TRUE AND Membership_Start_Date__c<=today AND Membership_Expire_Date__c>=today))
ORDER BY Web_Expire_Date__c DESC NULLS LAST LIMIT 1]);
    Map<Id,Opportunity> accIdwithOpp = new Map<Id,Opportunity>();
    
    for(Opportunity opp : oppList){
        if(!accIdwithOpp.containsKey(opp.AccountId)){
            accIdwithOpp.put(opp.AccountId,opp);
        }
    }
    
    for(Account acc : trigger.new){
        if(accIdwithOpp.containsKey(acc.id)){
            acc.Active_Membership_Opportunity__c = accIdwithOpp.get(acc.id).id;
        }
        else{
            acc.Active_Membership_Opportunity__c = null;
        }
    }
}

 
Hi there, 

I made 3 seperate triggers to update 3 different fields on the account level. How do I combine them into one trigger for best practices. Code is below. Thanks for the help.
trigger act_opp_subscrip on Account (before insert, before update) {
    List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Name,AccountId FROM Opportunity WHERE AccountID in : trigger.new 
AND ((RecordType.Name = 'Subscription Renewal' AND Subscription_Status__c = 'Active') OR (RecordType.Name = 'Subscription Prospect' AND Web_Expire_Date__c != null))]);
    Map<Id,Opportunity> accIdwithOpp = new Map<Id,Opportunity>();
    
    for(Opportunity opp : oppList){
        if(!accIdwithOpp.containsKey(opp.AccountId)){
            accIdwithOpp.put(opp.AccountId,opp);
        }
    }
    
    for(Account acc : trigger.new){
        if(accIdwithOpp.containsKey(acc.id)){
            acc.Active_Subscription_Opportunity__c = accIdwithOpp.get(acc.id).id;
        }
        else{
            acc.Active_Subscription_Opportunity__c = null;
        }
    }
}
 
trigger act_opp on Account (before insert, before update) {
    List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Name,AccountId FROM Opportunity WHERE AccountID in : trigger.new 
AND ((RecordType.Name = 'Membership Renewal' AND Active_Membership__c = 'Active') OR (RecordType.Name = 'Membership Prospect' AND Web_Expire_Date__c != null))]);
    Map<Id,Opportunity> accIdwithOpp = new Map<Id,Opportunity>();
    
    for(Opportunity opp : oppList){
        if(!accIdwithOpp.containsKey(opp.AccountId)){
            accIdwithOpp.put(opp.AccountId,opp);
        }
    }
    
    for(Account acc : trigger.new){
        if(accIdwithOpp.containsKey(acc.id)){
            acc.Active_Membership_Opportunity__c = accIdwithOpp.get(acc.id).id;
        }
        else{
            acc.Active_Membership_Opportunity__c = null;
        }
    }
}
 
trigger act_opp_comms on Account (before insert, before update) {
    List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Name,AccountId FROM Opportunity WHERE AccountID in : trigger.new 
AND ((RecordType.Name = 'Communications Council Renewal' AND Comms_Status__c = 'Active') OR (RecordType.Name = 'Communications Council Prospect' AND Web_Expire_Date__c != null))]);
    Map<Id,Opportunity> accIdwithOpp = new Map<Id,Opportunity>();
    
    for(Opportunity opp : oppList){
        if(!accIdwithOpp.containsKey(opp.AccountId)){
            accIdwithOpp.put(opp.AccountId,opp);
        }
    }
    
    for(Account acc : trigger.new){
        if(accIdwithOpp.containsKey(acc.id)){
            acc.Active_Comms_Opportunity__c = accIdwithOpp.get(acc.id).id;
        }
        else{
            acc.Active_Comms_Opportunity__c = null;
        }
    }
}

 
Hi there, 

I desperatly need help tweaking this code. It works to pull in the active opportunity on the account level, but does not work when doing mass updates/uploads. I have the understading that the querry needs to be moved outside the loop but don't know how to do that exactly.
trigger act_opp on Account (before insert, before update) {

map<Id, Account> acctmap = new map<Id, Account>();
for(Account a : trigger.new){
    acctmap.putAll([SELECT Id, (SELECT Id, Name FROM Opportunities WHERE (RecordType.Name = 'Membership Renewal' AND Active_Membership__c = 'Active') OR (RecordType.Name = 'Membership Prospect' AND Web_Expire_Date__c != null) LIMIT 1) FROM Account WHERE Id =: a.Id]);}
    
for(Account a : trigger.new){
    if(acctmap.get(a.Id).Opportunities.size() != 0){
        Opportunity opp = acctmap.get(a.Id).Opportunities[0];
        a.Active_Membership_Opportunity__c = opp.Id;} 
    else {a.Active_Membership_Opportunity__c = null;}
}
}

Any help would be greatly appreciated. Thank you!
Hi there, 

I've been trying to create line items on a parent object after the parent obect is created or edited. I got this far with the code and it works great. The only problem is that when using data loader to insert records it throws out this erros: "Too many SOQL queries: 101 Status code: 16" . How would I bulkify this code to make it work with large uploads? 

Additional details. The code runs so that the roll up field "Total Number of Line Items" equals "Copy Count" as shown below. Thanks in advance! 
-Anthony
User-added image

trigger CreatePrintCopyLineItem on Print_Copy__c (after insert,after update) {

    List <Print_Copy_Line_Item__c> LineItemToInsert = new List <Print_Copy_Line_Item__c> ();
    
    for (Print_Copy__c PrintCopy : Trigger.new) {

        if(PrintCopy.Copy_Count__c > PrintCopy.Total_Number_of_Line_Items__c) {
            Decimal count = PrintCopy.Copy_Count__c - PrintCopy.Total_Number_of_Line_Items__c;

            for(integer i=0;i < count; i++)
            {
                Print_Copy_Line_Item__c LineItemToAdd = new Print_Copy_Line_Item__c ();
                LineItemToAdd.Print_Copy__c=PrintCopy.Id;
                LineItemToInsert.add(LineItemToAdd);
            }
         }      
    }
    
    if(LineItemToInsert.size() > 0)
        insert LineItemToInsert;

Hi there, 

I'm a little stuck on how to create a number of new child records on a parent object. I've been able to do it below a little half haphazardly by having the trigger fire multiple times until my two fields, “copy count” and a roll up field “Total Number of Line Items” are equal. Any insight on how to approach this to create the multiple line item records when Copy Count is less than the Total Number of Line Items so that copy count is equal to total number of line items. Thank you in advance.
Anthony
 
Current trigger below (When copy count increases by 15, "maximum trigger depth excedded" error is tripped):

trigger CreatePrintCopyLineItem on Print_Copy__c (before insert,after update) {

    List <Print_Copy_Line_Item__c> LineItemToInsert = new List <Print_Copy_Line_Item__c> ();
    
    for (Print_Copy__c PrintCopy : Trigger.new) {
    
       
       if (PrintCopy.Copy_Count__c>PrintCopy.Total_Number_of_Line_Items__c) {
       
       Print_Copy_Line_Item__c LineItemToAdd = new Print_Copy_Line_Item__c ();
       
       LineItemToAdd.Print_Copy__c=PrintCopy.Id;
       
       
       LineItemToInsert.add(LineItemToAdd);
       
       
       }
       
    }
    
    insert LineItemToInsert;
    
    
    }