Friday, December 24, 2021

Update Custom Setting in Apex Salesforce

Many times we came across scenarios where we sometime need to insert/update/delete the custom setting in our apex logic. The below code will help you in that.

Syntax : 

<Custom Setting Name>   cs = new <Custom Setting Name>();
cs.<Custom Setting Field Name> = true;
insert cs;

Example 
Custom setting name is Test__c and one of the field name in custom setting is Active__c.

Test__c cs = new Test__c();
cs.Active__c = true;
insert cs;

Hierarchy Custom Settings
Now if you want to Insert a record in Hierarchy Custom Settings then,
Test__c cs = new Test__c();
cs.Active__c = true;
cs.SetupOwnerId = UserInfo.getUserId();
insert cs;

cs.SetupOwnerId-- You can add the user id or profile id.

Update Org Level Custom Settings

Test__c cs = Test__c.getOrgDefaults();
cs.Active__c = true;
update cs;

Please share the blog and leave a comment about your thoughts.
Click here for more blogs.










Tuesday, October 12, 2021

Best Practices in Apex

Best Practices in Apex Code


This will not only improve the code quality but also make it easy to enhance in future.

  1. Bulkified code - Use collection like map or list
  2. Avoid DMLs inside loops
  3. Avoid query inside loops
  4. Donot hard code in the code- Use custom labels, custom settings etc
  5. Try to keep each method short so that code is easily understandable
  6. Always put a comment for a class and for each  method
  7. Try to put Null checks where ever it us possible. Use Safe Navigation Operator.
  8. Use try catch block - This will help to capture the scenarios which gets missed in try block.

Please share the blog and leave a comment about your thoughts.
Click here for more blogs.

Sunday, October 10, 2021

How to Execute, Monitor and Abort a batch class in Salesforce

Lets understand how to execute, monitor and abort a batch


How to execute a Batch

Syntax:

<Class name> <object name> = new <class name> ();

Database.executebatch(object name, size of batch);

Difference Between Delete and Remove from Record in Notes & Attachments in Salesforce Lightning Experience

Lets understand the difference Between Delete and Remove from Record in Notes & Attachments in Salesforce Lightning Experience 

We must have seen two buttons on the menu option of file attached in Lightning Experience.

1. Delete

2. Remove from Record

sfdc


Let us understand the difference between these.

When a user clicks one the delete button the files are deleted from all the records(parent/cloned both).

On the other hand, if a user click on the remove from record then the file is deleted only from the record.

Concerns:
There is no option to remove/hide these two from the menu options.
If there is any requirement to run a logic on delete then a trigger can be implemented on content document.

Please share the blog and leave a comment about your thoughts.
Click here for more blogs.

Mostly Viewed