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
OcoBriOcoBri 

Error on my first trigger

I'm writing a trigger that will:

Every time a new contact is created, of Record Type 'Consumer', I want to create a corresponding record in the master-detail custom object Compliance2__c.  The only field that needs to be passed is the lookup value.  The other fields on Compiance2__c are formulas.  
The error I'm getting is: "Error: Compile Error: unexpected token: 'for' at line 5 column 4"

My code is:
trigger createCompliance on Contact (after insert) {
    
    List <Compliance2__c> compToInsert = new List <Compliance2__c>
    
    for (Contact c : Trigger.new) {
        
        //check if this is a Consumer being created
        if (c.RecordType.Name = 'Consumer') {
            //create a new Compliance for each new Consumer
            Compliance2__c i = new Compliance2__c ();
            i.Consumer__c = c.Id;
            //add to the list of Compliances to insert
            compToInsert.add(i);
        } //end if
    } //end for c
    //after loop, insert new Compliances
    insert complianceToInsert;  
}

Any help is appreciated.
Thank you!
Admin Admin 31Admin Admin 31
The line number 3 should be:
List <Compliance2__c> compToInsert = new List <Compliance2__c>();
Justin ManchesterJustin Manchester
Hello,
Looks like it should be a quick fix. You are just missing this snippet at the end of line 3 in your code:
( );
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Instead of this c.RecordType.Name = 'Consumer'

 you can give c.name='Consumer'
OcoBriOcoBri
Thanks, I added the (); and was able to save.

But it's not working.  I've added test Contacts and no Compliances are created.
Justin ManchesterJustin Manchester
Alrighty, give this a shot. This will work for what you are looking to accomplish. I do not see a real need for a list to be used because you are only creating one related record. (Per record entering the trigger)

trigger createCompliance on Contact (after insert) {
    for (Contact c : Trigger.new) {    
        //check if this is a Consumer being created
        if (c.RecordType.Name = 'Consumer') {
            //create a new Compliance for each new Consumer
            Compliance2__c newComp = new Compliance2__c ();
            newComp.Consumer__c = c.Id;
            insert newComp;
        } //end if
    } //end for c
}
Jim JamJim Jam
Don't think you can get record type the way you're trying. Declare a variable before the for loop like this ..

Id consumerRecTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Consumer').getRecordTypeId();

and then replace if(c.RecordType.Name = 'Consumer') with ...

if(c.RecordTypeId == consumerRecTypeId)
Justin ManchesterJustin Manchester
WOW.... I totally did not notice this and really should have. Ok so your code can be used: with the List.
But your "IF" will not ever meet because you only have one "=" sign. This needs to be two "==" otherwise it is trying to assign that value not compare.

trigger createCompliance on Contact (after insert) {
    List <Compliance2__c> compToInsert = new List <Compliance2__c>();
    for (Contact c : Trigger.new) {
        //check if this is a Consumer being created
        if (c.RecordType.Name == 'Consumer') {
            Compliance2__c i = new Compliance2__c ();
            i.Consumer__c = c.Id;
            compToInsert.add(i);
        } 
    } 
}


OcoBriOcoBri
Sorry, I did actually fix the = to == but didn't post it.  I also tried cmrc1001.3884211976265837E12's approach, as well as something else I found from 4 or 5 years ago, but it's still not working.

The current code is this:

trigger createCompliance on Contact (after insert) {
   
    List <Compliance2__c> compToInsert = new List <Compliance2__c>();
   
    for (Contact c : Trigger.new) {
       
        //check if this is a Consumer being created
        //Get Consumer Record Type ID
        Id consumerRecTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Consumer').getRecordTypeId();
        if (c.RecordTypeId == consumerRecTypeId) {
            //create a new Compliance for each new Consumer
            Compliance2__c i = new Compliance2__c ();
            i.Consumer__c = c.Id;
            //add to the list of Compliances to insert
            compToInsert.add(i);
        } //end if
    } //end for c
    //after loop, insert new Compliances
    try {
        insert compToInsert;
    } catch (system.Dmlexception e) {
        system.debug(e);
    }
}

Jim JamJim Jam
Do you get as far as line 20? is there anything in the debug logs? Other than Consumer__c are there any other required fields on the Compliance2__c object? You need to do some more debuggin to get to the bottom of this I'm afraid.