You need to sign in to do that
Don't have an account?

How to add coditions to when my apex code inserts a record.
Below is my for loop to insert records:
List<Customer_Product_Summary__c> cslist = new List<Customer_Product_Summary__c>([select id, customer_Product_catalog__c,Primary_Condition__c, account__c from Customer_Product_Summary__c where account__c =: o.accountid]); for(cCustomer p : getsummary()) { system.debug('@@@@@@@$$$$$:'+p); Customer_Product_Summary__c cs = new Customer_Product_Summary__c(); cs.Account__c = o.accountid; cs.Customer_Product_Catalog__c = p.con.id; cs.name = p.con.name; cs.Primary_Condition__c = p.con.Condition_1__c; cs.Primary_Shape__c = p.con.Shape_1__c; cs.Defect_Description__c = p.con.Defect_Description__c; cs.Capacity__c = p.con.Capacity__c; cs.Bulk_Density__c = p.con.Bulk_Density__c; cs.Industry_Segment__c = p.con.Industry_Segment__c; If(cs.Customer_Product_Catalog__c!=null){ try { item1.add(cs); system.debug('#######################'+cs); } catch(System.DMLException e) { ApexPages.addMessages(e); } }
Add the condition to only create the record if cs.Customer_Product_Catalog__c contains a unique value when the record is added to the list 'cslist'?
Thanks
Hello,
1.By the way I wrote it the Set contains String - only the field Customer_Product_Catalog.
2.If your uniquness is combination of the 3 fields than add to the set the containation of them:
cusPro_Set.add(p.con.id + p.con.Condition_1__c + p.con.industry_segment__c);
If each of this field must be uniqe, then create 3 set and for each set add the relevant field.
inqSet1.add(p.con.id);
inqSet2.add(p.con.Condition_1__c);
inqSet3.add(p.con.industry_segment__c);
All Answers
hi,
I'm not sure I clearly understand.
If the target is to insert only if the Customer_Product_Catalog is not exists yet in privoues records, then:
1.Collect all the existsing values:
Set<String> cusPro_Set=new Set<String>();
for(Customer_Product_Summary__c cps : cslist)
cusPro_Set.add(cps.customer_Product_catalog__c);
2.Later when creating the new Customer_Product_Summary__c, use:
if(cusPro_Set.contains(p.con.id)
continue
else{
cusPro_Set.add(p.con.id);
//do the insertion
}
I think you understand. When I add the cps.customer_Product_catalog__c to the set, am I only adding that one field or the entire record. For example can I expand the code in item 2 to the following?
if(cusPro_Set.contains(p.con.id) and custPro_Set.contains(p.con.Condition_1__c) and CustPro_set.contains(p.con.industry_segment__c))
continue
else{
cusPro_Set.add(p.con.id);
//do the insertion
}
By the end of the code, I am wanting to only create unique records based on 3 separate fields (an id [p.con.id] and two picklist fields [p.con.condition_1__c and p.con.industry_segment__c]).
Thanks
Hello,
1.By the way I wrote it the Set contains String - only the field Customer_Product_Catalog.
2.If your uniquness is combination of the 3 fields than add to the set the containation of them:
cusPro_Set.add(p.con.id + p.con.Condition_1__c + p.con.industry_segment__c);
If each of this field must be uniqe, then create 3 set and for each set add the relevant field.
inqSet1.add(p.con.id);
inqSet2.add(p.con.Condition_1__c);
inqSet3.add(p.con.industry_segment__c);