• ranga babu vangalapudi 2
  • NEWBIE
  • 40 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 5
    Replies
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
Following is my Before trigger:

trigger GroupClusterMap on Account (before insert, before update) {
    set<string> groupId = new set<string>();
    list<Account> listAccs= new list<Account>();
    list<Outlets_Groups_Primary_Customer__c> groupOutlets = new list<Outlets_Groups_Primary_Customer__c>();
    for(Account acc:trigger.new) {
        if(acc.Group_Name1__c!=NULL)
            groupId.add(acc.Group_Name1__c);
        }
    listAccs = [Select Id, Cluster_Code__c,Group_Name1__c,TL_Code__c from Account where Group_Name1__c IN: groupId limit 1];
    groupOutlets = [Select Id, Lead_TL__c,Name from Outlets_Groups_Primary_Customer__c where Id IN: groupId limit 1];
    if(trigger.isinsert) {
        for(Account aa:trigger.new) {
            for(Account dup:listAccs) {
                if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.Cluster_Code__c!=dup.Cluster_Code__c)){
                    aa.adderror('Group is mapped to a Cluster');
                if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.TL_Code__c!=dup.TL_Code__c)){
                    for(Outlets_Groups_Primary_Customer__c outgrps: groupOutlets){
                        if(outgrps.Lead_TL__c == NULL || outgrps.Lead_TL__c == '')
                            aa.adderror('Please do a Lead TL mapping in the Group');
                    }
                }                    
                }
                }
            }
        }
    if(trigger.isupdate) {
        for(Account aa:trigger.new){
            for(Account dup:listAccs) {                             
               // if ((aa.Group_Name1__c==dup.Group_Name1__c && aa.Group_Name1__c !=trigger.oldmap.get(aa.id).Group_Name1__c) && (aa.Cluster_Code__c!=dup.Cluster_Code__c && aa.Cluster_Code__c !=trigger.oldmap.get(aa.id).Cluster_Code__c))
                if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.Cluster_Code__c!=dup.Cluster_Code__c)){
                    aa.adderror('Group is mapped to a Cluster');
                if (aa.Group_Name1__c!=NULL && (aa.Group_Name1__c==dup.Group_Name1__c && aa.TL_Code__c!=dup.TL_Code__c)){
                    for(Outlets_Groups_Primary_Customer__c outgrps: groupOutlets){
                        if(outgrps.Lead_TL__c == NULL || outgrps.Lead_TL__c == '')
                            aa.adderror('Please do a Lead TL mapping in the Group');
                    }
                  }                    
                }
            } 
        }
    }
}
=========================================================
Following is the test class i had written for it - it only covers 57% code.

@IsTest
public class GroupClusterMapTest {   
    
    static testMethod void GroupClusterMapTest(){
        Id RecordTypeId = Schema.SObjectType.Masters__c.getRecordTypeInfosByName().get('Cluster').getRecordTypeId();
        Masters__c master = New Masters__c();
        master.Cluster_Name__c='RPCSCL004';
        master.Geographic_State__c='ANDHRA PRADESH';
        master.RecordTypeid=RecordTypeId;
        insert master;        
        
        TL_Master__c tl1= new TL_Master__c();
        tl1.Name='APTL004';
        tl1.TL_Territory_Name__c='NORTH COASTAL';
        tl1.Cluster__c=master.id;
        insert tl1;
        
        TSE_Master__c oldTse2 = new TSE_Master__c();
        oldTse2.Name = 'APTS033';
        oldTse2.TSE_Territory_Name__c = 'VISAKHAPATNAM - II AREA-I';
        oldTse2.TL_Code__c=tl1.id;
        insert oldTse2;
        
        Beat_TSE_Mapping__c NewBte1 = new Beat_TSE_Mapping__c();
        NewBte1.Beat_Name__c = 'BEAT 03';
        NewBte1.Name = 'ABBT1130';
        NewBte1.TSE_Code__c = oldTse2.id;
        insert NewBte1;
        
        Outlets_Groups_Primary_Customer__c newGrp = new Outlets_Groups_Primary_Customer__c();
        newGrp.Name = 'mygroup';
        newGrp.Type__c = 'Groups';
        newGrp.State_Group_PC__c = 'ANDHRA PRADESH';
        newGrp.Lead_TL__c = tl1.Id;
        insert newGrp;
        
        Account OldAcc = new Account();        
        OldAcc.Beat_Code__c = NewBte1.Id;
        //OldAcc.Beat_Name__c = oldBte.Beat_Name__c ;
        OldAcc.TSE_Code__c = oldTse2.id;
        //OldAcc.TSE_ID__c = oldBte.TSE_ID__c;
        //OldAcc.TSE_Name__c = oldBte.TSE_Name__c;
        OldAcc.District__c = 'Test District';
        OldAcc.Goegraphy_state__c = 'ANDHRA PRADESH';
        OldAcc.Town__c = 'Test Town';
        OldAcc.Name = 'Test name';
        OldAcc.Market_Type__c = 'Corporation';
        OldAcc.MSL_Town_Tier__c= 'BTNTest';
        OldAcc.Cluster_Code__c=master.Id;
        OldAcc.TSE_Code__c=oldTse2.id;
        OldAcc.Outlet_Type__c='D';
        OldAcc.Beat_Code__c= NewBte1.Id;
        OldAcc.TL_Code__c = tl1.Id;
        OldAcc.Address__c = 'testadd';
        OldAcc.Stock_List__c = 'Tier 1';
        OldAcc.Channel_type__c = 'PrOP';
        OldAcc.Micro_channel__c = 'Prestige Bar';
        OldAcc.Perfect_Outlet__c = 'Yes';
        OldAcc.Group_Outlet__c='No';
        oldAcc.Group_Name1__c = newGrp.Id;
        OldAcc.Contract_type__c = 'Open';
        insert OldAcc;
        
         Account NewAcc = new Account();
        NewAcc.id = OldAcc.id;
        NewAcc.Beat_Code__c = NewBte1.ID;
        //  NewAcc.Beat_Name__c = NewBte.Beat_Name__c ; 
        //NewAcc.TSE_Code__c = oldTse3.id;
        // NewAcc.TSE_ID__c = NewBte.TSE_ID__c;
        // NewAcc.TSE_Name__c = NewBte.TSE_Name__c;
        NewAcc.District__c = 'Test District1';
        NewAcc.Town__c = 'Test Town1';
        NewAcc.Name = 'Test name1';
        oldAcc.Group_Name1__c = newGrp.Id;
        update NewAcc;
    }

}

Your help is greatly appreciated...Thank you !!!
 
This is my Schedule class:

global class OutletExporter implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        String today = Datetime.now().AddDays(-1).format('dd-MM-yyyy');
        List<Account> accList = [Select RPC__c,Cluster__c,TL_Territory_Name__c,Team_Lead_Employee_ID__c,TSE_Name__c,TSE_ID__c,TSE_Code__r.Outlet_Count__c  from Account where CreatedDate =Yesterday LIMIT 50000];
        //List<Account> latestModiaccList = [Select Id,Name, AccountNumber, Industry, CreatedDate,LastModifiedDate,Phone from Account where LastModifiedDate=TODAY AND CreatedDate !=TODAY];
        
        String header = 'Accounts inserted Today \r\n\n'+ 'RPC, Cluster, TLTerritory Name, TL EMP ID,TSE Territory Name,TSE EMP ID,Outlet Count \r\n';
        //String header1 ='\r\n\bAccounts Modified Today\b\r\n'+ '\bId, Name, Account Number, Industry,Phone\b\r\n';
        String generatedCSVFile = header+'';
        List<String> queryFields = new List<String>{'RPC__c','Cluster__c','TL_Territory_Name__c','Team_Lead_Employee_ID__c','TSE_Name__c','TSE_ID__c','Outlet Count'};
        String fileRow = '';
        system.debug('My Account List:'+accList);
        for(Account a: accList){
        fileRow = '';
        fileRow = fileRow +','+ a.RPC__c;
        fileRow = fileRow +','+ a.Cluster__c;
        fileRow = fileRow +','+ a.TL_Territory_Name__c;
        fileRow = fileRow +','+ a.Team_Lead_Employee_ID__c;
        fileRow = fileRow +','+ a.TSE_Name__c;
        fileRow = fileRow +','+ a.TSE_ID__c;
           fileRow = fileRow +','+ a.TSE_Code__r.Outlet_Count__c;
        fileRow = fileRow.replaceFirst(',','');
        generatedCSVFile = generatedCSVFile + fileRow + '\n';
        }
        Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
        Blob csvBlob = blob.valueOf(generatedCSVFile);
        String csvName = 'Outlets inserted and modified on '+today+'.csv';
        csvAttachment.setFileName(csvName);
        csvAttachment.setBody(csvBlob);
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{'ranga.vangalapudi@diageo.com','jayalakshmi.t@ivymobility.com','jennifer.b@ivymobility.com','Pradeep.Shiggavi@diageo.com','Nithyananda.Alva@diageo.com','Rajat.Yadav@diageo.com'};
        String subject = 'Outlets inserted and modified on '+today+'';
        email.setSubject(subject);
        email.setToAddresses(toAddresses);
        email.setPlainTextBody('Outlets inserted and modified on '+today+'');
        email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttachment});
        Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
       }
}

This is the my test class
@isTest
public class OutletExporterTest {
     
    static testmethod void testoutlets(){        
       Id RecordTypeId = Schema.SObjectType.Masters__c.getRecordTypeInfosByName().get('Cluster').getRecordTypeId();
        Masters__c master = New Masters__c();
        master.Cluster_Name__c='RPCSCL004';
        master.RecordTypeid=RecordTypeId;
        insert master;
        TL_Master__c tl= new TL_Master__c();
        tl.Name='APTL001';
        tl.TL_Territory_Name__c='CENTRAL COASTAL';
        tl.Cluster__c=master.id;
        insert tl;
        
        TSE_Master__c oldTse1 = new TSE_Master__c();
        oldTse1.Name = 'APTS001';
        oldTse1.TSE_Territory_Name__c = 'AMALAPURAM';
        oldTse1.TL_Code__c=tl.id;
        insert oldTse1;
        
        Beat_TSE_Mapping__c NewBte = new Beat_TSE_Mapping__c();
        NewBte.Beat_Name__c = 'Beat_1';
        NewBte.Name = 'KABT0564';
        NewBte.TSE_Code__c = oldTse1.id;
        insert NewBte;
        
        
        Account acc = new Account();
        acc.name = 'test name';
        //acc.RPC__c = 'RPC-N' ;
        acc.Active__c = 'Waiting Approval';
        acc.Pincode__c = '560068';
        // acc.Beat_Name__c = 'testBeat';
        acc.Town__c = 'testTown';
        acc.Outlet_License_Name__c = 'testOl';
        acc.Address__c = 'testadd';
        acc.Stock_List__c = 'Tier 1';
        acc.Channel_type__c = 'Off';
        acc.Micro_channel__c = 'Browsable PTO';
        acc.Perfect_Outlet__c = 'Yes';
        acc.Contract_type__c = 'Open';
        //acc.TL_Territory_Name__c = 'Gurgaon 3';
        //acc.RH_Territory_Name__c = 'Haryana 3';
        //acc.Cluster__c = 'Haryana 1' ;
        acc.District__c = 'Gurgaon 3';
        acc.Goegraphy_state__c = 'Haryana';
        acc.Market_Type__c='Bond Market';
        acc.MSL_Town_Tier__c='Uncovered';
        acc.Beat_Code__c= NewBte.Id;
        acc.Cluster_Code__c=master.Id;
        acc.TSE_Code__c=oldTse1.id;
        acc.TL_Code__c=tl.Id;
        acc.Outlet_Type__c='D';
        acc.Group_Outlet__c='No';
        insert acc ; 
        Account NewAcc = new Account();
        NewAcc.id = acc.id;
        NewAcc.Beat_Code__c = NewBte.ID;
        //  NewAcc.Beat_Name__c = NewBte.Beat_Name__c ; 
        //NewAcc.TSE_Code__c = oldTse3.id;
        // NewAcc.TSE_ID__c = NewBte.TSE_ID__c;
        // NewAcc.TSE_Name__c = NewBte.TSE_Name__c;
        NewAcc.District__c = 'Test District1';
        NewAcc.Town__c = 'Test Town1';
        NewAcc.Name = 'Test name1';
        update NewAcc;
            Test.startTest();
            OutletExporter  objTest = new OutletExporter();
            String sch = '0 0 7 ? * * *';
            system.schedule('Test Schdule', sch, objTest); 
            Test.stopTest();
    }
}

Please help me on this?
Hello Everyone,

I want to know the possible ways through which we can fetch more than 50k/1million records through apex class in salesforce.

Please respond.

Thanks in advance.

Rregards
Aakanksha Singh