1

Unfortunately, I'm not able to use simple Validation Rules to do my conditional statement validation. Instead I have to use a trigger to do so.

That being said, currently, I'm trying to only run this trigger on certain profile types. We have different records for our Opportunities and I only want it to run on, for example 'SGIOpportunities'. Here is the current code I have:

trigger UpdateOpportunity on Opportunity (before insert, before update){    

    Set<String> recordTypeToValidateAgainst = new Set<String>();
    recordTypeToValidateAgainst.add('SGIOpportunities'); // SGI Opportunity Type

    for(Opportunity opp: trigger.new){
        if(recordTypeToValidateAgainst.contains(Schema.RecordTypeInfo.getName()) && opp.StageName != '0 %'){
            OpportunityMethods.updateOppCompetitor(opp);  
        }
    }  
}

**OpportunityMethods.updateOppCompetitor(opp) is declared in an APEX class, that are my conditional statements.

Also the error I'm getting in the compiler is:

Error: Compile Error: Method does not exist or incorrect signature: Schema.RecordTypeInfo.getName() at line 7 column 49

Note: I do have the fully qualified name: Schema.RecordTypeInfo.getName() listed and not sure why it's not able to get the value..

Thanks for you help in advance!

4
  • what is the actual need for the trigger? Why can't you use a validation rule?
    – Rao
    Sep 2, 2014 at 16:28
  • I'm trying to dynamically update a multi-select picklist which you can't do with Validation Rules on certain conditions. Sep 2, 2014 at 16:31
  • are you trying to see if the recordtype of the opportunity record being saved / edited matches against a specific record type 'SGIOpportunities'?
    – Rao
    Sep 2, 2014 at 16:38
  • Yes, the 'record type name' is 'SGIOpportunities'. Just to reiterate I only want the trigger to perform on recordtype = 'SGIOpportunities'. Sep 2, 2014 at 16:48

1 Answer 1

2

I rephrased the answer from this SFSE post to accommodate your need :

How to find current record's RecordType name

trigger UpdateOpportunity on Opportunity (before insert, before update){    

    Set<String> recordTypeToValidateAgainst = new Set<String>();
    recordTypeToValidateAgainst.add('SGIOpportunities'); // SGI Opportunity Type

map<Id, Schema.RecordTypeInfo> rt_map = Schema.getGlobalDescribe().get('Opportunity').getDescribe().getRecordTypeInfosById();


    for(Opportunity opp: trigger.new){
        if(recordTypeToValidateAgainst.contains(rt_map.get(opp.recordTypeID).getName()) && opp.StageName != '0 %'){
            OpportunityMethods.updateOppCompetitor(opp);  
        }
    }  
}

OR

This should work too:

    trigger UpdateOpportunity on Opportunity (before insert, before update){    

    map<Id, Schema.RecordTypeInfo> rt_map = Schema.getGlobalDescribe()get('Opportunity').getDescribe().getRecordTypeInfosById()
    for(Opportunity opp : trigger.new){
        if(rt_map.get(opp.recordTypeID).getName().containsIgnoreCase('SGIOpportunities') && opp.StageName != '0 %'){
              OpportunityMethods.updateOppCompetitor(opp);  
        }
}     
}

Update: Missed a . here Was:

getGlobalDescribe()get('Opportunity')

updated to:

getGlobalDescribe().get('Opportunity')

8
  • Thanks rao. My compiler is looking for a semi-colon for the get statement. Please see error below: Error: Compile Error: expecting a semi-colon, found 'get' at line 3 column 65 I even added a semi-colon at the end of getRecordTypeInfosById() Sep 2, 2014 at 19:45
  • 1
    @user3708392 updated the ans
    – Rao
    Sep 2, 2014 at 20:03
  • Hi rao, your solution is not working, and my admin is saying that I'm making the trigger to complex for what I'm trying to do. He recommended the following trigger: trigger UpdateOpportunity on Opportunity (before insert, before update){ for(Opportunity opp : trigger.new){ if(opp.RecordType.Name == 'SGI Opportunity' && opp.StageName != 'Lead 0 %'){ OpportunityMethods.updateOppCompetitor(opp); } } } But that doesn't work either. All I'm really trying to check is if Record Type Name = SGIOpportunity, if so perform the logic. Sep 4, 2014 at 13:48
  • 1
    @user3708392 your trigger will work if you change for(opportuntiy opp: trigger.new){} to for(opportuntiy opp: [ select id, recordtype.name, stagename from opportunity where id in:trigger.new]){} you can access recordtypeid without querying for recordtype from trigger.new, but not the name or other recordtype related columns.
    – Rao
    Sep 4, 2014 at 16:50
  • 1
    it should be trigger.new() , sorry have been replying from my phone so lazy typing ()
    – Rao
    Sep 4, 2014 at 16:53

You must log in to answer this question.

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