Salesforce | All You Have to Know About SOSL and Associated Data in Apex


On this weblog, we’ll focus on what are associated data in Salesforce and what we understood by SOSL in apex

What are Associated Data in Apex?

Inserting Associated Data 

Instance :

Account acc = new Account(Identify=’abc’, Telephone = ‘123456’); 
insert acc; 
Contact con = new Contact(Account=acc.Id, FirstName = ‘abc’ , LastName = ‘xyz’, Telephone = ‘12345’); 
Insert con;

Updating Associated Data 

Contact con = [SELECT Id, Name,Account.Phone FROM Contact WHERE Name = ‘abc’ AND AccountId!=Null AND Account.Name = ‘Account1’];  
Con.Telephone=’111’; 
Con.Account.Telephone=’2222’; 
replace con; 
replace con.Account;

dont miss out iconDo not forget to take a look at: Learn All About Salesforce Apex Programming

Now, let’s transfer to SOSL!

What’s SOSL in Apex?

  • SOSL stands for Salesforce Object Search language. 
  • It’s used to carry out a textual content search in data. 
  • We are able to use SOSL to go looking fields throughout a number of sObjects data. 
  • SOQL is used to retrieve data for a single object whereas use SOSL to go looking fields throughout a number of objects. 

Syntax 

Discover ‘SearchQuery’ [IN SearchGroup] [RETURNING ObjectsAndFields];

What’s a SearchQuery?

  • Single Phrase: It ought to be enclosed in single quotes. 
  • Phrase: It’s having a number of phrases and ought to be enclosed in double quotes. 

What’s a SearchGroup?

SearchGroup is optionally available. 

  • The default is ALL FIELDS. 
  • You’ll be able to select from the next search teams: 
  • ALL FIELDS 
  • NAME FIELDS 
  • EMAIL FIELDS 
  • PHONE FIELDS 
  • SIDEBAR FIELDS 

ObjectsAndFields 

  • ObjectsAndFields is optionally available. 
  • It’s the info to return within the search end result – a listing of a number of sObjects and inside every sObject, record of a number of fields, with optionally available values to filter in opposition to. 
  • If not specified the search end result comprise the IDs of all objects discovered. 

dont miss out iconTake a look at one other superb weblog by Bhawana right here: All You Need to Know About Database Class Methods to Perform DML Operations

Execute Nameless Window 

Listing<Listing<sObject>> searchList = [FIND ‘Cloud’ IN ALL FIELDS RETURNING Account(Name),Contact(FirstName, Lastname, Email)];

Question Editor 

FIND {Cloud} IN ALL FIELDS RETURNING Account(Identify), Contact(FirstName, LastName, E-mail)];

For instance:

Listing<Listing<sObject>> searchList = [FIND ‘Abc’ IN ALL FIELDS RETURNING Account(Name), Contact(FirstName,LastName,Email)]; 
Listing<Account> accList = new Listing<Account>(); 
Listing<Contact> conList = new Listing<Contact>(); 
accList = (Listing<Account>) searchList[0]; 
conList = (Listing<Contact>) searchList[1]; 
for(Account acc : accList)
{ 
    System.debug(‘Identify =>' + acc.Identify); 
} 
for(Contact con : conList)
{ 
    System.debug(con.FirstName + ‘ ‘ + con.LastName); 
}

So, that is all about associated data in apex and SOSL. I hope this info is useful to you. 





Source link

Thanks for Reading

Enjoyed this post? Share it with your networks.

Leave a Feedback!