+ Start a Discussion
lizmijohnlizmijohn 

Need help on Apex trigger and custom button click

Hi,

 

I have 3 objects:

Analysis (Parent of Project)

             Project (Parent of Project Analysis)

                        Project Analysis

On change of any field in Analysis, I need to mass update the Projects records for which I use a custom button. The code as follows:

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 

var records = {!GETRECORDIDS($ObjectType.Project1__c)}; 
var newRecords = []; 

if (records[0] == null) 

alert("Please select at least one row") 

else 

for (var n=0; n<=records.length; n++) { 
var t = new sforce.SObject("Project1__c"); 
t.id = records[n]; 
newRecords.push(t); 

result = sforce.connection.update(newRecords); 

window.location.reload(); 
}

 

The code updates the changes in Project records. But on this update of Project records a trigger needs to  work on Project Analysis records as follows. But in my case Project analysis records will be changed to 0 records. Meaning the update trigger code as follows is not working due to some reason:

 

trigger EqupdProjectAnalysis on Project1__c(after update)
 {
    List<Project_Analysis__c> ProjectAnalysis = new List <Project_Analysis__c> {};
    for (Project1__c p : trigger.new)
    {
       if(p.Start_Date_check__c == 1)
        {
          Project1__c[] updrec = [select id,start_Date_check__c from Project1__c pj where id = :p.id]; 
          for(Project1__c p1:updrec)
          {
         
          p1.start_Date_check__c=0;
          update p1;
          }
          Project_Analysis__c[] objsToDelete = [select id from Project_Analysis__c ol1 where ol1.Project1__c = :p.id];  
          delete objsToDelete; 
        }
        if(p.Start_Date_check__c == 1)
        {
          if ((p.Project_Type__c!='Master')&&(p.EPC_Awarding_Date__c !=null))
             {
                Integer i = 0;
                date Date1= p.EPC_Awarding_Date__c ;
                date myDate1=Date1;
                Decimal x=0;
                Decimal y=0;
                for (i=0;i< p.No_of_Months__c; i++) 
                {
                   
                    if((myDate1>=p.EPC_Awarding_Date__c) && (myDate1 < p.EndDate__c))
                   {
                       if(myDate1<=p.Q1__c)
                       {
                           x=p.Eq_Q1_month__c;
                         }
                       else if(myDate1<=p.Q2__c)
                       {
                           x=p.Eq_Q2_month__c;
                        }
                        else if(myDate1<p.Q3__c)
                       {
                           x=p.Eq_Q3_month__c;
                        }
                       else if(myDate1>=p.Q3__c)
                       {
                           x=p.Eq_Q4_month__c;
                        } 
                       }
                     else
                     {
                         x=0;
                     }
                   
                   if((myDate1>=p.EPC_Awarding_Date__c) && (myDate1 < p.EndDate__c))
                  {
                       if(myDate1<=p.Q1_FM__c)
                       {
                           y=p.FM_Q1_month__c;
                          
                       }
                       else if(myDate1<=p.Q2_FM__c)
                       {
                           y=p.FM_Q2_month__c;
                        }
                        else if(myDate1<p.Q3_FM__c)
                       {
                           y=p.FM_Q3_month__c;
                        }
                       else if(myDate1>=p.Q3_FM__c)
                       {
                           y=p.FM_Q4_month__c;
                        } 
                      }
                     else
                     {
                         y=0;
                     }
                   
                  ProjectAnalysis.add(new Project_Analysis__c(Project1__c= p.Id,Amount_per_month__c=x,FM__c=y,Date__c = myDate1));       
                  MyDate1=MyDate1.addMonths(1); 
                 }               
                }
           }
         }
         try
         {
               insert ProjectAnalysis;
          }
          catch (Exception Ex)
          {    
               system.debug(Ex);
          }
     }

 

Note: If I individually edit and save the project record, project analysis records are populated correctly.

 

Any help is greatly appreciated.

 

Thanks,

Liz

ArunDavidNSIArunDavidNSI

Does it not create any project analysis records or are the project analysis records created with 0 values?

lizmijohnlizmijohn

No Project analysis records are created.

ArunDavidNSIArunDavidNSI

There are a couple of points to note. 

 

1) You must not have queries or DML statements within a loop. This is not best practice and will create problems for you when handling larger number of records. I am referring to the statements in line numbers 8, 13, 15 and 16.

2) I am not sure of the difference in line numbers 6 and 18 as it appears to be checking for the same condition twice.

 

Though the best practice is not followed, I am not able to quickly put a finger on why no records were created. There are 2 things to try here.

 

1) In your javascript where you have "result = sforce.connection.update(newRecords);" do an alert on the result to see if there is any error as a result of the operation taking place behind the button.

2) Also add different debug statements in different sections in your code and monitor the debug logs to see if the code is executing some of the sections.

 

Arun

lizmijohnlizmijohn

Thanks Arun for your response.

I am trying to debug the problem.

 

Regards,

Liz