2

I need some help, I have the following trigger, however I get the following error message “Error: Compile Error: expecting a right parentheses, found 'Integer' at line 5 column 8” What I am trying to do, is just make this trigger work for this one record type.

Record type ID = '012R00000004wJv'

My trigger is

trigger ObjectNameCheckProducts on Campaign (before insert, before update) {
  for(Campaign o : Trigger.New){

    If(o.Campaign.RecordType == '012R00000004wJv'
    Integer counter = 0;

    if(o.Institutional_Products_A_G__c != null) 
       counter += o.Institutional_Products_A_G__c.split(';').size();
    if(o.Institutional_Products_G_Z__c != null)
       counter += o.Institutional_Products_G_Z__c.split(';').size();
    if(o.Intermediary_Products_A_F__c != null)
       counter += o.Intermediary_Products_A_F__c.split(';').size();
    if(o.Intermediary_Products_G_L__c != null)
       counter += o.Intermediary_Products_G_L__c.split(';').size();
    if(o.Intermediary_Products_M_Z__c != null)
       counter += o.Intermediary_Products_M_Z__c.split(';').size();

    if(counter > 5) o.addError('Max 5 values can be selected from Product lists');
 }    

}

2 Answers 2

5

Compile Error: expecting a right parentheses, found 'Integer'

and

If(o.Campaign.RecordType == '012R00000004wJv'

Looks like your if statement is missing a right parentheses. Note that the full error message say that it found 'Integer' on the line after (line 5) the if statement - with no closing parentheses at the end of line 4, the parser kept going until it found something that didn't belong in an if statement. If you don't see the error on the line indicated, always look at the line(s) directly above.

Also, the field you need to check is RecordTypeId, not RecordType (see reference). Try this:

If(o.Campaign.RecordTypeId == '012R00000004wJv')

However, you shouldn't be hardcoding a record type id (or any id) into your code. Instead, look up the record type id by name. You could query the RecordType table, but there's a better way (reference):

trigger ObjectNameCheckProducts on Campaign (before insert, before update) {
  // Look up record type id
  String recordTypeName = 'Long-Term Strategic Products'; // <-- Change this to your record type name
  Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Campaign.getRecordTypeInfosByName();
  Schema.RecordTypeInfo rtInfo =  rtMapByName.get(recordTypeName);
  id recordTypeId = rtInfo.getRecordTypeId();

  for(Campaign o : Trigger.New){
    if(o.RecordTypeId == recordTypeId) {
      Integer counter = 0;

      if(o.Institutional_Products_A_G__c != null) 
        counter += o.Institutional_Products_A_G__c.split(';').size();
      if(o.Institutional_Products_G_Z__c != null)
        counter += o.Institutional_Products_G_Z__c.split(';').size();
      if(o.Intermediary_Products_A_F__c != null)
        counter += o.Intermediary_Products_A_F__c.split(';').size();
      if(o.Intermediary_Products_G_L__c != null)
        counter += o.Intermediary_Products_G_L__c.split(';').size();
      if(o.Intermediary_Products_M_Z__c != null)
        counter += o.Intermediary_Products_M_Z__c.split(';').size();

      if(counter > 5) o.addError('Max 5 values can be selected from Product lists');
    }
  }
}    

Finally - Note the added braces after the initial if, wrapping the other if statements.

10
  • @ jason Clark Where do i put String recordTypeName = 'Long-Term Strategic Products' // <-- Change this to your record type name Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.Campaign.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtInfo = rtMapByName.get(recordTypeName); id recordTypeId = rtInfo.getRecordTypeId(); and the if(o.Campaign.RecordTypeId == recordTypeId) into my trigger ?
    – Masond3
    Commented Mar 31, 2014 at 14:22
  • Anywhere before you use it. In this case, it must be before the if statement; before the for is best. I've updated my answer to show the full trigger. Commented Mar 31, 2014 at 14:28
  • @ Jason Clark, if i use your trigger above i get the following Error: Compile Error: expecting a semi-colon, found 'Map' at line 4 column 2
    – Masond3
    Commented Mar 31, 2014 at 14:33
  • See my original advice; check the line before the error :) On the line before line 4, String recordTypeName = 'Long-Term Strategic Products', I forgot the semicolon. I've updated the code in my answer. Commented Mar 31, 2014 at 14:35
  • @ jason Clark Thanks for providing update but i now get the following :( Error: Compile Error: Method does not exist or incorrect signature: Schema.Campaign.getRecordTypeInfosByName() at line 4 column 51
    – Masond3
    Commented Mar 31, 2014 at 14:39
4

It looks like you're trying to implement this Maximum 5 check for the given record type. If so, you'll need to encompass all the counter increment logic in the if statement (see my edit below).

Furthermore, hardcoding Ids is a terrible idea and worst practice. Either switch the RecordType check to use a Custom Setting instead, or use an additional query to fetch the Record Type Id before looping over the trigger results.

More on custom settings here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm

trigger ObjectNameCheckProducts on Campaign (before insert, before update) {
    for(Campaign o : Trigger.New){

    //Assuming you have created a custom hierarchy setting with CampaignRecordType__c field
        if(o.Campaign.RecordType ==  Custom_Setting__c.getInstance().CampaignRecordType__c){ 
            Integer counter = 0;

            if(o.Institutional_Products_A_G__c != null) 
                counter += o.Institutional_Products_A_G__c.split(';').size();
            if(o.Institutional_Products_G_Z__c != null)
                counter += o.Institutional_Products_G_Z__c.split(';').size();
            if(o.Intermediary_Products_A_F__c != null)
                counter += o.Intermediary_Products_A_F__c.split(';').size();
            if(o.Intermediary_Products_G_L__c != null)
                counter += o.Intermediary_Products_G_L__c.split(';').size();
            if(o.Intermediary_Products_M_Z__c != null)
                counter += o.Intermediary_Products_M_Z__c.split(';').size();

            if(counter > 5) o.addError('Max 5 values can be selected from Product lists');
        }
    }    
}

Alternatively, here is what you could do instead, with querying for recordtype.developername:

trigger ObjectNameCheckProducts on Campaign (before insert, before update) {

    Id recordTypeId = [Select Id From RecordType Where SObjectType = 'Campaign' And DeveloperName='Your RT's dev name'].Id;

    for(Campaign o : Trigger.New){
        if(o.Campaign.RecordType == recordTypeId){ 
            Integer counter = 0;

            if(o.Institutional_Products_A_G__c != null) 
                counter += o.Institutional_Products_A_G__c.split(';').size();
            if(o.Institutional_Products_G_Z__c != null)
                counter += o.Institutional_Products_G_Z__c.split(';').size();
            if(o.Intermediary_Products_A_F__c != null)
                counter += o.Intermediary_Products_A_F__c.split(';').size();
            if(o.Intermediary_Products_G_L__c != null)
                counter += o.Intermediary_Products_G_L__c.split(';').size();
            if(o.Intermediary_Products_M_Z__c != null)
                counter += o.Intermediary_Products_M_Z__c.split(';').size();

            if(counter > 5) o.addError('Max 5 values can be selected from Product lists');
        }
    }    
}
7
  • @ James loghry, im new to apex i dont mind using the id which is 012R00000004wJv or record type label "Long-Term Strategic Products" I just dont know where to put it in the trigger
    – Masond3
    Commented Mar 31, 2014 at 14:14
  • The issue with hard coding ids in your code is that Ids change between environments. Once you deploy this code and the RecordType to production, the Id of the record type will change, and your code will be broken. You can grab the DeveloperName of the Record Type by either going into the Record Type in your Salesforce Setup UI, or doing a query using execute anonymous System.debug([Select DeveloperName From RecordType Where Id='012R00000004wJv']); and then plug that into the text where I show 'Your RT's dev name' Commented Mar 31, 2014 at 14:21
  • 2
    Your query example assumes that no other object has a record type of the same name - you should add a criteria for SObjectType = 'Campaign, or use getRecordTypeInfosByName() Commented Mar 31, 2014 at 14:21
  • Good point Jason, I've updated the query. Commented Mar 31, 2014 at 14:22
  • 1
    'changing the ID when in live' is an invitation to disaster. Don't be confused: There are several methods for avoiding hardcoded ids in code: 1. Move the id into a custom setting. This is James' first example. However, you still need to update each environment. 2. Query the RecordType table. This is James' second example. 3. Use the Schema object and getRecordTypeInfosByName(). This is my example (see my answer). This uses information about your org's schema that is cached by apex, and so avoids a SOQL query against your limits. Commented Mar 31, 2014 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .