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
Marty DorrenMarty Dorren 

does map exsist?

I have triggers that reach out to other custom objects subsequently firing their triggers. at the beginning of each trigger for each object, I create a map at the begining of each trigger containing all recordtypes in case needed, which is not usually a problem however, when these triggers start firing one another, I run the risk of reaching my SOQL limit.

The simple solution for me would be to only create the map if it doesn't exist from a previously called trigger.

Is there a way that I can test for the existence of a map before creating it again?

Thanks,
Marty
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior

Hello,

I am not sure if I got it right but if you are trying to persist a Map across your triggers that it is not possible. And if you are getting these RecordTypes by a SOQL statement you should consider using the Schema.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_Schema_RecordTypeInfo.htm

I would be something like this:

Map<String, Schema.RecordTypeInfo > rts = yourCustomSObject.sObjectType.getDescribe().getRecordTypeInfosByName();

Id rtId = rts.get('Record Type Name').getRecordTypeId();

Regards

Marty DorrenMarty Dorren
Hi,
Thanks for the quick reply.  It seems that the describe method is more granular in it's execution as opposed to one soql query such as:

string RecTypeMapKey = null;
Map<string,Id> RecTypeMap = new Map<string,Id>();
list<Recordtype> RecTypeList = [select Id,name,SObjectType from RecordType where isactive = true];
    for(RecordType RecTypeLoop : RecTypeList){
    RecTypeMapKey = RecTypeLoop.name+RecTypeLoop.SObjectType;
    RecTypeMap.put(RecTypeMapKey,RecTypeLoop.Id);
}

So I suppose my next question would be:
Can I persisist a variable across my triggers?

If so, I could could put the above code at the beginning of my triggers inside an IF condition asking if the variable was true or false:
if(RecTypeStaticVar == false){
    //Above code
}

Am I making any sense here? :)

Thanks
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

I am afraid you cannot persist variables between triggers as static variables in Salesforce does not behave as you are expecting.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm" target="_blank)

Furthermore, using the schema you can also check if a recordtype is active without querying it using SOQL.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_Schema_RecordTypeInfo.htm#apex_Schema_RecordTypeInfo_isAvailable (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_Schema_RecordTypeInfo.htm#apex_Schema_RecordTypeInfo_isAvailable" target="_blank)

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you. 
Marty DorrenMarty Dorren
I suppose that takes care of the last "rabbit out of my hat".  It seems that using the schema, I can create a map for every trigger without using my SOQL limits, although if I'm not mistaken, the schema also has a 100 ceiling.  It should aleviate the SOQL threshold though.  I was hoping that there was a way of putting all of the recordtype records in custom settings, but there doesn't seem to be a way of dynamically adding and updating it.  Is there a way to loop through the entire recordtype object using the schema technique?  It looks like I have to be specific when querying the schema.  Thanks again.