Thursday, 14 November 2013

Better Naming Convention for Maintaining multi-level(GrandParent/Parent/Child RecordType Relationships) Campaigns


Better Naming Convention for Maintaining multi-level Campaigns based on the term and year, implemented by using a Map & SchemaObject Call for particular Record and RecordType to avoid unneceassy code executions/calls and exposing the usage of simple Map.

Instead of Hardcoding the RecordTypeId's, we can use the below Map to retrieve the necessary RecordType Name by giving the RecordTypeId, The point is to avoid hardcoding id's, it should makes the code workable even after moved to some other salesforce instance .


  1. Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Campaign.getRecordTypeInfosById();
  2. String RecTypeName = rtMap.get(<<RecordTypeId>>).getName()

Example:


1.GrandParent - Year

2. Parent – Term (fall, spring, summer)

3. Child – Campaign Details for the particular Parent

Campaign with Multiple RecordTypes.

     1. Grandparent/Parent under one Record Type.
     2. Child can be any Recordtype.

Parent Campaign Name is Concatenated  with the GrandParent Campaign Name  whenever a Parent Campaign is created, The same for the Child Campaign with the Child Campaign Record Type Name.


Apex Code:

  1. Trigger CampaignNamePop on Campaign (before insert) {
  2. Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Campaign.getRecordTypeInfosById();
  3. Set<id> ParentIdSet = new Set<id>();  
  4. Boolean bool=false;    
  5.        
  6.        
  7. For(Campaign camp:Trigger.New){
  8. if(rtMap.get(camp.RecordTypeId).getName()=='Grandparent/Parent Mktg' && camp.ParentId!=null){
  9. ParentIdSet.add(camp.ParentId);
  10. bool=false;
  11. }
  12. if(rtMap.get(camp.RecordTypeId).getName()!='Grandparent/Parent Mktg' && camp.ParentId!=null){
  13. ParentIdSet.add(camp.ParentId);
  14. bool=true;
  15. }
  16. }
  17. Map<ID,Campaign> cObjectMap = new Map<ID,Campaign>([select Id, Name, RecordTypeId, RecordType.Name from Campaign where Id IN: ParentIdSet AND RecordType.Name='Grandparent/Parent Mktg'  ]);
  18. if(cObjectMap.size()>0 ){
  19. For(Campaign camp:Trigger.New){
  20. if(bool==false){
  21. camp.Name=camp.Name+' '+cObjectMap.get(camp.ParentId).Name;
  22. }
  23. if(bool==true){
  24. camp.Name=camp.Name+','+rtMap.get(camp.RecordTypeId).getName()+' - '+cObjectMap.get(camp.ParentId).Name;
  25. }
  26. }
  27. }
  28. }

No comments:

Post a Comment