You need to sign in to do that
Don't have an account?
HTANIRS
schedule apex could not able insert record
Hi,
I have created a schedule class to RUN every 1 hour but It is not checking the newly created record and inserting. Kindly review my code and let me know what changes need to be done.
My Requirement:
I want to write a schedule class on custom object to check newly created custom object record is created in Opportunity Line Items.
I have a field common to find custom obj and opportunity which is used to find opplineitems.
Need assistance to complete this code.
Thanks.
I have created a schedule class to RUN every 1 hour but It is not checking the newly created record and inserting. Kindly review my code and let me know what changes need to be done.
My Requirement:
I want to write a schedule class on custom object to check newly created custom object record is created in Opportunity Line Items.
I have a field common to find custom obj and opportunity which is used to find opplineitems.
global class orderItems Implements Schedulable { global void execute(SchedulableContext sc) { insertOrder(); } public void insertOrder() { List<OrderItems__c> lstOpty = [SELECT Id, Name, OrderId__c, Due_Date__c, Name__c, Discount_Amount__c, Price__c, Product_Id__c, Product_Type__c, Qty_Backordered__c, Qty_Canceled__c, Qty_Invoiced__c, Qty_Ordered__c, Qty_Refunded__c, Qty_Returned__c, Qty_Shipped__c, Sku__c FROM OrderItems__c WHERE CreatedDate > TODAY]; Set<Decimal> orderIds = new Set<Decimal>(); for(OrderItems__c oi : lstOpty) { orderIds.add(oi.OrderId__c); System.debug('--- Getting Custom Obj Id: ---' + orderIds); } Map<Decimal, List<Opportunity>> oppMap = new Map<Decimal, List<Opportunity>>(); if(!orderIds.isEmpty()){ System.debug('--- Checking Order ID: ---'); for(Opportunity opp : [SELECT Id, Name, OrderItem__c FROM Opportunity WHERE OrderItem__c IN: orderIds]) { if(!oppMap.containsKey(opp.OrderItem__c)) { oppMap.put(opp.OrderItem__c, new List<Opportunity> {opp}); System.debug('--- Opportunity Map ---' + oppMap); } } } List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1]; List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); for(OrderItems__c oi : lstOpty){ System.debug('--- Order Items: ---' + oi); if(oppMap.containsKey(oi.OrderId__c)){ for(Opportunity opp : oppMap.get(oi.OrderId__c)) { System.debug('--- Inside Opportunity ---'); OpportunityLineItem oli = new OpportunityLineItem(); oli.OpportunityId = opp.Id; oli.PricebookEntryId = priceBookList[0].Id; oli.Quantity = oi.Qty_Ordered__c; oli.TotalPrice = oi.Price__c; oli.item_id__c = oi.Name; oli.Name__c = oi.Name__c; oli.product_id__c = oi.product_id__c; oli.Due_Date__c = oi.Due_Date__c; oli.product_type__c = oi.product_type__c; oli.qty_backordered__c = oi.qty_backordered__c; oli.qty_canceled__c = oi.qty_canceled__c; oli.qty_invoiced__c = oi.qty_invoiced__c; oli.qty_ordered__c = oi.qty_ordered__c; oli.qty_refunded__c = oi.qty_refunded__c; oli.qty_returned__c = oi.qty_returned__c; oli.qty_shipped__c = oi.qty_shipped__c; oli.Sku__c = oi.Sku__c; oliList.add(oli); System.debug('--- Inside Opportunity' + opp); System.debug('--- Oli List: ---'); } } } try { if(oliList.size()>0) { insert oliList; System.debug('--- Inserted Opp Line Items: ---'); System.debug('--- Inserted Opp Line Items: ---' + olilist.size()); } } catch(Exception e){ System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage()); } } }
Need assistance to complete this code.
Thanks.
SOQL OrderItems__c(Line number 09) will not able to fetch any item because of where clause. You can modify query where clause as below :
Your schedulable job running every hour so above where clause will fetch only those records which are created in 1 hour.
Hit Kudos if this solve you problem and if this is what you are looking for then please mark it as a solution for other benefits.
Thanks
Dheeraj Kumar
All Answers
SOQL OrderItems__c(Line number 09) will not able to fetch any item because of where clause. You can modify query where clause as below :
Your schedulable job running every hour so above where clause will fetch only those records which are created in 1 hour.
Hit Kudos if this solve you problem and if this is what you are looking for then please mark it as a solution for other benefits.
Thanks
Dheeraj Kumar
Thanks for your help. It is working.