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
Muni uMuni u 

when i am trying to insert a record from custom object into standard opportunityLineItemSchedule object by firing a trigger

I am getting the below error, when i am trying to insert a record from custom object into standard opportunityLineItemSchedule object by firing a trigger

Exception Mesage:Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: Type, Quantity, unknown (invalid quantity/revenue for given type): [Type, Quantity, unknown]
NagendraNagendra (Salesforce Developers) 
Hi Muni,

Sorry for this issue you are facing.

In the Apex API docs look up OpportunityLineItemSchedule, there are some things you need to take into consideration when you create these objects in code.
 
Also, you need to set the quantity.
 
On another note please look into bulking your trigger. 

If you can post the complete code snippet we can look into it and can help you accordingly in troubleshooting the issue.

Thanks,
Nagendra
Muni uMuni u
Hii Nagendra ,
Thank you for responding.I have placed the code below.


---> This is trigger

trigger optyProductSchedule on Opty_Product_Schedule__c (before insert,after insert,after update,after delete) 
{
    
    //optyProductScheduleHandler handler=new optyProductScheduleHandler();
    if(trigger.isInsert && trigger.isBefore)
    {
        optyProductScheduleHandler.insertRecord(Trigger.new);
    }
    
    if(trigger.isInsert && trigger.isAfter)
    {
        List<Id> ScheduleIds = new List<Id>(); 
        for(Opty_Product_Schedule__c ops:trigger.new)
        {
            ScheduleIds.add(ops.Id);
        }
        optyProductScheduleHandler.insertOnStandardOpportunityLineItemSchedule(ScheduleIds);      
    }
}


---> Its handler class


/**
Author             : U.Muneeswarrao
Description     : Handler class for  optyProductSchedule trigger.
Created Date     : 3/12/2018
Last modified     : 4/12/2018
**/

public with sharing class optyProductScheduleHandler 
{
    
    public static void insertRecord(List<Opty_Product_Schedule__c> schedules)
    {
        if(AvoidRecursion.isFirstRun())
        {
            system.debug('i am inside optyProductScheduleHandler');
            try
            {
                system.debug('inside Try block');              
                List<Opty_Product_Schedule__c> lstOps= new List<Opty_Product_Schedule__c>();
                
                Set<Id>OptProduct = new set<Id>();    //using this Set ids we are deleting custom OpportunityLineItemSchedule records. 
                for(Opty_Product_Schedule__c o:schedules)
                {
                    system.debug('o.Quantity__c-->'+o.Quantity__c+'---------o.Number_of_Installments__c-->'+o.Number_of_Installments__c);
                    integer quantity = (Integer)o.Quantity__c;
                    integer revenue = (Integer)o.Revenue__c;  //
                    OptProduct.add(o.Opty_Product__c);
                    if(o.Number_of_Installments__c!=null)
                    {
                        o.Quantity__c = o.Quantity__c/o.Number_of_Installments__c;
                        o.Revenue__c=o.Revenue__c/o.Number_of_Installments__c;      //
                        
                        for(integer i=0;i<o.Number_of_Installments__c-1;i++)
                        {       
                            Opty_Product_Schedule__c ops=new Opty_Product_Schedule__c();
                            ops.Quantity__c = quantity/o.Number_of_Installments__c;
                            ops.Revenue__c= revenue/o.Number_of_Installments__c;    //    
                            ops.Start_Date__c=o.Start_Date__c.addDays(i+1);
                            ops.Opty_Product__c = o.Opty_Product__c;
                            system.debug('ops'+ops);                    
                            lstOps.add(ops);
                        }  
                    }
                }
                Set<Id> LineItems = new Set<Id>();    //using this Set ids we are deleting standard OpportunityLineItemSchedule records.
                for(Opty_Product__C op:[select OppLineItemId__c from Opty_Product__c where id=:OptProduct])
                {
                    LineItems.add(op.OppLineItemId__c);
                }
                delete [select id from OpportunityLineItemSchedule where OpportunityLineItemId=:LineItems];
                delete [select id from Opty_Product_Schedule__c where Opty_Product__c=:OptProduct];
                
                insert lstOps;
                system.debug('lstOps'+lstOps);          
            }
            catch(Exception e)
            {
                System.debug('Exception Line number:'+e.getLineNumber());
                System.debug('Exception Mesage :'+e.getMessage());
            }
        }
    }   
    
    public static void insertOnStandardOpportunityLineItemSchedule(List<Id> schedules)
    {
        try
        {                     
            
            List<Opty_Product_Schedule__c> ops=[select id,Type__c,Start_Date__c,Quantity__c,Number_of_Installments__c,Revenue__c,Opty_Product__c, Opty_Product__r.product2__c,Opty_Product__r.OppLineItemId__c from Opty_Product_Schedule__c where Id IN: schedules];           
            system.debug('ops.Quantity__c-->'+ops[0].Quantity__c+'---------ops.Number_of_Installments__c-->'+ops[0].Number_of_Installments__c);
            system.debug('ops====>'+ops.size());
            set<Id> setId=new set<Id>(); 
            List<OpportunityLineItemSchedule> lstOplis=new List<OpportunityLineItemSchedule>(); 
            
            for(Opty_Product_Schedule__c o:ops)
            {
                system.debug('=======>> Record is :'+ops);
                setId.add(o.Opty_Product__r.OppLineItemId__c);             
                OpportunityLineItemSchedule olis=new OpportunityLineItemSchedule();
                olis.OpportunityLineItemId=o.Opty_Product__r.OppLineItemId__c;                    
                olis.Type=o.Type__c;
                olis.ScheduleDate=o.Start_Date__c;            
                system.debug('=======Qty===>>>'+o.Quantity__c);
                if(o.Quantity__c != null){
                olis.Quantity=o.Quantity__c; 
                }
                system.debug('======Rev====>>>'+o.Revenue__c);
                if(o.Revenue__c != null){
                olis.Revenue=o.Revenue__c;
                }//
                lstOplis.add(olis);
            }
            system.debug('====> lstOplis-->'+lstOplis.size());           
            insert lstOplis;            
            system.debug('====> lstOplis'+lstOplis);
        }
        catch(Exception e)
        {
            System.debug('Exception Line number:'+e.getLineNumber());
            System.debug('Exception Mesage :'+e.getMessage());
        }        
    }    
}