You need to sign in to do that
Don't have an account?
aottaru
Apex Trigger not working Properly
I have written a trigger that updates a field in the parent record when the child record is updated.
The trigger is supposed to combine value entered in few fields in all child records associated to the parent record and then update a field in the parent object with this value.
The trigger is working 98% of the time, for the other time the field is not being updated properly. It appears there are some extra values added. I am not sure where they are coming from.
Can someone please help?
Below is my trigger. Thanks
trigger UpdateDispatches on Charter__c (before Update)
{
//Declare variables
String strScheduledDate ='';
String strActualDispatch='';
String strDispatchQuantity='';
String strSchDate='';
string strAcDate='';
string Disp='';
for(Charter__c c: Trigger.new)
{
//Selecting required fields from dispatches
List<Dispatch__c> Dispatch = [select id,
Date_Scheduled__c,
Scheduled_Quantity_1__c,
Scheduled_Quantity_2__c,
Scheduled_Quantity_3__c,
Scheduled_Quantity_4__c,
Description__c,
Actual_Date__c,
Actual_Quantity_1__c,
Actual_Quantity_2__c,
Actual_Quantity_3__c,
Actual_Quantity_4__c
from Dispatch__c where Charter__c = :c.Id ORDER BY Date_Scheduled__c ASC];
for(Dispatch__c d: Dispatch)
{
//Going through each dispatches and assign values
Integer intMonthIn;
Integer intDayIn;
Integer intYearIn;
String strMonth;
String StrQuantities;
String strActualDate;
intMonthIn = d.Date_Scheduled__c.Month();
intDayIn = d.Date_Scheduled__c.Day();
intYearIn = d.Date_Scheduled__c.Year();
strMonth = MonthString(intMonthIn);
string scd;
string acd;
If(d.Description__c == null)
{
scd= 'Scheduled Date : ' + strMonth +' '+ String.valueOf(intDayIn)+ ', '+ String.valueOf(intYearIn)+' Quantity: '+ String.valueOf(d.Scheduled_Quantity_1__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_2__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_3__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_4__c);
}else
{
scd= d.Description__c +' : '+ strMonth +' '+ String.valueOf(intDayIn)+ ', '+ String.valueOf(intYearIn)+' Quantity: '+ String.valueOf(d.Scheduled_Quantity_1__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_2__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_3__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_4__c);
}
strScheduledDate = scd;
disp = disp +'\n'+ strScheduledDate.substringBefore('/ null');
If(String.valueOf(d.Actual_Date__c) == null)
{
}else
{
intMonthIn = d.Actual_Date__c.Month();
intDayIn = d.Actual_Date__c.Day();
intYearIn = d.Actual_Date__c.Year();
strMonth = MonthString(intMonthIn);
//acd = 'Actual Date : ' +strMonth + ' '+ String.valueOf(intDayIn) +', '+ String.valueOf(intYearIn)+' Quantity: '+String.valueOf(d.Actual_Quantity_1__c)+ ' / '+String.valueOf(d.Actual_Quantity_2__c)+ ' / '+String.valueOf(d.Actual_Quantity_3__c)+ ' / '+String.valueOf(d.Actual_Quantity_4__c)+'\n';
acd = '- Actual Date'+' : ' +strMonth + ' '+ String.valueOf(intDayIn) +', '+ String.valueOf(intYearIn)+' Quantity: '+String.valueOf(d.Actual_Quantity_1__c)+ ' / '+String.valueOf(d.Actual_Quantity_2__c)+ ' / '+String.valueOf(d.Actual_Quantity_3__c)+ ' / '+String.valueOf(d.Actual_Quantity_4__c);
}
if(acd ==null)
{
disp = disp +'\n';
}
else
{
strActualDispatch = acd;
disp = disp+'\n' + strActualDispatch.substringBefore('/ null')+'\n';
}
}
}
for(Charter__c c: trigger.new)
{
c.Dispatch_Schedule__c = disp.remove('null');
}
public String MonthString(Integer intMonth)
{
//This method will return a string value of the month
If(intMonth == 1)
{
return 'Jan';
}else if (intMonth == 2)
{
return 'Feb';
}
else if (intMonth == 3)
{
return 'Mar';
} else if (intMonth == 4)
{
return 'Apr';
}
else if (intMonth == 5)
{
return 'May';
} else if (intMonth == 6)
{
return 'Jun';
}else if (intMonth == 7)
{
return 'Jul';
}
else if (intMonth == 8)
{
return 'Aug';
}
else if (intMonth == 9)
{
return 'Sep';
}
else if (intMonth == 10)
{
return 'Oct';
}
else if (intMonth == 11)
{
return 'Nov';
}
else if (intMonth == 12)
{
return 'Dec';
}
return '';
}
}
The trigger is supposed to combine value entered in few fields in all child records associated to the parent record and then update a field in the parent object with this value.
The trigger is working 98% of the time, for the other time the field is not being updated properly. It appears there are some extra values added. I am not sure where they are coming from.
Can someone please help?
Below is my trigger. Thanks
trigger UpdateDispatches on Charter__c (before Update)
{
//Declare variables
String strScheduledDate ='';
String strActualDispatch='';
String strDispatchQuantity='';
String strSchDate='';
string strAcDate='';
string Disp='';
for(Charter__c c: Trigger.new)
{
//Selecting required fields from dispatches
List<Dispatch__c> Dispatch = [select id,
Date_Scheduled__c,
Scheduled_Quantity_1__c,
Scheduled_Quantity_2__c,
Scheduled_Quantity_3__c,
Scheduled_Quantity_4__c,
Description__c,
Actual_Date__c,
Actual_Quantity_1__c,
Actual_Quantity_2__c,
Actual_Quantity_3__c,
Actual_Quantity_4__c
from Dispatch__c where Charter__c = :c.Id ORDER BY Date_Scheduled__c ASC];
for(Dispatch__c d: Dispatch)
{
//Going through each dispatches and assign values
Integer intMonthIn;
Integer intDayIn;
Integer intYearIn;
String strMonth;
String StrQuantities;
String strActualDate;
intMonthIn = d.Date_Scheduled__c.Month();
intDayIn = d.Date_Scheduled__c.Day();
intYearIn = d.Date_Scheduled__c.Year();
strMonth = MonthString(intMonthIn);
string scd;
string acd;
If(d.Description__c == null)
{
scd= 'Scheduled Date : ' + strMonth +' '+ String.valueOf(intDayIn)+ ', '+ String.valueOf(intYearIn)+' Quantity: '+ String.valueOf(d.Scheduled_Quantity_1__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_2__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_3__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_4__c);
}else
{
scd= d.Description__c +' : '+ strMonth +' '+ String.valueOf(intDayIn)+ ', '+ String.valueOf(intYearIn)+' Quantity: '+ String.valueOf(d.Scheduled_Quantity_1__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_2__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_3__c)+ ' / '+ String.valueOf(d.Scheduled_Quantity_4__c);
}
strScheduledDate = scd;
disp = disp +'\n'+ strScheduledDate.substringBefore('/ null');
If(String.valueOf(d.Actual_Date__c) == null)
{
}else
{
intMonthIn = d.Actual_Date__c.Month();
intDayIn = d.Actual_Date__c.Day();
intYearIn = d.Actual_Date__c.Year();
strMonth = MonthString(intMonthIn);
//acd = 'Actual Date : ' +strMonth + ' '+ String.valueOf(intDayIn) +', '+ String.valueOf(intYearIn)+' Quantity: '+String.valueOf(d.Actual_Quantity_1__c)+ ' / '+String.valueOf(d.Actual_Quantity_2__c)+ ' / '+String.valueOf(d.Actual_Quantity_3__c)+ ' / '+String.valueOf(d.Actual_Quantity_4__c)+'\n';
acd = '- Actual Date'+' : ' +strMonth + ' '+ String.valueOf(intDayIn) +', '+ String.valueOf(intYearIn)+' Quantity: '+String.valueOf(d.Actual_Quantity_1__c)+ ' / '+String.valueOf(d.Actual_Quantity_2__c)+ ' / '+String.valueOf(d.Actual_Quantity_3__c)+ ' / '+String.valueOf(d.Actual_Quantity_4__c);
}
if(acd ==null)
{
disp = disp +'\n';
}
else
{
strActualDispatch = acd;
disp = disp+'\n' + strActualDispatch.substringBefore('/ null')+'\n';
}
}
}
for(Charter__c c: trigger.new)
{
c.Dispatch_Schedule__c = disp.remove('null');
}
public String MonthString(Integer intMonth)
{
//This method will return a string value of the month
If(intMonth == 1)
{
return 'Jan';
}else if (intMonth == 2)
{
return 'Feb';
}
else if (intMonth == 3)
{
return 'Mar';
} else if (intMonth == 4)
{
return 'Apr';
}
else if (intMonth == 5)
{
return 'May';
} else if (intMonth == 6)
{
return 'Jun';
}else if (intMonth == 7)
{
return 'Jul';
}
else if (intMonth == 8)
{
return 'Aug';
}
else if (intMonth == 9)
{
return 'Sep';
}
else if (intMonth == 10)
{
return 'Oct';
}
else if (intMonth == 11)
{
return 'Nov';
}
else if (intMonth == 12)
{
return 'Dec';
}
return '';
}
}
Don't use SOQL inside the for loop.
http://www.infallibletechie.com/2014/03/trigger-to-find-total-opportunity.html
If this solves your problem, kindly mark it as the best answer.
Regards,
Magulan
http://www.infallibletechie.com
This output below is for a parent record with only one child record.
The output was
Scheduled Date : Oct 27, 2014 Quantity: 1000
Scheduled Date : Jul 28, 2014 Quantity: 7000
Scheduled Date : Sep 29, 2014 Quantity: 10000
Instead of just
Scheduled Date : Oct 27, 2014 Quantity: 1000
I am not sure where the last two lines came from? Please let me know if this helps. Thanks
Please update the same record(which is showing wrong information) and let me know what does the size shows up as.
Just as a sidenote as Magulan D suggested move the select statement out of forloop. This might have an effect when you do bulk update.
Some records have multiple Dispatch__c records but this one does not, only one. I am not sure if this make sense.
As Magulan D suggested, I will move the select statement out of the loop; In fact I have had some issues in the past when using the Data loader for bulk updates. Thank you.
Update: The trigger is giving out incorrect output with one specific user. I am thinking it is related to permission but I am not sure where to look.
Thanks
I now have dispatch IDs used to update the records. Dispatches ID a0vG0000007GqgMIAS and
a0vG0000007GqgWIAS was added to two Charters (parent records) with IDs a0rG0000009emYL and a0rG0000009emY1. Trigger should just add dispatch with ID a0vG0000007GqgMIAS to Charter a0rG0000009emY1 and dispatch with ID a0vG0000007GqgWIAS to Charter a0rG0000009emYL
I am really not sure why this is happening. When I edit the record to force the trigger to run again, The records are being updated properly.
Thank you for your help on this Ramu, I am really confused on what is going on. It appears as if the trigger is not using the entire ID all the time. Is there a way to force this?