• franky@gmail.com
  • NEWBIE
  • 0 Points
  • Member since 2004

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
I'm new to Flows, and am looking at 5 flows that had been bulit by a developer of ours not too long ago.  The flows are currently being triggered by a Process Builder process.  The business process goes like this:

When an invoice is created or updated, roll up the amount value to one of the 5 fields on the Account record.  Which one of the 5 fields depends on whether the invoice is paid, or past due.  If past due, 30, 60, 90 or >90 days past due.  My question is:  Is there a way to group all of this into just one Flow, instead of having 5 separate flows, that essentially does the same thing, except to different fields?

I had the pleasure of writing my first trigger today.  I'm not new to programming, just new to the APEX language.  In creating this trigger, I tried to adhere as much as I can to some of the best practices outlined in many of the resources available.  I do however have some concerns about my code, specifically with regards to what I'm reading about governor limits.

 

Here's a brief background into what the business process is and why the trigger is necessary: We have a need to keep track of the campaigns a lead or contact takes part of.  We have a specific text field in the Lead and Contact table where a list of campaign names of the campaigns they are a part of will be stored.  This field will be updated each time a new campaign member record is created or modified for this lead or contact. 

 

The trigger is on the Campaign Member table, and basically, each time the trigger fires, the field (on the Lead or Contact record, depending on which one it is) will be re-calculated based on all of the campaigns that person is a part of.  The guts of the trigger will get a list of all the Campaign Member records for the Lead or Contact (of this current Campaign Member record), from a SOQL statement.  This SOQL statement is embedded in the "for" loop, which may break if the governor limit for SOQL queries are reached, in this case, 100 SOQL queries.  So, if we have a bulk upload of Campaign member records using say a data loading tool of some sort, it seems to me that using this trigger, we'd hit the governor limit real quick.  How can this be improved so that we do not reach the governor limits?

 

Any help is appreciated!

 

Thanks,

Frank

 

Trigger code below:

==================================================

 

trigger UpdateProspectWithCampaignName on CampaignMember (after insert, after update, after delete) {
    if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {      
        // Iterate over each CampaignMember record
        for (CampaignMember newCM : Trigger.new) {  
            string mycampaigns = '';         
            if (!string.isEmpty(newCM.ContactId)) { //If the Prospect is a Contact record
                Contact ContactToMod = [SELECT Id, Name, Test_CL__c, (SELECT Campaign.Name FROM CampaignMembers)
                FROM Contact
                WHERE Id = :newCM.ContactId];
                // Access child records.
                List<CampaignMember> cms = ContactToMod.CampaignMembers;
                for(CampaignMember thisone : cms){
                    mycampaigns += thisone.Campaign.Name + ';';
                }
                mycampaigns = mycampaigns.removeEnd(';');
                ContactToMod.Test_CL__c = mycampaigns;
                update ContactToMod;                         
            } else  { //If the Prospect is a Lead record
                Lead LeadToMod = [SELECT Id, Name, Test_CL__c, (SELECT Campaign.Name FROM CampaignMembers)
                FROM Lead
                WHERE Id = :newCM.LeadId];
                // Access child records.
                List<CampaignMember> cms = LeadToMod.CampaignMembers;
                for(CampaignMember thisone : cms){
                    mycampaigns += thisone.Campaign.Name + ';';
                }
                mycampaigns = mycampaigns.removeEnd(';');
                LeadToMod.Test_CL__c = mycampaigns;
                update LeadToMod;           
            }    
        }
    }
}

 

 

I had the pleasure of writing my first trigger today.  I'm not new to programming, just new to the APEX language.  In creating this trigger, I tried to adhere as much as I can to some of the best practices outlined in many of the resources available.  I do however have some concerns about my code, specifically with regards to what I'm reading about governor limits.

 

Here's a brief background into what the business process is and why the trigger is necessary: We have a need to keep track of the campaigns a lead or contact takes part of.  We have a specific text field in the Lead and Contact table where a list of campaign names of the campaigns they are a part of will be stored.  This field will be updated each time a new campaign member record is created or modified for this lead or contact. 

 

The trigger is on the Campaign Member table, and basically, each time the trigger fires, the field (on the Lead or Contact record, depending on which one it is) will be re-calculated based on all of the campaigns that person is a part of.  The guts of the trigger will get a list of all the Campaign Member records for the Lead or Contact (of this current Campaign Member record), from a SOQL statement.  This SOQL statement is embedded in the "for" loop, which may break if the governor limit for SOQL queries are reached, in this case, 100 SOQL queries.  So, if we have a bulk upload of Campaign member records using say a data loading tool of some sort, it seems to me that using this trigger, we'd hit the governor limit real quick.  How can this be improved so that we do not reach the governor limits?

 

Any help is appreciated!

 

Thanks,

Frank

 

Trigger code below:

==================================================

 

trigger UpdateProspectWithCampaignName on CampaignMember (after insert, after update, after delete) {
    if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {      
        // Iterate over each CampaignMember record
        for (CampaignMember newCM : Trigger.new) {  
            string mycampaigns = '';         
            if (!string.isEmpty(newCM.ContactId)) { //If the Prospect is a Contact record
                Contact ContactToMod = [SELECT Id, Name, Test_CL__c, (SELECT Campaign.Name FROM CampaignMembers)
                FROM Contact
                WHERE Id = :newCM.ContactId];
                // Access child records.
                List<CampaignMember> cms = ContactToMod.CampaignMembers;
                for(CampaignMember thisone : cms){
                    mycampaigns += thisone.Campaign.Name + ';';
                }
                mycampaigns = mycampaigns.removeEnd(';');
                ContactToMod.Test_CL__c = mycampaigns;
                update ContactToMod;                         
            } else  { //If the Prospect is a Lead record
                Lead LeadToMod = [SELECT Id, Name, Test_CL__c, (SELECT Campaign.Name FROM CampaignMembers)
                FROM Lead
                WHERE Id = :newCM.LeadId];
                // Access child records.
                List<CampaignMember> cms = LeadToMod.CampaignMembers;
                for(CampaignMember thisone : cms){
                    mycampaigns += thisone.Campaign.Name + ';';
                }
                mycampaigns = mycampaigns.removeEnd(';');
                LeadToMod.Test_CL__c = mycampaigns;
                update LeadToMod;           
            }    
        }
    }
}