Skip to main content

Posts

Showing posts with the label SFDC

How to add list of records to a map?

We all know map is very much required in SFDC, As a developer we should be familiar with map uses. This post is all about to add list of records to a map. Map<Id,Account> account_map =  new Map<Id,Account>(); public static void addinglisttoMap(List<Account> accList){ for(Account acc:accList){ account_map.put(acc.id,acc); } }  Normally we used to do like above, where processing time will be more(CPU execution time will be more) because we are iterating over a for loop to construct a map.  I will tell you the best way to construct the map where we can avoid iteration.  public static void addinglisttoMap(List<Account> accList){ Map<Id,Account> account_map =  new Map<Id,Account>(accList); } Advantages-  Processing time will be very less No of character in apex class is also less.  Hope it will be helpful.  Keep coding and exploring :)  

How to split a string into a multiple parts from a common character ?

We may need to split a string into multiple parts from common characters to store it in a set which can be used later for comparison.  I came across one scenario  like ,I was getting some string which was containing three parts separated by ":"  and I had to compare with custom settings record, I was not sure which part of the string will be match with custom settings record. So I split the string and stored in the set. Below is some sample code:- String s = ' Another Person : ChildLine Volunteer Administrator : None '; String s2; set<String> set_string = new set<String>(); while(s.contains(':')){ s2 = s.substring(0,s.indexof(':')+1); set_string.add(s2.substring(0,s2.length()-1)); s = s.remove(s2); } set_string.add(s); System.debug('Printing---set_string '+set_string); Keep learning and sharing...

How contains key method works ? Does it allow partial matches or exact matches ?

This might be very simple, Even I was also thinking as the method name is "containskey" then it should return true if partial matches, unfortunately it is not like that. "Containskey" method will return true only if key is exact match. It is also case sensitive. Below is some code snippet map<string,string> test = new map<string,string>(); test.put('abc123','GM'); system.debug('Exact matches -abc123 -'+test.ContainsKey('abc123'));---- True system.debug('Partial matches-123--'+test.ContainsKey('123')); --- False system.debug('Partial matches-abc--'+test.ContainsKey('abc')); --- False system.debug('Case sensitive matches-ABC123-- '+test.ContainsKey('ABC123')); --- False

COMPILE ERROR: Invalid initial type List for Map

As we already know map will be very useful while writing code. How will we create map<Id,Account> ? Usually we will do using put method of map map<Id,Account> map_Account = new map<Id,Account>(); for(Account acc:[Select id,name from account]){     map_Account.put(acc.id,acc); }  Instead of iteration we also can do other way for batter performance. map<id,Account> account_map = new map<Id,Account>([select id,name from account]); system.debug('printing-----'+account_map ); Now lets go for little advance, instead of static query what if want to use dynamic query like below map<id,Account> account_map = new map<Id,Account>(database.query(sQuerry)); system.debug('printing-----'+account_map ); we will get an error message like " COMPILE ERROR: Invalid initial type List<SObject> for Map<Id,Account>" To address above error we can use like  map<id,sObject> account_map = new map<...

How to Invoke Opportunity Trigger when Opportunity Contact Role Created ?

As we already know Opportunity and contact are related through the junction object OpportunityContactRole and when OpportunityContRole created there wont be any update event to Opportunity. In that case how will we invoke opportunity trigger ? How will we perform some action when an opportunity is linked to contact? Possibly answer would be via workflow or trigger. Unfortunately we wont to able to write trigger on OpportunityContactRole nor workflow. Below is workaround for that scenario. Create  Visual-force page. Create a temp field on Opportunity. Add that vf page on opportunity layout. Set height and width as zero so that it wont be displayed on opportunity layout.  After OpportunityConRole created, it returns back to Opportunity layout that means inline vf page will be loaded by which one action method will be invoked to update temp field on opportunity. Once that field is updated, obliviously Opportunity Trigger will be executed.   <apex...

Workaround for COMPILE ERROR: Field is not writeable: Account.IsPartner

As part of my requirement, I had to create an account making isPartner as true, Unfortunately I got this   COMPILE ERROR: Field is not writable: Account.IsPartner  Then I first inserted that account then updated isPartner as true. Account acc = new Account(name = 'Asish'); insert acc; System.debug('Inserted account Ispartner ---'+acc.Ispartner); acc.Ispartner = true; update acc; System.debug('Inserted account---'+acc); For more info on Ispartner field--   https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_account.htm

How to find duplicates element from a list?

This is an interview question one of my colleague asked, so thought of sharing. Below is code snippet List<String> stringList = new List<String>{'One','two','Three','Four','One','two'}; set<String> setString = new set<String>(); set<String> duplicatesetString = new set<String>(); for(String s:stringList ){     if(!setString.add(s)){       duplicatesetString .add(s);    } } System.debug('duplicatelistString----'+duplicatesetString ); System.debug('duplicatelistString----'+duplicatesetString.add('ten'));

Closing the child window and Refreshing parent window.

As we all know there are many window properties are available so it is quite easy even i had thought same thing but while implementing i faced many issue so i thought of sharing . Below are few window property which are useful. ·          window.opener  refers to the window that called  window.open( ... )  to open the window from which it's called ·          window.parent  refers to the parent of a window in a  <frame>  or  <iframe> ·          window.top  refers to the top-most window from a window nested in one or more layers of  <iframe>  sub-windows. Lets say there is button on account "Change Account Name" ,On clicked one popup is opening and which would allow you to change Account name and one button "Save" is there in popup window. On clicked child window will be closed and acco...

How to schedule one class to run in every 1 mins?

Sometimes we need to run a class in every minute to do some operation.     // This section of code will schedule the next execution 1 minutes from now    global class Scheduling_Svc_WS_CreateBatch implements Schedulable{  // Execute method     global void execute(SchedulableContext SC) {          datetime nextScheduleTime = system.now().addMinutes(1);          string minute = string.valueof(nextScheduleTime.minute());          string second = string.valueof(nextScheduleTime.second ());          string cronvalue = second+' '+minute+' 0-23 * * ?' ;          string jobName = 'selfReschedulingClass ' +nextScheduleTime.format('hh:mm');          Scheduling_Svc_WS_CreateBatch p = new Scheduling_Svc_WS_CreateBatch();           system.schedule(jobName, cronvalu...