function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Luciano RobertoLuciano Roberto 

Filter the trigger on child object based on the record type type on parent object

Hi all,
 

404/5000
I have two objects:
Object1__c (parent)
Object2__c (child)
Relationship master details.
For the parent object I have two types of records A, B

For the child object 2 types of records 1, 2 then if the parent object is of type A, the layout for the child will only appear 1 and if the parent object is B, the child layout will be only 2.

Someone has some trigger code to be able to do this verification.

 

Thanks!

Best Answer chosen by Luciano Roberto
Luciano RobertoLuciano Roberto

I actually realized that the trigger was not answering me, so I did the following:

I created a custom button that calls the VFP.
I created a VFP that stores the data of the current record (parent record);

<apex:page standardController="Contestacao_RVI__c" extensions="NewDadosContestacaoController">
    
    
    
    
    <apex:form >

         <apex:outputPanel id="Panel" rendered="{!isPanel = true}">
         
         <apex:outputField value="{!currentRecord.Id}" /><br/>
         <apex:outputField value="{!currentRecord.Name}" /><br/>
         <apex:outputField value="{!currentRecord.Nome_Tipo_de_Registro__c}" /> <br/>
         <apex:outputField value="{!currentRecord.Motivo__c}" /><br/>
         
         </apex:outputPanel>
        
      Criar um novo dado de contestação de  <apex:outputField value="{!currentRecord.Plataforma__c}" /> ?<br/><br/>
          <apex:commandButton action="{!NovoDadosContestacao}" value="Confirma"/>

         
    </apex:form>
    
    
    
</apex:page>
 

I created a class that creates the child record based on the parent's criteria, then redirects to the created record (child) in edit mode

public with sharing class NewDadosContestacaoController {

    public String myStr {get; set;}
    
    public Contestacao_RVI__c currentRecord{get; set;}
    
	RecordType tipoDadosContestacao;    
    Id devRecordTypeId;
    
    public Boolean isPanel {get; set;}
    
    
    public NewDadosContestacaoController(ApexPages.StandardController con)
    {
         currentRecord = [SELECT Id, Name, Motivo__c, Plataforma__c, Nome_Tipo_de_Registro__c FROM Contestacao_RVI__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];  
    }
    
    
    
   
    
      
  public PageReference NovoDadosContestacao(){
  
     
       if(currentRecord.Plataforma__c == 'Conectividade')
        {
            tipoDadosContestacao = [SELECT Id, Name FROM RecordType WHERE DeveloperName = 'Conectividade' AND sobjecttype = 'Dados_da_contestacao__c'];
            
            //Pega o ID do tipo de registro pelo nome
            devRecordTypeId = Schema.SObjectType.Dados_da_contestacao__c.getRecordTypeInfosByName().get('Conectividade').getRecordTypeId();
        }
      
     
      else if(currentRecord.Plataforma__c == 'Mobilidade')
        {
            tipoDadosContestacao = [SELECT Id, Name FROM RecordType WHERE DeveloperName = 'Mobilidade' AND sobjecttype = 'Dados_da_contestacao__c'];
            
            //Pega o ID do tipo de registro pelo nome
            devRecordTypeId = Schema.SObjectType.Dados_da_contestacao__c.getRecordTypeInfosByName().get('Mobilidade').getRecordTypeId();
        }
      
       else if(currentRecord.Plataforma__c == 'Soluções digitais')
        {
           tipoDadosContestacao = [SELECT Id, Name FROM RecordType WHERE DeveloperName = 'SolucoesDigitais' AND sobjecttype = 'Dados_da_contestacao__c'];
            
            //Pega o ID do tipo de registro pelo nome
            devRecordTypeId = Schema.SObjectType.Dados_da_contestacao__c.getRecordTypeInfosByName().get('SolucoesDigitais').getRecordTypeId();        }
      
      
     else 
        {
            tipoDadosContestacao = [SELECT Id, Name FROM RecordType WHERE DeveloperName = 'Outros' AND sobjecttype = 'Dados_da_contestacao__c'];
            
            //Pega o ID do tipo de registro pelo nome
            devRecordTypeId = Schema.SObjectType.Dados_da_contestacao__c.getRecordTypeInfosByName().get('Outros').getRecordTypeId();
        }     
      
      
      
    
     			Dados_da_contestacao__c dc = new Dados_da_contestacao__c ( 
                                                                            Contestacao__c = currentRecord.Id,
                                                                            Plataforma_da_contestacao__c = currentRecord.Plataforma__c,
                                                                            Motivo_da_contestacao__c = currentRecord.Motivo__c,
                                                                            Status__c = 'Aberto',
                                                                            RecordTypeId = String.valueOf(devRecordTypeId) 
                														 );
                                                                          //  RecordTypeId = '0123D000000WCHg' );
      
      // Redireciona para o registro criado em modo de edição
      insert dc;
      return (new ApexPages.StandardController (new Dados_da_contestacao__c(Id=dc.Id))).edit();
    
     
          
  }
    
    
    
    
    
}

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Luciano,

Please find the sample code below and tweak it as per your requirement.
trigger MyTrigger on custom_object__2 (after update) {

    // Map is custom_object__2 to developer name of custom_object__1 record type
    Map<Id, String> m = new Map<Id, String>();
    for (custom_object__1 c1 : [
            select custom_object__2, RecordType.DeveloperName
            from custom_object__1
            where custom_object__2 in :Trigger.newMap.keySet()
            ]) {
        m.put(c1.custom_object__2, c1.RecordType.DeveloperName);
    }

    for (custom_object__2 c2 : Trigger.new) {
        String developerName = m.get(c2.Id);
        if (developerName == 'xyz') {
            ...
        }
    }
}
Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
 
Luciano RobertoLuciano Roberto

Hi Nagendra,

Thank you for your help,
My code looks like this.

Objects:

- Contest_test__c is the parent object
- Test_data_data__c is the child object (List)


Code:

 

Map<Id, String> m = new Map<Id, String>();
    for (Dados_da_contestacao_teste__c c1 : [ select Contestacao_teste__c, RecordType.DeveloperName from Dados_da_contestacao_teste__c
            							where Contestacao_teste__c in :Trigger.newMap.keySet()  ]) {
        m.put(c1.Contestacao_teste__c, c1.RecordType.DeveloperName);
    }

    for (Contestacao_teste__c c2 : Trigger.new) {
        String developerName = m.get(c2.Id);
        if (developerName == 'RVI') {
            
        }
    }
 

However, I'm getting the following error when trying to save :

User-added image

Luciano RobertoLuciano Roberto

I actually realized that the trigger was not answering me, so I did the following:

I created a custom button that calls the VFP.
I created a VFP that stores the data of the current record (parent record);

<apex:page standardController="Contestacao_RVI__c" extensions="NewDadosContestacaoController">
    
    
    
    
    <apex:form >

         <apex:outputPanel id="Panel" rendered="{!isPanel = true}">
         
         <apex:outputField value="{!currentRecord.Id}" /><br/>
         <apex:outputField value="{!currentRecord.Name}" /><br/>
         <apex:outputField value="{!currentRecord.Nome_Tipo_de_Registro__c}" /> <br/>
         <apex:outputField value="{!currentRecord.Motivo__c}" /><br/>
         
         </apex:outputPanel>
        
      Criar um novo dado de contestação de  <apex:outputField value="{!currentRecord.Plataforma__c}" /> ?<br/><br/>
          <apex:commandButton action="{!NovoDadosContestacao}" value="Confirma"/>

         
    </apex:form>
    
    
    
</apex:page>
 

I created a class that creates the child record based on the parent's criteria, then redirects to the created record (child) in edit mode

public with sharing class NewDadosContestacaoController {

    public String myStr {get; set;}
    
    public Contestacao_RVI__c currentRecord{get; set;}
    
	RecordType tipoDadosContestacao;    
    Id devRecordTypeId;
    
    public Boolean isPanel {get; set;}
    
    
    public NewDadosContestacaoController(ApexPages.StandardController con)
    {
         currentRecord = [SELECT Id, Name, Motivo__c, Plataforma__c, Nome_Tipo_de_Registro__c FROM Contestacao_RVI__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];  
    }
    
    
    
   
    
      
  public PageReference NovoDadosContestacao(){
  
     
       if(currentRecord.Plataforma__c == 'Conectividade')
        {
            tipoDadosContestacao = [SELECT Id, Name FROM RecordType WHERE DeveloperName = 'Conectividade' AND sobjecttype = 'Dados_da_contestacao__c'];
            
            //Pega o ID do tipo de registro pelo nome
            devRecordTypeId = Schema.SObjectType.Dados_da_contestacao__c.getRecordTypeInfosByName().get('Conectividade').getRecordTypeId();
        }
      
     
      else if(currentRecord.Plataforma__c == 'Mobilidade')
        {
            tipoDadosContestacao = [SELECT Id, Name FROM RecordType WHERE DeveloperName = 'Mobilidade' AND sobjecttype = 'Dados_da_contestacao__c'];
            
            //Pega o ID do tipo de registro pelo nome
            devRecordTypeId = Schema.SObjectType.Dados_da_contestacao__c.getRecordTypeInfosByName().get('Mobilidade').getRecordTypeId();
        }
      
       else if(currentRecord.Plataforma__c == 'Soluções digitais')
        {
           tipoDadosContestacao = [SELECT Id, Name FROM RecordType WHERE DeveloperName = 'SolucoesDigitais' AND sobjecttype = 'Dados_da_contestacao__c'];
            
            //Pega o ID do tipo de registro pelo nome
            devRecordTypeId = Schema.SObjectType.Dados_da_contestacao__c.getRecordTypeInfosByName().get('SolucoesDigitais').getRecordTypeId();        }
      
      
     else 
        {
            tipoDadosContestacao = [SELECT Id, Name FROM RecordType WHERE DeveloperName = 'Outros' AND sobjecttype = 'Dados_da_contestacao__c'];
            
            //Pega o ID do tipo de registro pelo nome
            devRecordTypeId = Schema.SObjectType.Dados_da_contestacao__c.getRecordTypeInfosByName().get('Outros').getRecordTypeId();
        }     
      
      
      
    
     			Dados_da_contestacao__c dc = new Dados_da_contestacao__c ( 
                                                                            Contestacao__c = currentRecord.Id,
                                                                            Plataforma_da_contestacao__c = currentRecord.Plataforma__c,
                                                                            Motivo_da_contestacao__c = currentRecord.Motivo__c,
                                                                            Status__c = 'Aberto',
                                                                            RecordTypeId = String.valueOf(devRecordTypeId) 
                														 );
                                                                          //  RecordTypeId = '0123D000000WCHg' );
      
      // Redireciona para o registro criado em modo de edição
      insert dc;
      return (new ApexPages.StandardController (new Dados_da_contestacao__c(Id=dc.Id))).edit();
    
     
          
  }
    
    
    
    
    
}
This was selected as the best answer