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
SS KarthickSS Karthick 

Fetch the reocrd based on the topic name

Hi Everybody,
       I want to fetch the records based on the topic name ..
For Example
I have 15 contacts ..10 contacts are associated with the topic name sample1 and others are sample2

Wanna fetch the records associated with the topic name sample1

Please Help!
Best Answer chosen by SS Karthick
logontokartiklogontokartik
List<String> contactIds = new List<String>();
String contactIdPrefix = Schema.SObjectType.Contact.getKeyPrefix();
for(TopicAssignment ta:[SELECT CreatedById,CreatedDate,EntityId,Id,TopicId FROM TopicAssignment where Topic.Name = 'Svp']){
   // This is using Id's getSobjectTypeMethod
    if(ta.EntityId.getSobjectType().getDescribe().getLocalName() == 'Contact'){
        contactIds.add(ta.EntityId);
    }
    
   // you can use this as well
    if(String.valueof(ta.EntityId).subString(0,3) == contactIdPrefix){
        contactIds.add(ta.EntityId);
    }
}
system.debug(contactIds);

Here is sample code:

You can use either of the two ways, but dont use both


All Answers

SfdcprogrammerSfdcprogrammer
I assume that you have created a Custom Field on Contact for saving your Topic Names, Lets take its API as topic_name__c

then your possible SOQL query will be -

select id,Name from Contact where topic_name__c='test1'

See how I have used it to get my records in List -

List<Contact> lstContact = new List<Contact>();

    lstContact=[select id,Name from Contact where topic_name__c='test1'];  
    //here you got you got your records of topic=test1 in List lstContact, now use for loop to access them - like

    for(Contact objContact:lstContact){
        /* you have got each record in **objContact** who's having topic_name=test1, you access fields as per your requirement*/ 
      System.Debug('---Name---'+objContact.Name);
    }


logontokartiklogontokartik
Hi, you can use the below query on TopicAssignment Object

SELECT CreatedById,CreatedDate,EntityId,Id,TopicId FROM TopicAssignment where Topic.Name = 'sample1'
If you want to just get Contacts, after you query, just use the first three characters of EntityId to get specific Object records. Eg: 003 for contact

SS KarthickSS Karthick
Hy logontokartik,
          Thanks for your response..
but i cant understand how to get the contacts
Can you give me some example
logontokartiklogontokartik
List<String> contactIds = new List<String>();
String contactIdPrefix = Schema.SObjectType.Contact.getKeyPrefix();
for(TopicAssignment ta:[SELECT CreatedById,CreatedDate,EntityId,Id,TopicId FROM TopicAssignment where Topic.Name = 'Svp']){
   // This is using Id's getSobjectTypeMethod
    if(ta.EntityId.getSobjectType().getDescribe().getLocalName() == 'Contact'){
        contactIds.add(ta.EntityId);
    }
    
   // you can use this as well
    if(String.valueof(ta.EntityId).subString(0,3) == contactIdPrefix){
        contactIds.add(ta.EntityId);
    }
}
system.debug(contactIds);

Here is sample code:

You can use either of the two ways, but dont use both


This was selected as the best answer
SS KarthickSS Karthick
Hy logontokartik,
     Can you tell me the process of following code
if(ta.EntityId.getSobjectType().getDescribe().getLocalName() == 'Contact'){
        contactIds.add(ta.EntityId);
    }

SS KarthickSS Karthick
Hy logontokartik,
        Can you tell me how to the topic names based on the contact name?
Reverse order(1 contact have 15 topic name )
wanna fetch 15 topic name


Thanks in advance
Karthick

logontokartiklogontokartik
You can get the contactId from contact table and then query TopicAssignment using EntityId, which would be the contact Id.