You need to sign in to do that
Don't have an account?
Apex code needs to be triggered on custom button click in list view
Hi,
I want to invoke an apex trigger on click on 'Update' button in a list view.
The update button code on 'Onclick java script' is 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();
}
And Apex trigger that needs to be invoked is as follows on button click:
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.Q3__c))
{
if(myDate1<p.Q1__c)
{
x=p.Eq_Q1_month__c;
}
else
{
x=p.Eq_Q2_3_4_month__c;
}
}
else
{
x=0;
}
if((myDate1>=p.EPC_Awarding_Date__c) && (myDate1 < p.Q3_FM__c))
{
if(myDate1<p.Q1_FM__c)
{
y=p.Eq_Q1_month_FM__c;
}
else if(myDate1<p.Q2_FM__c)
{
y=0;
}
else if(myDate1>=p.Q2_FM__c)
{
y=p.Eq_Q2_3_4_month_FM__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);
}
}
I assume there should be a line added in update custom button code as follows:
var result = sforce.apex.execute("class1", "method1",{iTitle : noteTitle});
But I do not have a global class defined. If I need to add let me know how to.
I am a big zero in coding. Please help.
Thanks.
Hi,
trigger will always invoke on the dml (update, insert, delete )mentioned in bracket ...
trigger trname Project1__c(after update)
You need not invoke it seperatly.
Your trigger will be called only in one conditon that is an "Update" on Project1__c.
Thanks,
Hi
I am trying to do a mass update on the records. The given code on custom button is not working.
Thanks
Well Few pointers.
Summary : You need not to do anything after you do a update from the Button, trigger will automatically fire based because you are doing an update
This is doing something similar
My triggers are active. But when I click on 'Update' button in list view, what I expect is the child records(Project Analysis) for 'Project' object need to be recalculated. But the child records are deleted on update.
When I edit and save individual Project record the child records are populated correctly.
I guess your js code behind the button has some errors.
I know my JS code has some errors. Thatz y I requested for help. I need some help from somebody to correct the code that I pasted in post.
Thanks,
Liz
Any help on this please!
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