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
Perspectiva Negócios DigitaisPerspectiva Negócios Digitais 

Trigger Insert Before Insert with .csv

I created an object called Dados_Utiliza_o__c which contains about 50 fields, which are populated with .csv, among these fields there is a called Nome_do_Paciente__C that contains the name of an account, where my intention is to create a trigger before inserting, where:
1. Check if there is an account (patient in my case) with the CPF (document of unique registration of person in Brazil, similar to Social Security Card) registered in the system;
2. If it does not exist it starts the process of registering
3. As .csv sends the full name in the field, then it breaks the name to enter in the salesforce, first name and last name.
4. Register or update the account if it is already on the system.
5. If the registered account name is the same as the Nome_do_Segurado__c field (another field and something that can occur in .csv), it lists this created or updated account, otherwise it just inserts in text form in the unrelated field

this is my trigger currently, I'm not finding what's wrong or if something is missing:

trigger PacienteUtilizacao on Dados_Utiliza_o__c (before insert) {
    
	Account paciente;
    Account segurado;
    String firstSplit;
    String restSplits; 
   
    RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Paciente' AND SObjectType = 'Account'];
    
    //verifica se existe um account com o preenchido
    for(Dados_Utiliza_o__c utilizacao : Trigger.new)
    {
    	try {
            paciente = [
                SELECT
                	FirstName,
                	LastName,
                    Sexo__pc,
                    Birthdate__c,
                    CPF__pc
                FROM Account
                WHERE CPF__pc = :utilizacao.CPF_CGC_do_Referenciado__c
                LIMIT 1
            ];
        //se não encontrar ele cadastra
		} catch (Exception e) {
       		paciente = new Account();         
		}
        
        //Quebra o nome do paciente em First Name e Last Name
		if(utilizacao.Nome_do_Paciente__r.Name != null){
            String input = utilizacao.Nome_do_Paciente__r.Name;
            firstSplit = input.substringBefore(' ');
            restSplits = input.substringAfter(' ');
        }
        
        //Quando não encontrar o paciente no try de cima ele então cadastra
        try {             
        	paciente.RecordTypeId = personAccountRecordType.Id;
            paciente.FirstName = firstSplit;
            paciente.LastName = restSplits;
            paciente.Sexo__pc = utilizacao.Sexo__c;
            paciente.Birthdate__c = utilizacao.Data_de_Nascimento_Y2K__c;
            paciente.CPF__pc = utilizacao.CPF_CGC_do_Referenciado__c;
            upsert paciente;
        } catch (Exception e) {
            System.debug(e.getMessage());
        }
          
        IF (paciente.Name == utilizacao.Nome_do_Segurado__c){
			utilizacao.Nome_do_Segurado__c = paciente.Id;
			// se for, entao o id do segurado é o id que achamos para o paciente    
        } ELSE{
             // se nao for, vamos tentar buscar o segurado pelo nome.
			try {
            	segurado = [
                	SELECT
                    FirstName,
                    LastName,
                    Sexo__pc,
                    Birthdate__c,
                    CPF__pc
                    FROM Account
                    WHERE CPF__pc = :utilizacao.Nome_do_Segurado__c
                    LIMIT 1
                ];
                // se acharmos, entao vamos bucsar o ID no registro de utilizacao que estamos criando.
                utilizacao.Nome_do_Segurado__c = segurado.Id;
            } catch (Exception e) {
            	System.debug(e.getMessage());        
            }
        }
    }
}
karthikeyan perumalkarthikeyan perumal
Hello, 

you mean to say, you have CSV fiel that contains Accounts( patient details in your case) you want to upload that CSV in to SF system. during this insertion you want check  if the account name is already registered in this SF system or not. if Yes  you need to update that record else you want insert it..
this is you logic you trying to expline  am i correct?..

Thanks
karthik