Sample Code:
- trigger AccountTrigger on Account ( after insert ) {
- Set < String > setBillCountries = new Set < String >();
- for( Account acc : trigger.new ) {
- if ( acc.BillingCountry != null )
- setBillCountries.add( acc.BillingCountry );
- }
- if ( setBillCountries.size() > 0 ) {
- Map < String, String > mapTopicNameId = new Map < String, String >();
- for ( Topic objTopic : [ SELECT Id, Name FROM Topic WHERE Name IN: setBillCountries ] )
- mapTopicNameId.put( objTopic.Name, objTopic.Id );
- for( Account acc : trigger.new ) {
- if ( acc.BillingCountry != null && mapTopicNameId.containsKey( acc.BillingCountry ) ) {
- ConnectAPI.Topic objTopic = ConnectAPI.Topics.assignTopic( null, String.valueOf( acc.Id ), mapTopicNameId.get( acc.BillingCountry ) );
- }
- }
- }
- }
Note:
Each assignTopic() counts against DML limit.
Use assignTopicByName() to avoid SOQL and use Topic Name instead of Id.
No comments:
Post a Comment