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
brunol11brunol11 

Trigger error: execution of AfterUpdate caused by: System.StringException: Invalid id: 0001282:

Hi!

 

I Wonder if anyone could help me on this. 

 

I Have a custom object named "Contratacao__c" and when the field "Status_do_Contrato__c" is filled with the value 'Emitir Boletos' it was suposed to create a new record on the related custom object "Faturamento__c".

 

But after insert or update a "Contratacao__c" record, the trigger below returns the following error:

 

Erro:O acionador CreateBilling do Apex causou uma exceção inesperada. Entre em contato com o administrador: CreateBilling: execution of AfterUpdate caused by: System.StringException: Invalid id: 0001282: Trigger.CreateBilling: line 8, column 36

 

Which means that the "Faturamento__c.Contrato__c" field related to the "Contratacao__c.name" is not filled with the right value. 

In the above example, I have a record in "Contratacao__c" registered as "0001282" and the related record "Faturamento__c" should have a field "Faturamento__c.Contrato__c" filled with the same "0001282"

 

Faturamento__c.Contrato__C: Relationship field (Master and Detail)

Contratacao__c.name = automatica numbered

 

What am I doing wrong? How to Fix it?

 

Here´s the code:

 



trigger CreateBilling on Contratacao__c (after update, after insert) {

Contratacao__c[] Contratos = Trigger.new;

 if (Contratos[0].Status_do_Contrato__c == 'Emitir Boletos')
      {
        Faturamento__c Billing = new Faturamento__c (
        Contrato__c = Contratos[0].name,
        F__c = Contratos[0].Total_do_Contrato__c,
        V__c = Contratos[0].Nascimento__c);
        insert Billing;
      }
 }
Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Update this

Contrato__c = Contratos[0].name

 

to

 

Contrato__c = Contratos[0].Id

 

In your trigger you are assiging String value in ID Type field

 

 Your trigger will be like this

trigger CreateBilling on Contratacao__c (after update, after insert) {

Contratacao__c[] Contratos = Trigger.new;

 if (Contratos[0].Status_do_Contrato__c == 'Emitir Boletos')
      {
        Faturamento__c Billing = new Faturamento__c (
        Contrato__c = Contratos[0].Id,
        F__c = Contratos[0].Total_do_Contrato__c,
        V__c = Contratos[0].Nascimento__c);
        insert Billing;
      }
 }

 

All Answers

Shashikant SharmaShashikant Sharma

Update this

Contrato__c = Contratos[0].name

 

to

 

Contrato__c = Contratos[0].Id

 

In your trigger you are assiging String value in ID Type field

 

 Your trigger will be like this

trigger CreateBilling on Contratacao__c (after update, after insert) {

Contratacao__c[] Contratos = Trigger.new;

 if (Contratos[0].Status_do_Contrato__c == 'Emitir Boletos')
      {
        Faturamento__c Billing = new Faturamento__c (
        Contrato__c = Contratos[0].Id,
        F__c = Contratos[0].Total_do_Contrato__c,
        V__c = Contratos[0].Nascimento__c);
        insert Billing;
      }
 }

 

This was selected as the best answer
brunol11brunol11

Thanks a lot! worked pretty fine. 

 

brunol11brunol11

Hi Shashikant Sharma

 

What sugestion could you give me to make the trigger builds 12 Billings?

Can I have a loop inside a Trigger?

Any idea?

 

Thanks in advance!