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
sieb4mesieb4me 

question on related list and parent reference

hi friends,

i have requirement to prevent dupilcate records on related list (products) which is child for case.

 

I got the duplicate code working fine on the productline object independent of case, however now i want to make it work as part of case since requirment is to prevent duplcate on related list product line.

 

so i need to now check all the products on product line as part of parent object case and then prevent user from entering duplicate, how do i reference case from child object productline?

 

thanks

 

 

 

trigger pp on Product_Line__c (before insert, before update) {
//private list<Case> caseemlist;
//caseemlist=new list<Case>();
Map<String, Product_Line__c> Prodmap = new Map<String, Product_Line__c>();
for (Product_Line__c Pl : System.Trigger.new) {

// Make sure we don't treat an Products__c that
// isn't changing during an update as a duplicate.

if ((Pl.Products__c != null) &&(System.Trigger.isInsert || (Pl.Products__c !=
System.Trigger.oldMap.get(Pl.Id).Products__c)))
{

// Make sure another new Product_Line__c isn't also a duplicate

if (Prodmap.containsKey(Pl.Products__c))
{
Pl.Products__c.addError('Another new Product has the '
+ 'same Products');
} else {
Prodmap.put(Pl.Products__c, Pl);
}
}
}

// Using a single database query, find all the Product_Line__cs in
// the database that have the same Products__c as any
// of the Product_Line__cs being inserted or updated.

for (Product_Line__c Pl : [SELECT Products__c FROM Product_Line__c
WHERE Products__c IN :Prodmap.KeySet()]) {
Product_Line__c newpl = Prodmap.get(Pl.Products__c);
newPl.Products__c.addError('This product '
+ 'already exists.');
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Balu_SFDCBalu_SFDC

Hi,

 

You can reference the parent object using the Field name of the case in productLine

Like...

 

SELECT Products__c FROM Product_Line__c
WHERE Products__c IN :Prodmap.KeySet() And Case__C=:trigger.new.case__C

 

here it gets all the product records which are related to case. hope you understand.

 

Good Day....

Balu...

All Answers

Balu_SFDCBalu_SFDC

Hi,

 

You can reference the parent object using the Field name of the case in productLine

Like...

 

SELECT Products__c FROM Product_Line__c
WHERE Products__c IN :Prodmap.KeySet() And Case__C=:trigger.new.case__C

 

here it gets all the product records which are related to case. hope you understand.

 

Good Day....

Balu...

This was selected as the best answer
sieb4mesieb4me
thanks Balu, I did something similar.