• Bruce Hanson
  • NEWBIE
  • 15 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 3
    Replies
As a newbie/begginner developer, I've successfully written some simple apex triggers, but I'm not sure where to start with this one.  In short, I want the ability to set a checkbox on the opportunity record, which would trigger the deletion of all opportunity product line items.  In short, I've never had the need to do a mass delete so I'm not sure where to get started.  Any assistance is greatly appreciated.
I could use some help solving an Apex governor limit warning issue. I have two problems.  First, I don't know for sure which trigger or class is causing the error (but I think I know) ... and ... I don't know how to fix the trigger (assuming I'm correct about the culprit).  
Assuming I'm right, the trigger below is the culprit.  In short, its simple purpose is to set the account record type based on a custom formula field that derives its values from a custom picklist field.  The trigger works just as I expect it to.  However, when I try to use API tools like dataloader, I get the governor limit warnings.  I have it set up to send me an email until I get it solved.  If possible, please give guidance on how I can determine for sure what is the cause, and if you agree the below trigger is the cause, any assistance in 'bulkifying' this trigger is much appreciated.  

trigger SET_ACCOUNT_RTYPE_TRIGGER2 on Account (before insert, before update) {
Map<String, Id> typeMap = New Map<String, Id>();

   for(RecordType RT: [Select DeveloperName, Id From RecordType Where sObjectType = 'Account']) {
      typeMap.put(RT.DeveloperName, RT.Id);
   }
   for (Account ACT : trigger.new)  {
        
             id recid = typeMap.get(ACT.Rtype_FM__c);
             recordtype rectype = [select id, developername from recordtype where id=:recid];
            ACT.RecordTypeid = rectype.id; 
                        
            }
   }

I have a working trigger (see below) that sets the Contact Record Type based on the Record type of the Account record.  By design, the contact record type names match the account record type names.  The purpose of this design is to use record types/page layouts to contol/filter the available values in a custom pick list field.  Although this trigger accurately sets the Contact Record type to the correct value, it does not set it until after the initial contact record is saved.  Unfortunatley, I need it to be set and be in effect when the initial 'create new contact record' screen appears.  If working properly, the inital screen would take into affect the controls/filters as defined for that specific record type.  I had assumed that the trigger setting of 'before insert, before update' would meet my requirement but it does not. Can anyone help with this or am I asking for something that is not possible.  I'm aware that I can use User Profiles to control the default record type, but that is not really a good solution for me.  Any advice and insight is greatly appreciated!
Thanks,
Bruce

trigger SET_CON_RTYPE on Contact(before insert, before update)
{
    List<RecordType> C_rtypes = [Select Name, Id From RecordType where sObjectType='Contact' and isActive=true];
    Map<String,RecordType> mapContactRecordTypes = new Map<String,RecordType>{};
    for(RecordType C_RTY: C_rtypes)
    {
       mapContactRecordTypes.put(C_RTY.Name,C_RTY);
    }
    Set<String> setAccID = new Set<String>();
    for (Contact THIS_CON : trigger.new)  
    {
        setAccID.add(THIS_CON.accountid);
    }      
    Map<Id,Account> MapAccount = new Map<id,Account>( [Select id,RecordType.Name,recordTypeId from account where id in :setAccID ] );
    for (Contact THIS_CON : trigger.new)  
    {
        if(MapAccount.containsKey(THIS_CON.accountId))
        {
            Account acc = MapAccount.get(THIS_CON.accountId);
            if(mapContactRecordTypes.containsKey(acc.RecordType.Name))
            {
                THIS_CON.recordTypeId = mapContactRecordTypes.get(acc.RecordType.Name).id;
            }   }  } }
 

am a begginner developer and I could really benefit from some assistance.  I’m trying to code a trigger that will set the Contact record type based on the Account record type.  I’m doing this in order to control the available picklist options on a custom pick list field on the contact record.  In short, the record types on both the account and contact objects are matching names.  I can’t use workflow because I need the contact record type established (to control the custom picklist field selection options) on the INITIAL  ‘create new’ page layout.  Although my trigger runs without error, its is not working as the ‘new’ contact record type always stays as the ‘default’ setting.  I’m not good at debugging so I’m not sure where to turn.  My code is below.  Any insight is greatly appreciated!

trigger SET_CON_RTYPE on Contact(before insert, before update){

//Query for the Account record types
    List<RecordType> A_rtypes = [Select Name, Id From RecordType
                 where sObjectType='Account' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
    Map<String,String> A_RecordTypes = new Map<String,String>{};
    for(RecordType A_RTY: A_rtypes)
       A_RecordTypes.put(A_RTY.Name,A_RTY.Id);

//Query for the Contact record types
    List<RecordType> C_rtypes = [Select Name, Id From RecordType
                 where sObjectType='Contact' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
    Map<String,String> C_RecordTypes = new Map<String,String>{};
    for(RecordType C_RTY: C_rtypes)
       C_RecordTypes.put(C_RTY.Name,C_RTY.Id);

  for (Contact THIS_CON : trigger.new)  {
  
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('Agency')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('Agency');
      
           } else
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('BodyShop')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('BodyShop');
      
           } else
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('Rental')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('Rental');
                                  
           } else {
           THIS_CON.RecordTypeid = C_RecordTypes.get('Other');
           }
      }
    }
I'm looking for some simple apex code that will set the value of a picklist field based on another text (or formula) field value (whenever it changes).
Using workflow would take too many rules because I need a new rule for each possible value.
Thanks!
am a begginner developer and I could really benefit from some assistance.  I’m trying to code a trigger that will set the Contact record type based on the Account record type.  I’m doing this in order to control the available picklist options on a custom pick list field on the contact record.  In short, the record types on both the account and contact objects are matching names.  I can’t use workflow because I need the contact record type established (to control the custom picklist field selection options) on the INITIAL  ‘create new’ page layout.  Although my trigger runs without error, its is not working as the ‘new’ contact record type always stays as the ‘default’ setting.  I’m not good at debugging so I’m not sure where to turn.  My code is below.  Any insight is greatly appreciated!

trigger SET_CON_RTYPE on Contact(before insert, before update){

//Query for the Account record types
    List<RecordType> A_rtypes = [Select Name, Id From RecordType
                 where sObjectType='Account' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
    Map<String,String> A_RecordTypes = new Map<String,String>{};
    for(RecordType A_RTY: A_rtypes)
       A_RecordTypes.put(A_RTY.Name,A_RTY.Id);

//Query for the Contact record types
    List<RecordType> C_rtypes = [Select Name, Id From RecordType
                 where sObjectType='Contact' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
    Map<String,String> C_RecordTypes = new Map<String,String>{};
    for(RecordType C_RTY: C_rtypes)
       C_RecordTypes.put(C_RTY.Name,C_RTY.Id);

  for (Contact THIS_CON : trigger.new)  {
  
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('Agency')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('Agency');
      
           } else
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('BodyShop')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('BodyShop');
      
           } else
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('Rental')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('Rental');
                                  
           } else {
           THIS_CON.RecordTypeid = C_RecordTypes.get('Other');
           }
      }
    }
As a newbie/begginner developer, I've successfully written some simple apex triggers, but I'm not sure where to start with this one.  In short, I want the ability to set a checkbox on the opportunity record, which would trigger the deletion of all opportunity product line items.  In short, I've never had the need to do a mass delete so I'm not sure where to get started.  Any assistance is greatly appreciated.

I have a working trigger (see below) that sets the Contact Record Type based on the Record type of the Account record.  By design, the contact record type names match the account record type names.  The purpose of this design is to use record types/page layouts to contol/filter the available values in a custom pick list field.  Although this trigger accurately sets the Contact Record type to the correct value, it does not set it until after the initial contact record is saved.  Unfortunatley, I need it to be set and be in effect when the initial 'create new contact record' screen appears.  If working properly, the inital screen would take into affect the controls/filters as defined for that specific record type.  I had assumed that the trigger setting of 'before insert, before update' would meet my requirement but it does not. Can anyone help with this or am I asking for something that is not possible.  I'm aware that I can use User Profiles to control the default record type, but that is not really a good solution for me.  Any advice and insight is greatly appreciated!
Thanks,
Bruce

trigger SET_CON_RTYPE on Contact(before insert, before update)
{
    List<RecordType> C_rtypes = [Select Name, Id From RecordType where sObjectType='Contact' and isActive=true];
    Map<String,RecordType> mapContactRecordTypes = new Map<String,RecordType>{};
    for(RecordType C_RTY: C_rtypes)
    {
       mapContactRecordTypes.put(C_RTY.Name,C_RTY);
    }
    Set<String> setAccID = new Set<String>();
    for (Contact THIS_CON : trigger.new)  
    {
        setAccID.add(THIS_CON.accountid);
    }      
    Map<Id,Account> MapAccount = new Map<id,Account>( [Select id,RecordType.Name,recordTypeId from account where id in :setAccID ] );
    for (Contact THIS_CON : trigger.new)  
    {
        if(MapAccount.containsKey(THIS_CON.accountId))
        {
            Account acc = MapAccount.get(THIS_CON.accountId);
            if(mapContactRecordTypes.containsKey(acc.RecordType.Name))
            {
                THIS_CON.recordTypeId = mapContactRecordTypes.get(acc.RecordType.Name).id;
            }   }  } }
 

am a begginner developer and I could really benefit from some assistance.  I’m trying to code a trigger that will set the Contact record type based on the Account record type.  I’m doing this in order to control the available picklist options on a custom pick list field on the contact record.  In short, the record types on both the account and contact objects are matching names.  I can’t use workflow because I need the contact record type established (to control the custom picklist field selection options) on the INITIAL  ‘create new’ page layout.  Although my trigger runs without error, its is not working as the ‘new’ contact record type always stays as the ‘default’ setting.  I’m not good at debugging so I’m not sure where to turn.  My code is below.  Any insight is greatly appreciated!

trigger SET_CON_RTYPE on Contact(before insert, before update){

//Query for the Account record types
    List<RecordType> A_rtypes = [Select Name, Id From RecordType
                 where sObjectType='Account' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
    Map<String,String> A_RecordTypes = new Map<String,String>{};
    for(RecordType A_RTY: A_rtypes)
       A_RecordTypes.put(A_RTY.Name,A_RTY.Id);

//Query for the Contact record types
    List<RecordType> C_rtypes = [Select Name, Id From RecordType
                 where sObjectType='Contact' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
    Map<String,String> C_RecordTypes = new Map<String,String>{};
    for(RecordType C_RTY: C_rtypes)
       C_RecordTypes.put(C_RTY.Name,C_RTY.Id);

  for (Contact THIS_CON : trigger.new)  {
  
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('Agency')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('Agency');
      
           } else
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('BodyShop')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('BodyShop');
      
           } else
           if(THIS_CON.Account_Rtype_F__c==A_RecordTypes.get('Rental')){  
           THIS_CON.RecordTypeid = C_RecordTypes.get('Rental');
                                  
           } else {
           THIS_CON.RecordTypeid = C_RecordTypes.get('Other');
           }
      }
    }