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
RashaRasha 

how to limit code firing for only one object record type record type

I have an Apex class that has sets a checkbox to true if certian criteria is met. Whet I need to do is to limit this change to only one record type. I'm bewbie to coding in Apex. Any suggestions on where to start from? 
Best Answer chosen by Rasha
Agustin BAgustin B
Hi, that error is because you are not checking for anything on the if statement, you are just getting the record type for AEP.
how does pse__Assignment__c  relate to pse__Proj__c?
You need to get the pse__Proj__c.RecordType and check if that is equal to Schema.SObjectType.pse__Proj__c.getRecordTypeInfosByDeveloperName().get('AEP').getRecordTypeId()

If you dont know how to do this please show from which field does pse__Assignment__c  relate to pse__Proj__c

All Answers

Agustin BAgustin B
Hi Rasha, check for the record type you want using Schema, for example for person account record type:
Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
With that if the the record type is the one you want then you set the checkbox to true.


if this solves your issue please mark this answer as correct, it may help others
RashaRasha
@Agustin, 
Thank you for the reply. When I add this line your provided, 
Schema.SObjectType.Project.getRecordTypeInfosByDeveloperName().get('Sourcing').getRecordTypeId();
I get the below error
"Error: Compile Error: Variable does not exist: Schema.SObjectType.Project at line 206 column 9" Any idea why? 
Agustin BAgustin B
Hi, try with Project__c because it is not a standard object
RashaRasha
Hi, 
Still getting the same error
"Error: Compile Error: Variable does not exist: Schema.SObjectType.Project__c at line 206 column 9"
Agustin BAgustin B
hi Rasha so this is not working? 
Id objectRecordTypeId = Schema.SObjectType.CustomObject__c.getRecordTypeInfosByName().get('CustomObject').getRecordTypeId();
Which is the Api name of the object and the developer name of the record type?
RashaRasha
Hi, I was uing the wrong API name for the object. When I use the correct one "Schema.SObjectType.pse__Proj__c.getRecordTypeInfosByDeveloperName().get('AEP').getRecordTypeId();"
And 'AEP' is the dev name for the record type, I get the below error 

Error: Compile Error: Method does not exist or incorrect signature: void getRecordTypeInfosByDeveloperName() from the type Schema.DescribeSObjectResult at line 206 column 41
Agustin BAgustin B
Hi Rasha, AEP is the record type name of the record type? I am testing this method and its working properly.
Please show me an image of the record type AEP.
You can try with system.debug(Schema.SObjectType.pse__Proj__c.getRecordTypeInfosByDeveloperName().get('AEP').getRecordTypeId());
Is pse a prefix?
RashaRasha
Hi,
Yes, AEP is the record type name. And yes, pse if a prefix. the Project's object API is "pse__Proj__c"User-added image
RashaRasha
I just tried "    system.debug(Schema.SObjectType.pse__Proj__c.getRecordTypeInfosByDeveloperName().get('AEP').getRecordTypeId());   " and got the same exact error:

Error: Compile Error: Method does not exist or incorrect signature: void getRecordTypeInfosByDeveloperName() from the type Schema.DescribeSObjectResult at line 206 column 41
Agustin BAgustin B
Rasha, verify that the version of the class is more than 42.
in Setup->Apex classes->your class->Click on Edit and Change your apex class version to 43 by switching to version settings tab .
RashaRasha
Hi, 
Why do we need to change the version. Can you please let me kno what impact that would have? 
I have updated the version, which then I was able to use this code and save the chane successfully 
"Schema.SObjectType.pse__Proj__c.getRecordTypeInfosByDeveloperName().get('AEP').getRecordTypeId();"

Even when saving the changes, the code still seems to work on all record types. I have multiple records, and the code is control a checklist field. When I try to update the checklist field on non AEP record types, the code fires off, and revert my changes. 
Agustin BAgustin B
Hi Rasha, that is because an older version did not have that getRecordTypeInfosByDeveloperName method.
It wont have a negative impact, if salesforce removes methods for example that may be an impact but if you compile correctly then you should be fine.
Are you using a if(pse__Proj__c.RecordType ==Schema.SObjectType.pse__Proj__c.getRecordTypeInfosByDeveloperName().get('AEP').getRecordTypeId()) then do something condition?

please show me your code.

If you are happy with your progress please like my answers or select them as correct ones.

Regards
RashaRasha
Hi Agustin, 
When I add the if statement, I get this error "
Error: Compile Error: Condition expression must be of type Boolean: Id at line 207 column 91"

Here is the code: 
private void excludeSmallAssignmentsFromPlanner(pse__Assignment__c a) {
        Decimal threshold = PSA_General_Settings__c.getInstance().Exclude_from_Planner_Threshold__c;
        
        if(Schema.SObjectType.pse__Proj__c.getRecordTypeInfosByDeveloperName().get('AEP').getRecordTypeId() )//Rasha
        
        
        {//Rasha
        if( threshold == null ) {
            return;
        }
        Decimal numWeeks = a.pse__Start_Date__c.daysBetween( a.pse__End_Date__c ) / 7.0;
        if( numWeeks < 1.0 ){
            numWeeks = 1.0;
        }
        Decimal hoursPerWeek = a.pse__Scheduled_Hours__c / numWeeks.round(System.RoundingMode.HALF_UP);

        System.debug('Assignment updated with '+numWeeks+' weeks of time and '+hoursPerWeek+' hours per week. Threshold is '+threshold+'.');

        if( hoursPerWeek <= threshold ) {
            a.pse__Exclude_from_Planners__c = true;
        } else if (a.pse__Exclude_from_Planners__c == true) {
            a.pse__Exclude_from_Planners__c = false;
        }
        
        }//Rasha
    }


Thank you for your time and effore to help here even if I don't get to accomplish my goal of updating the code, I'll certainly select your answer as correct. 


 
Agustin BAgustin B
Hi, that error is because you are not checking for anything on the if statement, you are just getting the record type for AEP.
how does pse__Assignment__c  relate to pse__Proj__c?
You need to get the pse__Proj__c.RecordType and check if that is equal to Schema.SObjectType.pse__Proj__c.getRecordTypeInfosByDeveloperName().get('AEP').getRecordTypeId()

If you dont know how to do this please show from which field does pse__Assignment__c  relate to pse__Proj__c
This was selected as the best answer