function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
diydiy 

How can i remove duplicate product in my code?

My requirement is

 I have 3 product in one object and 6 serial number in another object.. serial number related to product.. Datas are p1 -> s1,s2 p2 ->s3,s4 p3->s5,s6..  In my UI have 2 page block table.. 1st page block table I enter serial number s1,s2,s3 ... my 2nd pageblock wil display p1 and p2 ... but its display p1,p1 and p2.. duplicate value display.. How can I resolve this issue? my code is..

public void productadd (){

for(integer i=0;i<AssetAddList.size();i++ ){          
    ProductList = [select id,Product_Number__c,Product_Number__r.name,Serial_Number__c from Equipment_Master__c where Serial_Number__c   =:  AssetAddList[i].Serial_Number__c ];

    serialIncConfigsItem[i].Product_Number__c = ProductList.Product_Number__c;


}
 

  AssetAddlist - > Enter serial number page block table
  serialIncConfigsItem -> display product

 

How can i remove duplicate product in my code?

Best Answer chosen by Admin (Salesforce Developers) 
digamber.prasaddigamber.prasad

Hi,

 

Could you please try below code snippet:-

 

for(integer i=0;i<AssetAddList.size();i++ ){ 
         
    ProductList = [select id,Product_Number__c,Product_Number__r.name,Serial_Number__c from Equipment_Master__c where Serial_Number__c   =:  AssetAddList[i].Serial_Number__c ];
	boolean dup = false;
	for(integer j=0; j<serialIncConfigsItem.size(); j++){
		if(serialIncConfigsItem[j].Product_Number__c == ProductList.Product_Number__c){
			dup = true;
			break;
		}
	}

	if(dup == false)
		serialIncConfigsItem[i].Product_Number__c = ProductList.Product_Number__c;


}

 Let me know if you still see issue.

 

Happy to help you!

 

 

 

All Answers

digamber.prasaddigamber.prasad

Hi,

 

Could you please try below code snippet:-

 

for(integer i=0;i<AssetAddList.size();i++ ){ 
         
    ProductList = [select id,Product_Number__c,Product_Number__r.name,Serial_Number__c from Equipment_Master__c where Serial_Number__c   =:  AssetAddList[i].Serial_Number__c ];
	boolean dup = false;
	for(integer j=0; j<serialIncConfigsItem.size(); j++){
		if(serialIncConfigsItem[j].Product_Number__c == ProductList.Product_Number__c){
			dup = true;
			break;
		}
	}

	if(dup == false)
		serialIncConfigsItem[i].Product_Number__c = ProductList.Product_Number__c;


}

 Let me know if you still see issue.

 

Happy to help you!

 

 

 

This was selected as the best answer
diydiy

Thank u lot.. working fine

 

diydiy

data display

 

pno

1

2

3

null

null

4

5

 

I want to display

 

1

2

3

4

5

 

digamber.prasaddigamber.prasad

Hi,

 

I did some modification, could you please try below code snippet:-

 

integer k=0;
for(integer i=0;i<AssetAddList.size();i++ ){ 
         
    ProductList = [select id,Product_Number__c,Product_Number__r.name,Serial_Number__c from Equipment_Master__c where Serial_Number__c   =:  AssetAddList[i].Serial_Number__c ];
	boolean dup = false;
	for(integer j=0; j<serialIncConfigsItem.size(); j++){
		if(serialIncConfigsItem[j].Product_Number__c == ProductList.Product_Number__c){
			dup = true;
			break;
		}
	}

	if(dup == false){
		serialIncConfigsItem[k].Product_Number__c = ProductList.Product_Number__c;
		k++;
	}

}

 

Let me know if you still have any issue.

 

Happy to help you!

 

digamber.prasaddigamber.prasad

Did it help you!

diydiy

Thanks working fine now

diydiy

I think query inside the for loop.. too many soql 101 error

digamber.prasaddigamber.prasad

Hi,

 

To solve this, we have to bulkify the query. Let me know if you need help on this.

 

In the meanwhile, if above modification regarding getting rid off 'NULL' elements helped you, could you please be kind enought to give me KUDOS for that.

 

Happy to help you!

diydiy

bulify the quert means?????????

digamber.prasaddigamber.prasad

Hi,

 

Bulkify means, enable your code to handle bulk records and still don't hit governor limits. Do you want any help on this?

diydiy

how can i enable?

digamber.prasaddigamber.prasad

Give me sometime, I will modify existing code so that you can use it.

digamber.prasaddigamber.prasad

Hey,

 

Could you please send me your complete trigger so that I can bulkify the whole trigger.