+ Start a Discussion
P KumarP Kumar 

Adding RecordType Ids to Set in Trigger

Hi,

I have created trigger with hardcoding.
i want that to work dynamically.
Because, Recordtype Ids are different in Production and sandbox.

Kindly help me where i have to make changes exactly.

Following is my trigger code.
=========================================================================

trigger APACLeadExactTargetEmails on xtma_Individual_Email_Result__c (after insert, after update)
{
    
     set<Id> LeadRts = new Set<Id>{'012200000005uXiAAI','0127E000000CgrLQAS','0127E000000CgqNQAS'};

//    Id rtID= [select Id,name from RecordType where name ='APAC - Open'OR name ='Global Abandoned Checkout' OR name = 'Global New Web Registrant' limit 1].Id;

    Set<String> setIDs = new Set<String>();

    for(xtma_Individual_Email_Result__c Emails : trigger.new){
        if(Emails.Lead__c!=null){
            setIDs.add(Emails.Lead__c);
        }
    }

    List<lead> listLead = [ Select Id FROM Lead where RecordTypeId in :LeadRts AND id in :setIDs ] ;
    Map<String,Lead> mapKeyWiseLead = new Map<String,Lead>();
        for(Lead obj :  listLead )
        {
            String key = obj.id;
            mapKeyWiseLead.put(key,obj);
        }
        
    List<Lead> listUpdatelead = new List<Lead>();
    Set<String> setLeadID = new Set<String>();
    
    String key;
    for(xtma_Individual_Email_Result__c Emails : trigger.new)
    {
        key='';
        if(Emails.Lead__c!=null){
            key=Emails.Lead__c;
            
            if( mapKeyWiseLead.containsKey(key) )
            {
                Lead checkLead = mapKeyWiseLead.get(Key) ;
                
               if(emails.name.contains('1st Newsletter - EDE')) 
                {
                    checkLead.X1st_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X1st_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X1st_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X1st_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='1st Newsletter - EDE'){checkLead.X1st_Email_Name__c = '1st Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                if(emails.name.contains('2nd Newsletter - EDE'))
                {
                    checkLead.X2nd_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X2nd_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X2nd_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X2nd_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='2nd Newsletter - EDE'){checkLead.X2nd_Email_Name__c = '2nd Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                if(emails.name.contains('3rd Newsletter - EDE')) 
                {
                    checkLead.X3rd_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X3rd_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X3rd_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X3rd_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='3rd Newsletter - EDE'){checkLead.X3rd_Email_Name__c = '3rd Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                
            }
        }
    }
        if( listUpdatelead.size() > 0 )
        {
                try
                    {
                        update listUpdatelead;            
                    }
                catch(DMLException e)
                {  
                    System.debug('The following exception has occurred: ' + e.getMessage());
                }
        }
}

Regards,
Pranav
Best Answer chosen by P Kumar
veda shriveda shri
Hi Kumar,

Please check below code:
trigger APACLeadExactTargetEmails on xtma_Individual_Email_Result__c (after insert, after update)
{
	Set<String> setIDs = new Set<String>();

    for(xtma_Individual_Email_Result__c Emails : trigger.new){
        if(Emails.Lead__c!=null){
            setIDs.add(Emails.Lead__c);
        }
    }
	
	List<String> lstRecordTypeName = new List<String>{'APAC_Open','Global_Abandoned_Checkout','Global_New_Web_Registrant'};
    
     //set<Id> LeadRts = new Set<Id>{'012200000005uXiAAI','0127E000000CgrLQAS','0127E000000CgqNQAS'};
	 
	set<Id> LeadRts = new set<Id>();
	
	For(RecordType objRecType :[SELECT Id FROM RecordType WHERE SObjectType='Lead' where developerName IN: lstRecordTypeName])
	{
		LeadRts.add(objRecType.Id);
	}

    Set<String> setIDs = new Set<String>();

    for(xtma_Individual_Email_Result__c Emails : trigger.new){
        if(Emails.Lead__c!=null){
            setIDs.add(Emails.Lead__c);
        }
    }

    List<lead> listLead = [ Select Id FROM Lead where RecordTypeId in :LeadRts AND id in :setIDs ] ;
    Map<String,Lead> mapKeyWiseLead = new Map<String,Lead>();
        for(Lead obj :  listLead )
        {
            String key = obj.id;
            mapKeyWiseLead.put(key,obj);
        }
        
    List<Lead> listUpdatelead = new List<Lead>();
    Set<String> setLeadID = new Set<String>();
    
    String key;
    for(xtma_Individual_Email_Result__c Emails : trigger.new)
    {
        key='';
        if(Emails.Lead__c!=null){
            key=Emails.Lead__c;
            
            if( mapKeyWiseLead.containsKey(key) )
            {
                Lead checkLead = mapKeyWiseLead.get(Key) ;
                
               if(emails.name.contains('1st Newsletter - EDE')) 
                {
                    checkLead.X1st_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X1st_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X1st_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X1st_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='1st Newsletter - EDE'){checkLead.X1st_Email_Name__c = '1st Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                if(emails.name.contains('2nd Newsletter - EDE'))
                {
                    checkLead.X2nd_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X2nd_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X2nd_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X2nd_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='2nd Newsletter - EDE'){checkLead.X2nd_Email_Name__c = '2nd Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                if(emails.name.contains('3rd Newsletter - EDE')) 
                {
                    checkLead.X3rd_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X3rd_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X3rd_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X3rd_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='3rd Newsletter - EDE'){checkLead.X3rd_Email_Name__c = '3rd Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                
            }
        }
    }
        if( listUpdatelead.size() > 0 )
        {
                try
                    {
                        update listUpdatelead;            
                    }
                catch(DMLException e)
                {  
                    System.debug('The following exception has occurred: ' + e.getMessage());
                }
        }
}

 

All Answers

Ishwar ShindeIshwar Shinde
Hi Pranav, 

Yes, Record Type ids are diffirent in sandbox and production, But its developername(i.e. API Name) is unique. You can use this field to get Id of record type by making SOQL on RecordType object.

Select Id,Name, DeveloperName from RecordType where developerName = 'Sample_RT'

You can use this in code to make it dyanamic. 

Mark this as best ans if this helps!!!
veda shriveda shri
Hi,

You can use below approch
For(RecordType objRecType :[SELECT Id FROM RecordType WHERE SObjectType='Lead'])
{
	LeadRts.add(objRecType.Id);
}

if you want record type with perticular name you can add below condition in where clause
Name='RECORDTYPE_NAME_HERE'

Let me know if you need any help.

Thanks,
vedashri
Ishwar ShindeIshwar Shinde
Updated code - (pls replace name with developer name on query)
List<RecordType>  rtypeList= [select Id,name from RecordType where name ='APAC - Open'OR name ='Global Abandoned Checkout' OR name = 'Global New Web Registrant'];

//Replace name with developerName and mention API name of record type (which not conatins space)

set<Id> LeadRts = new Set<ID>();
for(RecordType rt : rtypeList){
     LeadRts.add(rt.Id);
}

 Set<String> setIDs = new Set<String>();

    for(xtma_Individual_Email_Result__c Emails : trigger.new){
        if(Emails.Lead__c!=null){
            setIDs.add(Emails.Lead__c);
        }
    }

    List<lead> listLead = [ Select Id FROM Lead where RecordTypeId in :LeadRts AND id in :setIDs ] ;
    Map<String,Lead> mapKeyWiseLead = new Map<String,Lead>();
        for(Lead obj :  listLead )
        {
            String key = obj.id;
            mapKeyWiseLead.put(key,obj);
        }
        
    List<Lead> listUpdatelead = new List<Lead>();
    Set<String> setLeadID = new Set<String>();
    
    String key;
    for(xtma_Individual_Email_Result__c Emails : trigger.new)
    {
        key='';
        if(Emails.Lead__c!=null){
            key=Emails.Lead__c;
            
            if( mapKeyWiseLead.containsKey(key) )
            {
                Lead checkLead = mapKeyWiseLead.get(Key) ;
                
               if(emails.name.contains('1st Newsletter - EDE')) 
                {
                    checkLead.X1st_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X1st_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X1st_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X1st_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='1st Newsletter - EDE'){checkLead.X1st_Email_Name__c = '1st Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                if(emails.name.contains('2nd Newsletter - EDE'))
                {
                    checkLead.X2nd_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X2nd_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X2nd_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X2nd_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='2nd Newsletter - EDE'){checkLead.X2nd_Email_Name__c = '2nd Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                if(emails.name.contains('3rd Newsletter - EDE')) 
                {
                    checkLead.X3rd_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X3rd_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X3rd_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X3rd_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='3rd Newsletter - EDE'){checkLead.X3rd_Email_Name__c = '3rd Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                
            }
        }
    }
        if( listUpdatelead.size() > 0 )
        {
                try
                    {
                        update listUpdatelead;            
                    }
                catch(DMLException e)
                {  
                    System.debug('The following exception has occurred: ' + e.getMessage());
                }
        }
}

 
mrkimrki
The following code retrieves all lead record type IDs to LeadRts set:
Map<Id, RecordType> LeadRTMap = new Map<Id, RecordType>([SELECT Id, Name FROM RecordType WHERE SobjectType = 'Lead']);
Set<Id> LeadRts = LeadRTMap.keySet();
Ishwar ShindeIshwar Shinde
Hi Pranav,

Please close the issue if it is resolved.

 
P KumarP Kumar

Am not able to fix the issue with your guidelines.

Am totally new to the development.
 

veda shriveda shri
Hi Kumar,

Can you pass me the record type name here?
or please share your number I'll call you.

Thanks,
Vedashri
P KumarP Kumar

Hi Veda Shri,

Heres my Email ID : mail.pranava@gmail.com

Ping me here.

Regards,
Pranav

veda shriveda shri
Hi Kumar,

Please check below code:
trigger APACLeadExactTargetEmails on xtma_Individual_Email_Result__c (after insert, after update)
{
	Set<String> setIDs = new Set<String>();

    for(xtma_Individual_Email_Result__c Emails : trigger.new){
        if(Emails.Lead__c!=null){
            setIDs.add(Emails.Lead__c);
        }
    }
	
	List<String> lstRecordTypeName = new List<String>{'APAC_Open','Global_Abandoned_Checkout','Global_New_Web_Registrant'};
    
     //set<Id> LeadRts = new Set<Id>{'012200000005uXiAAI','0127E000000CgrLQAS','0127E000000CgqNQAS'};
	 
	set<Id> LeadRts = new set<Id>();
	
	For(RecordType objRecType :[SELECT Id FROM RecordType WHERE SObjectType='Lead' where developerName IN: lstRecordTypeName])
	{
		LeadRts.add(objRecType.Id);
	}

    Set<String> setIDs = new Set<String>();

    for(xtma_Individual_Email_Result__c Emails : trigger.new){
        if(Emails.Lead__c!=null){
            setIDs.add(Emails.Lead__c);
        }
    }

    List<lead> listLead = [ Select Id FROM Lead where RecordTypeId in :LeadRts AND id in :setIDs ] ;
    Map<String,Lead> mapKeyWiseLead = new Map<String,Lead>();
        for(Lead obj :  listLead )
        {
            String key = obj.id;
            mapKeyWiseLead.put(key,obj);
        }
        
    List<Lead> listUpdatelead = new List<Lead>();
    Set<String> setLeadID = new Set<String>();
    
    String key;
    for(xtma_Individual_Email_Result__c Emails : trigger.new)
    {
        key='';
        if(Emails.Lead__c!=null){
            key=Emails.Lead__c;
            
            if( mapKeyWiseLead.containsKey(key) )
            {
                Lead checkLead = mapKeyWiseLead.get(Key) ;
                
               if(emails.name.contains('1st Newsletter - EDE')) 
                {
                    checkLead.X1st_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X1st_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X1st_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X1st_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='1st Newsletter - EDE'){checkLead.X1st_Email_Name__c = '1st Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                if(emails.name.contains('2nd Newsletter - EDE'))
                {
                    checkLead.X2nd_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X2nd_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X2nd_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X2nd_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='2nd Newsletter - EDE'){checkLead.X2nd_Email_Name__c = '2nd Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                if(emails.name.contains('3rd Newsletter - EDE')) 
                {
                    checkLead.X3rd_Email_Date__c= Emails.Date_Time_Sent__c.Date();

                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==FALSE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X3rd_Email_Outcome__c = 'Sent';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c==null){
                        checkLead.X3rd_Email_Outcome__c = 'Opened';
                    }
                    if(Emails.Date_Time_Sent__c!=null && Emails.Opened__c==TRUE && Emails.Number_of_Total_Clicks__c>0){
                        checkLead.X3rd_Email_Outcome__c = 'Clicked';
                        }
                       if(emails.name=='3rd Newsletter - EDE'){checkLead.X3rd_Email_Name__c = '3rd Newsletter - EDE';
                    }
                        listUpdatelead.add(checkLead);
                        setLeadID.add(checkLead.id);
                }
                
            }
        }
    }
        if( listUpdatelead.size() > 0 )
        {
                try
                    {
                        update listUpdatelead;            
                    }
                catch(DMLException e)
                {  
                    System.debug('The following exception has occurred: ' + e.getMessage());
                }
        }
}

 
This was selected as the best answer