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
mhittmhitt 

Add if condition to code (new to apex)

Hello!

I have been using Salesforce for a couple of years now, but I am starting to dip my toes in Apex.  In the past we have utalized offshore resources, but to make a long story short we don't want to soely rely on them for coding and instead call them for larger projects.  I've found where the new condition needs to be added, but I'm not 100% sure what needs to be done.  Basically, I want to say "if custom checkbox = true, dont update the owner field".  Could I please have some help adding the new condition?

Thanks a bunch!

Here is the current code block:
//Set the Order Owner with Order Sales Rep
    public static void UpdateOrderOwner(list<Order> listNew, map<id,Order> mapOld){
        RTT_Setting__mdt DefOwnSett = [SELECT Id, DeveloperName, Value__c FROM 
                                       RTT_Setting__mdt where DeveloperName='Default_Residential_Order_Owner' limit 1];
        
        for(Order rec: listNew ){
            if(rec.RecordTypeId==vResOrderRecTypeId){
                if(mapOld.isEmpty() ||rec.Primary_Sales_Rep__c <> mapOld.get(rec.Id).Primary_Sales_Rep__c){
                    if(rec.Primary_Sales_Rep__c == null ){
                        rec.OwnerId=DefOwnSett.value__c;
                    }
                    else {
                        rec.OwnerId=rec.Primary_Sales_Rep__c;
                    }
                }
            }
mhittmhitt
Here is my assumption: 
 
if(mapOld.isEmpty() ||rec.Primary_Sales_Rep__c <> mapOld.get(rec.Id).Primary_Sales_Rep__c){
                    if(rec.Primary_Sales_Rep__c == null,
                       rec.New_Custom_Field__c == False ){
                        rec.OwnerId=DefOwnSett.value__c;
                    }
                    { if (rec.Primary_Sales_Rep__c != null,
                       rec.New_Custom_Field__c == False ){
                        rec.OwnerId=rec.Primary_Sales_Rep__c;
                   }
                    else {
                        rec.OwnerId=rec.________; (not sure what would go here)

 
AbhishekAbhishek (Salesforce Developers) 
yes you are right.
Maharajan CMaharajan C
Hi Matt,

Just add your condition in existing if condition itself like below:

for(Order rec: listNew ){
            if(rec.RecordTypeId==vResOrderRecTypeId && rec.New_Custom_Field__c == False){
                if(mapOld.isEmpty() ||rec.Primary_Sales_Rep__c <> mapOld.get(rec.Id).Primary_Sales_Rep__c){
                    if(rec.Primary_Sales_Rep__c == null ){
                        rec.OwnerId=DefOwnSett.value__c;
                    }
                    else {
                        rec.OwnerId=rec.Primary_Sales_Rep__c;
                    }
                }
            }
        }


Thanks,
Maharajan.C