Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Trigger Noob here trying to finish my first trigger... thanks for bearing with me!
Why am I getting the following error when trying to save this trigger?

Compile Error: expecting a right parentheses, found 'E000000092pN' at line 3 column 34

   trigger NewEnrollee on Contact (after insert) {
        for (Contact con : Trigger.new) {
            if (con.RecordTypeId = 012E000000092pN) {
                Enrollment_Process__c ep : new Enrollment_Process__c();
                ep.Student_Name__c = con.Id;
                ep.RecordTypeId    = '012E000000092pX';
                insert ep;
            }
        }
    }

The business requirement I am trying to satisfy is: any time a Contact of the record type "Student" is created, a "New Enrollment Process" record should also be created associated to the new Student.
012E000000092pN is the record type id for "Student" Contacts. 012E000000092pX is the record type id for "New Enrollment" (custom object Enrollment_Process__c has two record types: New Enrollment and Re-Enrollment. I need the newly created enrollment process record to be of the record type "New Enrollment").

share|improve this question
up vote 8 down vote accepted

You forgot to wrap Id in quote ' and use == instead of single =. Should be:

if (con.RecordTypeId == '012E000000092pN') {
....
....
}

Apart of this, it is not recommended to use hardcoding of Ids in code. You should query Id from the RecordType object.

You can get record type Id this way:

// RecordTypeLabel is name of Record Type e.eg: New-Enrollment or Student.
Id newEnrollId = Schema.Sobjecttype.Account.getRecordTypeInfosByName().get('RecordTypeLabel').getRecordTypeId();`

AND

Do not preform DML in for loop. This is bad way. Always use List and then perform DML operation on List. As insert listOfNewEnrollRecs;

share|improve this answer
    
Use double equal to if (con.RecordTypeId == '012E000000092pN') – Robert Barnett Feb 12 at 7:01
    
Thanks @RobertBarnett for pointing out :) Edited answer as per you advice. – Ashwani Feb 12 at 7:02
    
Line 4 also had an error! I used : instead of = – Jake Schmitz Feb 12 at 21:58

Whenever you do any bulk processing in your trigger, never perform DML or SOQL in for loop.

And you have forgot to wrap Id in quote:

Your code should look like below:

trigger NewEnrollee on Contact (after insert) {
    List<Enrollment_Process__c> enrollmentProcessList = New List<Enrollment_Process__c>();
    for (Contact con : Trigger.new) {
        if (con.RecordTypeId.equalsIgnoreCase('012E000000092pN')) {
            Enrollment_Process__c ep : new Enrollment_Process__c();
            ep.Student_Name__c = con.Id;
            ep.RecordTypeId    = '012E000000092pX';
            enrollmentProcessList.add(ep);
        }
    }
    if(!enrollmentProcessList.isEmpty()){
        INSERT enrollmentProcessList;
    }
}
share|improve this answer
1  
aside from the hardcoded ids as pointed out elsewhere; `equalsIgnoreCase(..) on an ID field is unnecessary as IDs are, in fact, case sensitive – cropredy Feb 12 at 19:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.