You need to sign in to do that
Don't have an account?
sam_Admin
Trigger help for mapping values
We have "Location" field on Opportunity with picklist values and we have "Opp_Location__c" field on the account, this trigger displays the values of "Location" field into "Opp_Location__c" field, how to modify the trigger so i can map the same values and not display it more than once? for ex i have picklist value "Hospital" and if the account has 10 opps with same hospital location then i want to display it only one time
trigger RollupLocation on Opportunity (after insert, after update) {
Map<ID, Account > parentOpps = new Map<ID, Account>();
Set<Id> listIds = new Set<Id>();
for (Opportunity childObj : Trigger.new) {
listIds.add(childObj.AccountId);
}
parentOpps = new Map<Id, Account>([SELECT id, Opp_Location__c ,
(SELECT ID, Location_Type__c FROM Opportunities)
FROM Account WHERE ID IN :listIds]);
List<Account> lstAccount = new List<Account>();
for(Account acct:parentOpps.values())
{
List<String> strType = new List<String>();
for(Opportunity opty:acct.Opportunities)
{
strType.add(opty.Location_Type__c);
}
acct.Opp_Location__c = String.join(strType,',');
}
update parentOpps.values();
}
trigger RollupLocation on Opportunity (after insert, after update) {
Map<ID, Account > parentOpps = new Map<ID, Account>();
Set<Id> listIds = new Set<Id>();
for (Opportunity childObj : Trigger.new) {
listIds.add(childObj.AccountId);
}
parentOpps = new Map<Id, Account>([SELECT id, Opp_Location__c ,
(SELECT ID, Location_Type__c FROM Opportunities)
FROM Account WHERE ID IN :listIds]);
List<Account> lstAccount = new List<Account>();
for(Account acct:parentOpps.values())
{
List<String> strType = new List<String>();
for(Opportunity opty:acct.Opportunities)
{
strType.add(opty.Location_Type__c);
}
acct.Opp_Location__c = String.join(strType,',');
}
update parentOpps.values();
}
I guess I should have tested that haha,
try this, put the values into a Set, then loop over the set and buld up your string
All Answers
If you change the List on line 17 (List<String> strType = new List<String>();) to a Set it should fix the issue, sets only contain 1 instance of a value, so if you put 'Hospital' into Set<String> strType 3 times strType would = {'Hospital'}
hope this helps
I guess I should have tested that haha,
try this, put the values into a Set, then loop over the set and buld up your string