+ Start a Discussion
Linda 98Linda 98 

Missing Argument,ID not specified in an update call-apex trigger help

Hello,

I am having a trigger on an Opportunity object(after update) which fires after the checkbox is selected and creates child objects.
I have this trigger to divide the total amount based on the number of days(start date to end date)

Everything works fine. Now I would like to update the child object records if the amount is changed and recalculate the amount and update the records.Debug logs shows the values correctly,but when i update list,i get error MISSING ARGUMENT ID
Below is my code.JUST THE UPDATE PART WHICH I AM DOING
 
trigger createprojects on Opportunity (after update) {
/****
other functions of trigger****/
    set<id> ids = new set<id>();  
               //Check if amount is changed
                if(oldOpp.Amount !=Opp.Amount__c && OPP.Amount__c != null)
                {
                    for(Opportunity op :trigger.new)
                    {
                        if (op.id != null) 
                        {
                            ids.add(op.id);

                        }
                    }
                    
                    for(Project__c plookup:[select id from Project__c where opportunity in :ids])
                    {
                        if(Opp.Start_Date__c != null && opp.End_date__c != null)
                        {
                        noOfMonths = Opp.Start_Date__c.monthsBetween(Opp.End_date__c); 
                        }
                        if(noOfMonths != Null & noOfMonths > 0)
                        {
                           Integer startMonth = Opp.Start_Date__c.month();
                           Date startDate = Opp.Start_Date__c;
                           Date endDate = OPP.Start_Date__c; 
                           for(Integer i = 0; noOfMonths >= i; i++)
                            {    
                        Project__c pro =new Project__c();
                            if(i > 0)
                            {
                            Date endD = endDate;
                            startDate = endD.addDays(1);
                            }
                            Date strD = startDate;

                            if(Opp.Amount !=null &&opp.Amount__c >0){
                            Integer dINM = date.daysInMonth(startDate.year(), (i < 0?(strD.month()+1):startDate.month()));

                            endDate = Date.newInstance(startDate.year(), startDate.month(), dINM);

                            Date eD= (i > 0)?(endDate > Opp.End_date__c?Opp.End_date__c:endDate):endDate;     

                            Integer noOfDays = startDate.daysBetween(ED)+1; 

                            pro.Name='Pro-'+startdate;
                            Pro.opporunity__c = Opp.id;
                            pro.Start_Date__c = startDate;
                            pro.End_Date__c = (i > 0)?(endDate > Opp.End_date__c?Opp.End_date__c:endDate):endDate;

                            pro.Opportunity = Pro.id;
                            pro.NumberofDays__c=noofdays;
                           pro.Plan_Amount__c=Opp.Amount/Opp.Project_Duration_CD__c*noofdays;
                            projectlistupdate.add(pro);
                            }
                        }          
                    }
               }  
              
    if(projectlistupdate.size() >0){
    update projectlistupdate;
    }
   }           
//    Insert projectlist;
    } 
}

 
Boss CoffeeBoss Coffee
Could the following line be causing the error? I don't see where the Project__c record is inserted to get its Id.
pro.Opportunity = Pro.id;
Linda 98Linda 98
Oh it's a typo error.Its Opp.id
Boss CoffeeBoss Coffee
Was that the cause? Or does the error still occur?
Linda 98Linda 98
Yes. I am getting missing argument ID error.
Boss CoffeeBoss Coffee
For the records in projectlistupdate, they haven't been inserted yet, right? It may be giving you that error because you are attempting to update records that do not have an Id.
update projectlistupdate;
Linda 98Linda 98
I am just updating a $ amount on parent record and updating it. I would like child records to recalculate the $ amount and update them(child's) so I am working on existing records which has Id
Boss CoffeeBoss Coffee
From the code provided, I only see new Project__c records being put into a list that is for updating. The update will fail if the records are missing their Ids.
for(Integer i = 0; noOfMonths >= i; i++)
    {    
        // COMMENT: New Project__c
        Project__c pro =new Project__c();
        if(i > 0)
        {
            Date endD = endDate;
            startDate = endD.addDays(1);
        }
        Date strD = startDate;
        
        if(Opp.Amount !=null &&opp.Amount__c >0){
            Integer dINM = date.daysInMonth(startDate.year(), (i < 0?(strD.month()+1):startDate.month()));
            
            endDate = Date.newInstance(startDate.year(), startDate.month(), dINM);
            
            Date eD= (i > 0)?(endDate > Opp.End_date__c?Opp.End_date__c:endDate):endDate;     
            
            Integer noOfDays = startDate.daysBetween(ED)+1; 
            
            // COMMENT: Assigning project__c fields
            pro.Name='Pro-'+startdate;
            Pro.opporunity__c = Opp.id;
            pro.Start_Date__c = startDate;
            pro.End_Date__c = (i > 0)?(endDate > Opp.End_date__c?Opp.End_date__c:endDate):endDate;
            
            pro.Opportunity = Pro.id;
            pro.NumberofDays__c=noofdays;
            pro.Plan_Amount__c=Opp.Amount/Opp.Project_Duration_CD__c*noofdays;

            // COMMENT: Adding project__c to list with no Id
            projectlistupdate.add(pro);
        }
    }