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
Kumar Saurav 45Kumar Saurav 45 

Unable to get rid of Too Many Soql code with code working but it's not bulky as DML operation are done inside for loop

@RestResource(urlMapping = '/OrdermasterUpdate1/*')
global with sharing class OrdermasterUpdate1 {
    string message;
    boolean status;
    id recordID;
    @httpPost
    global static ordermaster__c dopost(){
        OrdermasterUpdate1 response = new OrdermasterUpdate1();
        id productid = null;
        ordermaster__c s3 = new ordermaster__c();
        ordermaster__c s2 = new ordermaster__c();
      RestRequest req = RestContext.request;
      RestResponse res = Restcontext.response;
        Integer i = 0;
               i++;
            i++;
            i++;
            i++;   
            i++;
            i++;
            i++;
            i++;
            i++;
          
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
        string requestBody = restContext.request.requestBody.toString();
        list<ordermasterwrapper> u1 = (list<ordermasterwrapper>)JSON.deserializeStrict(requestBody, list<ordermasterwrapper>.class); 
        for(ordermasterwrapper oi : u1){
            List<ordermaster__c> s = new List<ordermaster__c>();
            s =[select problem_box_number__c,selected_for_process_time__c,selected_for_process_by__c,selected_for_process_box__c,processing_box_number__c,retail_approved_by__c,
        receiveddate__c,transction_id__c,id__c,website_id__c,company_id__c,custom_order_id__c,channel_order_id__c,customer_id__c
        ,customer_name__c,payment_method_name__c,payment_date__c,billing_name__c,billing_company__c,billing_email_address__c,billing_street_address__c,billing_city__c,billing_postcode__c,billing_state__c
        ,billing_country__c,billing_phone__c,billing_fax__c,delivery_name__c,delivery_email_address__c,delivery_street_address__c,delivery_city__c,delivery_postcode__c,delivery_state__c,
         delivery_country__c,delivery_phone__c,shippingdate__c,delivery_fax__c,net_amount__c,shipping_cost__c,paid_amount__c,gross_discount_amount__c,tax_amount__c,order_status__c,currency_code__c,ip_address__c
        ,created_date__c,bonusdate__c,is_mail__c,response_msg__c,send_notes__c,received_amount__c,assign_to__c,assign_wh__c,pay_txnTranID__c,ordertype__c,additionalshiipingtax_amount__c,member_discount__c
        ,cart_discount__c,delivery_father_name__c,delivery_grndfather_name__c,refund_stock__c,doll_to_iqd__c,eqipped_date__c,cancel_date__c,ready_for_shipment__c,is_replacement__c
        ,is_acknowledged__c,not_received_note__c,gen_note__c,gen_note_admin_uid__c,cancel_note__c,cancel_note_admin_uid__c,not_receive_date__c,replace_date__c,ready_to_ship_uid__c,ship_uid__c
        ,receive_uid__c,not_receive_uid__c,replacement_uid__c,is_problem__c,problemdate__c,is_returned__c,is_confirmed__c,returndate__c,replaced_status__c,replacementcount__c,probflag__c
        ,coupon_discount__c,coupon_id__c,shipment_waiting__c,waiting_invoice__c,waiting_reason__c,delivery_user__c,waiting_date__c,notification_send__c,device_token__c,device_type__c
        ,replaced_cancel_note__c,replaced_cancel_note_admin_uid__c,replaced_cancel_date__c,admin_viewed_waiting__c,assembly_confirm__c,remove_from_lottery__c
        ,equipped_cancelled__c,old_order_id__c,old_ship_cost__c,old_gt__c,new_order_id__c,reviews_given__c,points_received__c,points_conversion_value__c,review_points_percentage__c,retail_price__c
        ,retail_installation_price__c,retail_approve_date__c,order_retail_ratio__c,total_retail_emi__c,retail_emi_paid__c,retail_emi_deadline__c,first_retail_payment_date__c
        ,last_retail_payment_date__c,retail_selary__c,down_payment__c,shipping_code__c,shipping_addresses_id__c,neighborhood__c,delevery_name__c,delevery_phonenumber__c,delevery_reason__c
        ,refund_shipping_cost_onreturn__c,delevery_ammont_usd__c,delevery_ammont_iqd__c,received_latitude__c,received_longitude__c from ordermaster__c where id__c =: oi.id];
        if(s.size()>0){ 
                 productid = s.get(0).id;
            s.get(0).transction_id__c = oi.transction_id;
            s.get(0).id__c = oi.id;          
            s.get(0).customer_id__c = oi.customer_id;
            s.get(0).customer_name__c = oi.customer_name;
            s.get(0).payment_method_name__c = oi.payment_method_name;
            s.get(0).payment_date__c = oi.payment_date;
            s.get(0).billing_name__c = oi.billing_name;
            s.get(0).billing_company__c = oi.billing_company;
            s.get(0).billing_email_address__c = oi.billing_email_address;
            s.get(0).billing_street_address__c = oi.billing_street_address;
            s.get(0).billing_city__c = oi.billing_city;
            s.get(0).billing_postcode__c = oi.billing_postcode;
            s.get(0).billing_state__c = oi.billing_state;
            s.get(0).billing_country__c = oi.billing_country;
            s.get(0).billing_phone__c = oi.billing_phone;
            s.get(0).billing_fax__c = oi.billing_fax;
            s.get(0).delivery_name__c = oi.delivery_name;
            s.get(0).delivery_email_address__c = oi.delivery_email_address;
            s.get(0).delivery_street_address__c = oi.delivery_street_address;
            s.get(0).delivery_city__c = oi.delivery_city;
            s.get(0).delivery_postcode__c = oi.delivery_postcode;
            s.get(0).delivery_state__c = oi.delivery_state;
               s.get(0).delivery_country__c = oi.delivery_country;
            s.get(0).delivery_phone__c = oi.delivery_phone;
            s.get(0).shippingdate__c = oi.shippingdate;
            s.get(0).delivery_fax__c = oi.delivery_fax;
            s.get(0).net_amount__c = oi.net_amount;
            s.get(0).shipping_cost__c = oi.shipping_cost;
            s.get(0).paid_amount__c = oi.paid_amount;
            s.get(0).gross_discount_amount__c = oi.gross_discount_amount;
            s.get(0).tax_amount__c = oi.tax_amount;
            s.get(0).order_status__c = oi.order_status;
            s.get(0).currency_code__c = oi.currency_code;
            s.get(0).ip_address__c = oi.ip_address;
            s.get(0).created_date__c = oi.created_date;
            s.get(0).problem_box_number__c = oi.problem_box_number;
                try{
                update s.get(0);
                response.status=true;
                response.message='Product Updated Successfully';
            }catch(exception e){
                response.status=false; 
                response.message = 'Failed';
            }
            
            }else{
                 ordermaster__c o1 = new ordermaster__c();
            o1.transction_id__c = oi.transction_id;
            o1.id__c = oi.id;
            o1.website_id__c = oi.website_id;
            o1.company_id__c = oi.company_id;
            o1.custom_order_id__c = oi.custom_order_id;
            o1.channel_order_id__c = oi.channel_order_id;
            o1.customer_id__c = oi.customer_id;
            o1.delivery_name__c = oi.delivery_name;
            o1.delivery_email_address__c = oi.delivery_email_address;
            o1.delivery_street_address__c = oi.delivery_street_address;
            o1.delivery_city__c = oi.delivery_city;
            o1.delivery_postcode__c = oi.delivery_postcode;
            o1.delivery_state__c = oi.delivery_state;       
            o1.gross_discount_amount__c = oi.gross_discount_amount;   
            o1.response_msg__c = oi.response_msg;
            o1.send_notes__c = oi.send_notes;
            o1.received_amount__c = oi.received_amount;
            o1.assign_to__c = oi.assign_to;
            o1.assign_wh__c = oi.assign_wh;
            o1.pay_txnTranID__c = oi.pay_txnTranID;
            o1.ordertype__c = oi.ordertype;
            o1.additionalshiipingtax_amount__c = oi.additionalshiipingtax_amount;
            o1.member_discount__c = oi.member_discount;
            o1.cart_discount__c = oi.cart_discount;
            o1.delivery_father_name__c = oi.delivery_father_name;
            o1.delivery_grndfather_name__c = oi.delivery_grndfather_name;
            o1.refund_stock__c = oi.refund_stock;
            o1.doll_to_iqd__c = oi.doll_to_iqd;
            o1.eqipped_date__c = oi.eqipped_date;
            o1.cancel_date__c = oi.cancel_date;
            o1.ready_for_shipment__c = oi.ready_for_shipment;
            o1.is_replacement__c = oi.is_replacement;
            o1.is_acknowledged__c = oi.is_acknowledged;
            o1.replaced_cancel_note__c = oi.replaced_cancel_note;
            o1.replaced_cancel_note_admin_uid__c = oi.replaced_cancel_note_admin_uid;
              o1.replaced_cancel_date__c = oi.replaced_cancel_date;
            o1.admin_viewed_waiting__c = oi.admin_viewed_waiting;
            o1.assembly_confirm__c = oi.assembly_confirm;
            o1.remove_from_lottery__c = oi.remove_from_lottery;
                o1.selected_for_process_time__c = oi.selected_for_process_time;
                o1.problem_box_number__c = oi.problem_box_number;
            try{
                insert o1;
                response.status=true;
                response.message='Product Updated Successfully';
            }catch(exception e){
                response.status=false; 
                response.message = 'Failed';
            }
   
        }
            }
         return s3;
        
    }
    global class ordermasterwrapper{
        public double problem_box_number{get;set;}
        public datetime selected_for_process_time{get;set;}
        public integer selected_for_process_by{get;set;}
        public integer selected_for_process_box{get;set;}
        public double processing_box_number{get;set;}
        public double retail_approved_by{get;set;}
        public datetime receiveddate{get;set;}
        public string transction_id{get;set;}
        public double id{get;set;}
        public double website_id{get;set;}
        public double company_id{get;set;}
        public string custom_order_id{get;set;}
        public string channel_order_id{get;set;}
        public integer customer_id{get;set;}
        public string customer_name{get;set;}
        public string payment_method_name{get;set;}
        public datetime payment_date{get;set;}
        public string billing_name{get;set;}
        public string billing_company{get;set;}
        public string billing_email_address{get;set;}
        public string billing_street_address{get;set;}
        public double billing_city{get;set;}
        public string billing_postcode{get;set;}
        public string billing_state{get;set;}
        public integer billing_country{get;set;}
        public string billing_phone{get;set;}
        public string billing_fax{get;set;}
        public string delivery_name{get;set;}
        public string delivery_email_address{get;set;}
        public string delivery_street_address{get;set;}
        public integer delivery_city{get;set;}
        public string delivery_postcode{get;set;}
        public string delivery_state{get;set;}
        public integer delivery_country{get;set;}
        public string delivery_phone{get;set;}
        public datetime shippingdate{get;set;}
        public string delivery_fax{get;set;}
        public double net_amount{get;set;}
        public double shipping_cost{get;set;}
        public double paid_amount{get;set;}
        public double gross_discount_amount{get;set;}
        public double tax_amount{get;set;}
        public integer order_status{get;set;}
        public string currency_code{get;set;}
        public string ip_address{get;set;}
        public datetime created_date{get;set;}
        public datetime bonusdate{get;set;}
        public double is_mail{get;set;}
        public string response_msg{get;set;}
        public string send_notes{get;set;}
        public decimal received_amount{get;set;}
        public integer assign_to{get;set;}
        public integer assign_wh{get;set;}
        public string pay_txnTranID{get;set;}
        public string ordertype{get;set;}
        public decimal additionalshiipingtax_amount{get;set;}
        public decimal member_discount{get;set;}
        public decimal cart_discount{get;set;}
        public string delivery_father_name{get;set;}
        public string delivery_grndfather_name{get;set;}
        public integer refund_stock{get;set;}
        public decimal doll_to_iqd{get;set;}
        public datetime eqipped_date{get;set;}
        public datetime cancel_date{get;set;}
        public datetime ready_for_shipment{get;set;}
        public integer is_replacement{get;set;}
        public integer is_acknowledged{get;set;}
        public string not_received_note{get;set;}
        public string gen_note{get;set;}
        public double gen_note_admin_uid{get;set;}
        public string cancel_note{get;set;}
        public double cancel_note_admin_uid{get;set;}
        public datetime not_receive_date{get;set;}
        public datetime replace_date{get;set;}
        public double ready_to_ship_uid{get;set;}
        public double ship_uid{get;set;}
        public double receive_uid{get;set;}
        public double not_receive_uid{get;set;}
        public double replacement_uid{get;set;}
        public integer is_problem{get;set;}
        public datetime problemdate{get;set;}
        public integer is_returned{get;set;}
        public integer is_confirmed{get;set;}
        public datetime returndate{get;set;}
        public integer replaced_status{get;set;}
        public integer replacementcount{get;set;}
        public integer probflag{get;set;}
        public decimal coupon_discount{get;set;}
        public double coupon_id{get;set;}
        public integer shipment_waiting{get;set;}
        public double waiting_invoice{get;set;}
        public string waiting_reason{get;set;}
        public double delivery_user{get;set;}
        public datetime waiting_date{get;set;}
        public integer notification_send{get;set;}
        public string device_token{get;set;}
        public string device_type{get;set;}
        public string replaced_cancel_note{get;set;}
        public double replaced_cancel_note_admin_uid{get;set;}
        public datetime replaced_cancel_date{get;set;}
        public integer admin_viewed_waiting{get;set;}
        public integer assembly_confirm{get;set;}
        public integer remove_from_lottery{get;set;}
        public integer equipped_cancelled{get;set;}
        public integer old_order_id{get;set;}
        public decimal old_ship_cost{get;set;}
        public decimal old_gt{get;set;}
        public double new_order_id{get;set;}
        public integer reviews_given{get;set;}
        public integer points_received{get;set;}
        public integer points_conversion_value{get;set;}
        public integer review_points_percentage{get;set;}
        public decimal retail_price{get;set;}
        public decimal retail_installation_price{get;set;}
        public datetime retail_approve_date{get;set;}
        public integer order_retail_ratio{get;set;}
        public integer total_retail_emi{get;set;}
        public integer retail_emi_paid{get;set;}
        public datetime retail_emi_deadline{get;set;}
        public datetime first_retail_payment_date{get;set;}
        public datetime last_retail_payment_date{get;set;}
        public string retail_selary{get;set;}    
        public decimal down_payment{get;set;}
        public string shipping_code{get;set;}
        public integer shipping_addresses_id{get;set;}
        public integer neighborhood{get;set;}
        public string delevery_name{get;set;}
        public string delevery_phonenumber{get;set;}
        public string delevery_reason{get;set;}
        public integer refund_shipping_cost_onreturn{get;set;}
        public integer delevery_ammont_usd{get;set;}
        public integer delevery_ammont_iqd{get;set;}
        public string received_latitude{get;set;}
        public string received_longitude{get;set;}
    }
}

 
Best Answer chosen by Kumar Saurav 45
OFröhlichOFröhlich

Hi,

1. add the oi.ids to a list, do the select against this list outside the loop into a map with oi.id as key, use the map functionalities in the loop
result: only one select  and 2 DML statements
2. add the inserts and the updates to new lists and update / insert the list outside the loop.

If this helps, please mark as Best Answer to help others too.

All Answers

OFröhlichOFröhlich

Hi,

1. add the oi.ids to a list, do the select against this list outside the loop into a map with oi.id as key, use the map functionalities in the loop
result: only one select  and 2 DML statements
2. add the inserts and the updates to new lists and update / insert the list outside the loop.

If this helps, please mark as Best Answer to help others too.

This was selected as the best answer
Kumar Saurav 45Kumar Saurav 45
Thanks for replying