You need to sign in to do that
Don't have an account?
Help with trigger regarding Updating a Parent record from child
Hi,
I have two objects which are related contacts and relationships and in relationship i have a formula field which fills in values A OR B OR C OR D from user record and i am trying to get this information to the contact from relationship .
So i created a long text field on Contcts and wrote a trigger on relationships , everything works great. But the only pb is if i have records with values A or b or c or d more than once then on my long text field it displays more times but i want it to dispaly only once( i.e for 1 contact there can be 100 relationshps out of which it can have any values a,b,c, or d but i want only once to display) how can i acheive this.
trigger ContactRelationshipTrigger on Relationship__c (after delete, after insert, after update) {
// fires after both insert and update
if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter){
// find the ids of all Contacts that were affected
Set<Id> ContactIds = new Set<Id>();
for (Relationship__c ar : [select Id, Contact__c from Relationship__c
where Id IN :Trigger.newMap.keySet()])
ContactIds.add(ar.Contact__c);
// process the Contacts
ContactRelationshipTriggerHandler.ProcessRelationshipsAsync(ContactIds);
// fires when records are deleted. may want to do undelete also?
} else if(Trigger.isDelete && Trigger.isAfter){
// find the ids of all Contacts that were affected
Set<Id> ContactIds = new Set<Id>();
for (ID id : Trigger.oldMap.keySet())
ContactIds.add(Trigger.oldMap.get(id).Contact__c);
// process the Contacts
ContactRelationshipTriggerHandler.ProcessRelationshipsAsync(ContactIds);
}
}
--------------------------------------------------------------------------------------------------------------------------------
public with sharing class ContactRelationshipTriggerHandler {
@future
public static void ProcessRelationshipsAsync(Set<ID> ContactIds){
// holds a map of the Contact id and comma separated Relationships to build
Map<Id, String> ContactRelationshipMap = new Map<Id, String>();
// get ALL of the Relationships for all affected Contacts so we can build
List<Relationship__c> ContactRelationships = [select id, Contact__c,
Brand__c from Relationship__c
where Contact__c IN :ContactIds order by Brand__c ];
for (Relationship__c ar : ContactRelationships) {
if (!ContactRelationshipMap.containsKey(ar.Contact__c)) {
// if the key (Contact) doesn't exist, add it with Relationship name
ContactRelationshipMap.put(ar.Contact__c,ar.Brand__c);
} else {
// if the key (Contact) already exist, add ", Relationship-name"
ContactRelationshipMap.put(ar.Contact__c,ContactRelationshipMap.get(ar.Contact__c) +
', ' + ar.Brand__c);
}
}
// get the Contact that were affected
List<Contact> Contacts = [select id from Contact where Id IN :ContactIds];
// add the comma separated list of Relationships
for (Contact a : Contacts)
a.Brands__c = ContactRelationshipMap.get(a.id);
// update the Contacts
update Contacts;
}
}
Thanks ,
Akhil.
This is where you're composing your string value. If you want to prevent duplicates , just chek wether your ar.Brand__C is already in the string you are building. Looking at the quality of the rest of your code, I'm a bit suprised you need help on this.