+ Start a Discussion
amateur1amateur1 

avoid duplicate values in list

hi 

 

i have written a trigger where i have used list values to update the inventory transactions but with this when i am trying to insert two records at a time i am getting duplicate value error so please help me how to avoid the duplicate value error.i am marking red whre i am using the list value please help resolve the issue

 

trigger Wow_Fulfillment_FIFO_SNUM_LOTNUM on Fulfillement__c (after insert,after update) {

public list<Inventory_Transaction__c> ITList1{get;set;}
ITList1 = new List<Inventory_Transaction__c>();

public list<Inventory_Transaction__c> ITList2{get;set;}
ITList2 = new List<Inventory_Transaction__c>();

public list<Transaction_History__c> THList1{get;set;}
THList1 = new List<Transaction_History__c>();

for(Fulfillement__c FF : Trigger.new)
{
item__c i1 = [select id,Type__c from item__c where id=:ff.item__c];

if(i1.type__c == 'FIFO')
{
Inventory_Transaction__c[] IT = [select id,Item__c,Qty_In__c,Dev_Bucket_Qty_Remaining__c,Qty_Out__c,warehouse__c
from Inventory_Transaction__c where Item__c =:FF.Item__c AND
Warehouse__c =:FF.warehouse__c AND Dev_Bucket_Qty_Remaining__c != 0
Order By Date__c asc];

if(IT.size()==0)
{
ff.Invoice_Line_Item__c.adderror('Records are not available for Quantity to be fullfillment');
}
else
{
Decimal TotalINVqty=0 ;
for(Inventory_Transaction__c IT1:IT)
{
TotalINVqty = TotalINVqty + IT1.Dev_Bucket_Qty_Remaining__c;
system.debug('TotalINVqty:'+TotalINVqty);
}
if(FF.qty_out__c<=TotalINVqty )
{
//If fulfillment qty is same or less than INV transaction first Remaining qty
if(FF.qty_out__c <= IT[0].Dev_Bucket_Qty_Remaining__c)
{
Transaction_History__c TH = new Transaction_History__c();
TH.qty_out__c = FF.qty_out__c;
TH.Fulfillement__c = FF.id;
TH.Inventory_Transaction__c = IT[0].id;
THList1.add(TH);

IT[0].Dev_Bucket_Qty_Remaining__c = IT[0].Dev_Bucket_Qty_Remaining__c-FF.qty_out__c;
IT[0].qty_out__c =IT[0].qty_out__c+FF.qty_out__c;
ITList1.add(IT[0]);
}
else
{
// If fulfillment qty is greater than INV transaction first Remaining qty
Decimal FULFILLEDQTY =0.0;
Decimal REMFULFILLEDQTY =0.0;
FULFILLEDQTY = FF.qty_out__c;
if(FF.qty_out__c >= IT[0].Dev_Bucket_Qty_Remaining__c){
for(integer i =0;i<IT.size();i++)
{
if(FULFILLEDQTY >=0){
REMFULFILLEDQTY = FULFILLEDQTY -IT[i].Dev_Bucket_Qty_Remaining__c;
if(REMFULFILLEDQTY >0)
{
Transaction_History__c TH = new Transaction_History__c();
TH.qty_out__c = IT[i].Dev_Bucket_Qty_Remaining__c;
TH.Fulfillement__c = FF.id;
TH.Inventory_Transaction__c = IT[i].id;
THList1.add(TH);

IT[i].qty_out__c = IT[i].qty_out__c+IT[i].Dev_Bucket_Qty_Remaining__c;
IT[i].Dev_Bucket_Qty_Remaining__c = 0;
FULFILLEDQTY = REMFULFILLEDQTY ;
}
else
{
Transaction_History__c TH = new Transaction_History__c();
TH.qty_out__c = REMFULFILLEDQTY+IT[i].Dev_Bucket_Qty_Remaining__c;
TH.Fulfillement__c = FF.id;
TH.Inventory_Transaction__c = IT[i].id;
THList1.add(TH);

IT[i].qty_out__c =(IT[i].Dev_Bucket_Qty_Remaining__c+REMFULFILLEDQTY)+IT[i].qty_out__c ;
IT[i].Dev_Bucket_Qty_Remaining__c = IT[i].Dev_Bucket_Qty_Remaining__c-FULFILLEDQTY;
FULFILLEDQTY = REMFULFILLEDQTY ;
}
ITList2.add(IT[i]);
}
}
}
}
}
else
{
FF.Invoice_Line_Item__c.addError('Sorry!! "Qty Out" Is not Available to be Fullfilled');
}
}
}


else if(i1.type__c == 'SERIAL NUMBER')
{
Inventory_Transaction__c[] IT = [select id,Item__c,Qty_In__c,Dev_Bucket_Qty_Remaining__c,Qty_Out__c,warehouse__c
from Inventory_Transaction__c where Item__c =:FF.Item__c AND serial_number__c=:ff.serial_number__c
and Warehouse__c =:FF.warehouse__c AND Dev_Bucket_Qty_Remaining__c != 0 and serial_number__c!=null
Order By Date__c asc];
if(IT.size()==0)
{
ff.Invoice_Line_Item__c.adderror('Records are not available for Quantity to be fullfillment');
}
else
{
Decimal TotalINVqty=0 ;
for(Inventory_Transaction__c IT1:IT)
{
TotalINVqty = TotalINVqty + IT1.Dev_Bucket_Qty_Remaining__c;
system.debug('TotalINVqty:'+TotalINVqty);
}
if(FF.qty_out__c<=TotalINVqty )
{
//If fulfillment qty is same or less than INV transaction first Remaining qty
if(FF.qty_out__c <= IT[0].Dev_Bucket_Qty_Remaining__c)
{
Transaction_History__c TH = new Transaction_History__c();
TH.qty_out__c = FF.qty_out__c;
TH.Fulfillement__c = FF.id;
TH.Inventory_Transaction__c = IT[0].id;
THList1.add(TH);

IT[0].Dev_Bucket_Qty_Remaining__c = IT[0].Dev_Bucket_Qty_Remaining__c-FF.qty_out__c;
IT[0].qty_out__c =IT[0].qty_out__c+FF.qty_out__c;
ITList1.add(IT[0]);
}
else
{
// If fulfillment qty is greater than INV transaction first Remaining qty
Decimal FULFILLEDQTY =0.0;
Decimal REMFULFILLEDQTY =0.0;
FULFILLEDQTY = FF.qty_out__c;
if(FF.qty_out__c >= IT[0].Dev_Bucket_Qty_Remaining__c){
for(integer i =0;i<IT.size();i++)
{
if(FULFILLEDQTY >=0){
REMFULFILLEDQTY = FULFILLEDQTY -IT[i].Dev_Bucket_Qty_Remaining__c;
if(REMFULFILLEDQTY >0)
{
Transaction_History__c TH = new Transaction_History__c();
TH.qty_out__c = IT[i].Dev_Bucket_Qty_Remaining__c;
TH.Fulfillement__c = FF.id;
TH.Inventory_Transaction__c = IT[i].id;
THList1.add(TH);

IT[i].qty_out__c = IT[i].qty_out__c+IT[i].Dev_Bucket_Qty_Remaining__c;
IT[i].Dev_Bucket_Qty_Remaining__c = 0;
FULFILLEDQTY = REMFULFILLEDQTY ;
}
else
{
Transaction_History__c TH = new Transaction_History__c();
TH.qty_out__c = REMFULFILLEDQTY+IT[i].Dev_Bucket_Qty_Remaining__c;
TH.Fulfillement__c = FF.id;
TH.Inventory_Transaction__c = IT[i].id;
THList1.add(TH);

IT[i].qty_out__c =(IT[i].Dev_Bucket_Qty_Remaining__c+REMFULFILLEDQTY)+IT[i].qty_out__c ;
IT[i].Dev_Bucket_Qty_Remaining__c = IT[i].Dev_Bucket_Qty_Remaining__c-FULFILLEDQTY;
FULFILLEDQTY = REMFULFILLEDQTY ;
}
ITList2.add(IT[i]);
}
}
}
}
}
else
{
FF.Invoice_Line_Item__c.addError('Sorry!! "Qty Out" Is not Available to be Fullfilled');
}
}
}

if(i1.type__c == 'LOT NUMBER')
{
Inventory_Transaction__c[] IT = [select id,Item__c,Qty_In__c,Dev_Bucket_Qty_Remaining__c,Qty_Out__c,warehouse__c
from Inventory_Transaction__c where Item__c =:FF.Item__c AND lot_number__c=:ff.lot_number__c
and Warehouse__c =:FF.warehouse__c AND Dev_Bucket_Qty_Remaining__c != 0 and lot_number__c!=null
Order By Date__c asc];
if(IT.size()==0)
{
ff.Invoice_Line_Item__c.adderror('Records are not available for Quantity to be fullfillment');
}
else
{
Decimal TotalINVqty=0 ;
for(Inventory_Transaction__c IT1:IT)
{
TotalINVqty = TotalINVqty + IT1.Dev_Bucket_Qty_Remaining__c;
system.debug('TotalINVqty:'+TotalINVqty);
}
if(FF.qty_out__c<=TotalINVqty )
{
//If fulfillment qty is same or less than INV transaction first Remaining qty
if(FF.qty_out__c <= IT[0].Dev_Bucket_Qty_Remaining__c)
{
Transaction_History__c TH = new Transaction_History__c();
TH.qty_out__c = FF.qty_out__c;
TH.Fulfillement__c = FF.id;
TH.Inventory_Transaction__c = IT[0].id;
THList1.add(TH);

IT[0].Dev_Bucket_Qty_Remaining__c = IT[0].Dev_Bucket_Qty_Remaining__c-FF.qty_out__c;
IT[0].qty_out__c =IT[0].qty_out__c+FF.qty_out__c;
ITList1.add(IT[0]);
}
else
{
// If fulfillment qty is greater than INV transaction first Remaining qty
Decimal FULFILLEDQTY =0.0;
Decimal REMFULFILLEDQTY =0.0;
FULFILLEDQTY = FF.qty_out__c;
if(FF.qty_out__c >= IT[0].Dev_Bucket_Qty_Remaining__c){
for(integer i =0;i<IT.size();i++)
{
if(FULFILLEDQTY >=0){
REMFULFILLEDQTY = FULFILLEDQTY -IT[i].Dev_Bucket_Qty_Remaining__c;
if(REMFULFILLEDQTY >0)
{
Transaction_History__c TH = new Transaction_History__c();
TH.qty_out__c = IT[i].Dev_Bucket_Qty_Remaining__c;
TH.Fulfillement__c = FF.id;
TH.Inventory_Transaction__c = IT[i].id;
THList1.add(TH);

IT[i].qty_out__c = IT[i].qty_out__c+IT[i].Dev_Bucket_Qty_Remaining__c;
IT[i].Dev_Bucket_Qty_Remaining__c = 0;
FULFILLEDQTY = REMFULFILLEDQTY ;
}
else
{
Transaction_History__c TH = new Transaction_History__c();
TH.qty_out__c = REMFULFILLEDQTY+IT[i].Dev_Bucket_Qty_Remaining__c;
TH.Fulfillement__c = FF.id;
TH.Inventory_Transaction__c = IT[i].id;
THList1.add(TH);

IT[i].qty_out__c =(IT[i].Dev_Bucket_Qty_Remaining__c+REMFULFILLEDQTY)+IT[i].qty_out__c ;
IT[i].Dev_Bucket_Qty_Remaining__c = IT[i].Dev_Bucket_Qty_Remaining__c-FULFILLEDQTY;
FULFILLEDQTY = REMFULFILLEDQTY ;
}
ITList2.add(IT[i]);
}
}
}
}
}
else
{
FF.Invoice_Line_Item__c.addError('Sorry!! "Qty Out" Is not Available to be Fullfilled');
}
}
}
}
insert THList1;
update ITList1;
update ITList2;
}

 

Navatar_DbSupNavatar_DbSup

Hi,

 

If you want to deal with duplicate record issue then make use of Set in your code. Set helps in getting unique records also in last you can add all records to List by help of addAll() method.

 

For more detail follow the below links

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_set.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_list.htm

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

amateur1amateur1

hi navatar

 

i tried that way but still i am getting the error can u please help change my code so that i could do that in other cases

amateur1amateur1

hi navatar 

can u help me on the issue please