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
ranga babu vangalapudi 2ranga babu vangalapudi 2 

how to reduce if condition in for loop in a apex class

Hi ,

I have written the following Rest api, WHILE testing it using postman i am getting an error: "Apex:System CPU Limit exceeded."

Can any one help me with this.

@RestResource (urlMapping='/Outletmasters/*')
//https://cs73.salesforce.com/services/apexrest/Outletmasters/
global with sharing class OutletRestClass {  
    @HttpGet
    global static list<Account> getAccounts(){
           DateTime dt= system.today();
           DateTime starttime = dt.addMinutes(+90);
        DateTime endtime = dt.addMinutes(+990);     
        //where ((LastModifiedDate >=: starttime AND LastModifiedDate <=: endtime) OR (CreatedDate >=: starttime AND CreatedDate <=: endtime)) AND Ipas_Flag__c !=true  
        String query = 'Select Id,Name, Active__c,Address__c,Beat_Code__r.Name, Beat_Name__c,Channel_type__c,Cluster__c,Cluster_Code__r.Cluster_Code__c,Contract_type__c, Country_liquor_sold__c,District__c,Geo_Location__latitude__s,Geo_Location__longitude__s,Goegraphy_state__c,Group_Outlet__c,Group_Name__c,Group_ID_1_del__c,Location_Type__c,Lower_Prestige__c,Luxury_Core__c,Market_Type__c,Micro_channel__c,Mid_Prestige__c,MSL_Town_Tier__c,O_L_Id__c,Outlet_Contact_Person__c,Outlet_License_Code__c,Outlet_License_Name__c,Outlet_Mobile__c,Overall_Segmentation__c,Perfect_Outlet__c,Pincode__c,Premium_Core__c,Primary_Occasion__c,Primary_Customer_Name__r.Name,Primary_Customer_ID_del__c,RPC__c,RPC_Code__c,Secondary_Occasion__c,TL_Code__r.Name,Town__c,Town_code__c,TSE_Code__r.Name,TL_Territory_Name__c,TSE_Name__c,USL_Outlet_ID__c,Upper_Prestige__c,Visit_Frequency__c FROM Account where Ipas_Flag__c !=true LIMIT 1700';
        RestRequest req = RestContext.request; 
        RestResponse res = RestContext.response; 
        list<Account> acc_toUpdate = new list<Account>();
        list<Account> acc_tosend = new list<Account>();
           list<Account> accList = Database.query(query);         
        for(Account a: accList){
            a.Ipas_Flag__c = true;
            acc_toUpdate.add(a);
        } 
        update acc_toUpdate;
        for(Account ac: accList){
            if(ac.Name == NULL || ac.Name == '') ac.Name ='null';
            if(ac.Active__c == NULL || ac.Active__c == '') ac.Active__c ='null';
            if(ac.Address__c == NULL || ac.Address__c == '') ac.Address__c ='null';
            if(ac.Beat_Code__r.Name == NULL || ac.Beat_Code__r.Name == '') ac.Beat_Code__r.Name ='null';
            if(ac.Channel_type__c == NULL || ac.Channel_type__c == '') ac.Channel_type__c ='null';
            if(ac.Cluster_Code__r.Cluster_Code__c == NULL || ac.Cluster_Code__r.Cluster_Code__c == '') ac.Cluster_Code__r.Cluster_Code__c ='null';
            if(ac.Contract_type__c == NULL || ac.Contract_type__c == '') ac.Contract_type__c ='null';
            if(ac.Country_liquor_sold__c == NULL || ac.Country_liquor_sold__c == '') ac.Country_liquor_sold__c ='null';
            if(ac.District__c == NULL || ac.District__c == '') ac.District__c ='null';
            if(ac.Geo_Location__Latitude__s == NULL || ac.Geo_Location__Longitude__s == NULL ){
                ac.Geo_Location__Latitude__s = null;
                ac.Geo_Location__Longitude__s = null;
            }
            if(ac.Goegraphy_state__c == NULL || ac.Goegraphy_state__c == '') ac.Goegraphy_state__c ='null';
            if(ac.Group_Outlet__c == NULL || ac.Group_Outlet__c == '') ac.Group_Outlet__c ='null';
            if(ac.Group_Name__c == NULL || ac.Group_Name__c == '') ac.Group_Name__c ='null';
            //if(ac.Group_Lead_TL__c == NULL || ac.Group_Lead_TL__c == '') ac.Group_Lead_TL__c ='null';
            if(ac.Location_Type__c == NULL || ac.Location_Type__c == '') ac.Location_Type__c ='null';
            if(ac.Lower_Prestige__c == NULL || ac.Lower_Prestige__c == '') ac.Lower_Prestige__c ='null';
            if(ac.Luxury_Core__c == NULL || ac.Luxury_Core__c == '') ac.Luxury_Core__c ='null';
            if(ac.Market_Type__c == NULL || ac.Market_Type__c == '') ac.Market_Type__c ='null';
            if(ac.Micro_channel__c == NULL || ac.Micro_channel__c == '') ac.Micro_channel__c ='null';
            if(ac.Mid_Prestige__c == NULL || ac.Mid_Prestige__c == '') ac.Mid_Prestige__c ='null';
            if(ac.MSL_Town_Tier__c == NULL || ac.MSL_Town_Tier__c == '') ac.MSL_Town_Tier__c ='null';
            if(ac.O_L_Id__c == NULL || ac.O_L_Id__c == '') ac.O_L_Id__c ='null';
            if(ac.Outlet_Contact_Person__c == NULL || ac.Outlet_Contact_Person__c == '') ac.Outlet_Contact_Person__c ='null';
            if(ac.Outlet_License_Code__c == NULL || ac.Outlet_License_Code__c == '') ac.Outlet_License_Code__c ='null';
            if(ac.Outlet_License_Name__c == NULL || ac.Outlet_License_Name__c == '') ac.Outlet_License_Name__c ='null';
            if(ac.Outlet_Mobile__c == NULL || ac.Outlet_Mobile__c == '') ac.Outlet_Mobile__c ='null';
            if(ac.Overall_Segmentation__c == NULL || ac.Overall_Segmentation__c == '') ac.Overall_Segmentation__c ='null';
            if(ac.Perfect_Outlet__c == NULL || ac.Perfect_Outlet__c == '') ac.Perfect_Outlet__c ='null';
            if(ac.Pincode__c == NULL || ac.Pincode__c == '') ac.Pincode__c ='null';
            if(ac.Premium_Core__c == NULL || ac.Premium_Core__c == '') ac.Premium_Core__c ='null';
            if(ac.Primary_Occasion__c == NULL || ac.Primary_Occasion__c == '') ac.Primary_Occasion__c ='null';
            if(ac.Primary_Customer_Name__c == NULL || ac.Primary_Customer_Name__c == '') ac.Primary_Customer_Name__c =null;
            if(ac.Secondary_Occasion__c == NULL || ac.Secondary_Occasion__c == '') ac.Secondary_Occasion__c ='null';
            if(ac.TL_Code__r.Name == NULL || ac.TL_Code__r.Name == '') ac.TL_Code__r.Name ='null';  
            if(ac.Town__c == NULL || ac.Town__c == '') ac.Town__c ='null';  
            if(ac.Town_code__c == NULL || ac.Town_code__c == '') ac.Town_code__c ='null';  
            if(ac.TSE_Code__r.Name == NULL || ac.TSE_Code__r.Name == '') ac.TSE_Code__r.Name ='null';  
            if(ac.USL_Outlet_ID__c == NULL || ac.USL_Outlet_ID__c == '') ac.USL_Outlet_ID__c ='null';  
             if(ac.Upper_Prestige__c == NULL || ac.Upper_Prestige__c == '') {ac.Upper_Prestige__c ='null';}            
                   acc_tosend.add(ac);  
            }
        return acc_tosend;
        }       
    }
===============================================
i know this may happened becaused i used many if conditions in a for loop. please help me to reduce this.

Regards,
Ranga.V
Jay GuruswamyJay Guruswamy
Hi Ranga,

I do not see any reason why this should give CPU limit exceeded error.

Anyhow below are some of my thoughts to improve your code.

1. If the box you are building this code and testing is a sandbox, there could be some limitations. If so, you may try in some production instance.
2. You are setting the limit to 1700. This is not a big limit, Anyhow, please try by reducing this to 10, 20 etc and see on which number it fails.
3. You use two for loops that goes throught he same recordset. You may merge them into single loop.

Best Regards
Jay
ranga babu vangalapudi 2ranga babu vangalapudi 2
Thanks Jay for the prompt response.

As I already mentioned it is a Rest api & on a daily basis this API can be called up to 20 times by third party system.

We are getting this error when we are calling it more than 4 times, i mean calling for the 5th time we are getting this error.

Regards,
Ranga.V