I have two different custom objects Catalogs
and Products
. I have a visualforce page where user can select multiple catalogs and multiple products from two different picklists respectively. I also have a junction object called Catalog-Products
, a child to both catalogs and products. The selected catalogs and products are created as records for Catalog-Products Junction object
. So, I have two different lists of each of those object called as CatalogList
and ProductList
which are captured from that mulit-select picklists (that I explained above). There might be already some Catalog-Product records with the selected product and selected Catalogs.
How can I compare both those lists with existing Catalog-Product
junction object records and insert only new records?
Thanks.
Update- I am stuck with the code here
list<Catalog_Product__c> existingList = [select Id, Product__c, Catalog__c from Catalog_Product__c where Product__c IN: prodIds and Catalog__c IN: catalogIds];
//prodIds and catalogIds are Ids retrieved from selected values of Picklists
list<Catalog_Product__c> newList = new list<Catalog_Product__c>();
for(Id clogId: catalogIds)
{
for(Id prdId: prodIds)
{
Catalog_Product__c cpd = new Catalog_Product__c();
cpd.Product__c = prdId;
cpd.Catalog__c = clogId;
newList.add(cpd);
}
}
newList and existingList are the two lists of same Object . Need help in comparing those lists and inserting the non-duplicate values.
Catalog
andProduct
lists or are you querying these two objects and building the<apex:selectList />
options in your page controller?<apex:selectList />
to display the data.