Showing posts with label Apex. Show all posts
Showing posts with label Apex. Show all posts

Sunday, December 8, 2024

Handling bulk data imports in Salesforce



Handling bulk data imports in Salesforce effectively requires leveraging Salesforce tools and adhering to best practices to manage performance, maintain data integrity, and comply with platform limits. Here’s a step-by-step approach:

1. Prepare for the Import

Understand the Data

  • Analyze the dataset to be imported, focusing on volume, relationships, and dependencies (e.g., parent-child relationships like Accounts and Contacts).
  • Check for duplicate records to avoid conflicts.

Data Cleansing

  • Clean data to ensure consistency (e.g., standardize date formats, remove extra spaces).
  • Validate data for mandatory fields and avoid invalid entries.

Data Mapping

  • Map fields in the source data to Salesforce fields.
  • Prepare a clear mapping document to avoid errors during import.

Backup Data

  • Back up existing Salesforce data before initiating the import using tools like Data Export or third-party backup solutions.

2. Choose the Right Tool

Select the appropriate tool based on the volume and complexity of the import:

Tools for Bulk Import

  • Data Loader: Best for handling large volumes of data (up to 5 million records at a time).
  • Data Import Wizard: Suitable for small to medium volumes of data with simple configurations (up to 50,000 records).
  • Salesforce Bulk API: Ideal for very large datasets. It processes data asynchronously in batches.
  • Third-Party Tools: Use tools like Informatica, Mulesoft, or Talend for complex or cross-platform data imports

3. Configure the Import

Optimize for Performance

  • Use Bulk API if importing millions of records. It processes data in chunks, optimizing API calls.
  • For real-time needs or small datasets, REST API or SOAP API may be sufficient.

Batching Data

  • Split large datasets into smaller batches to avoid hitting governor limits and reduce processing time.

Error Logging

  • Enable detailed logging to track and resolve import errors.

Disable Automation Temporarily

  • Disable workflows, triggers, and validation rules temporarily to avoid unnecessary processing during the import.

4. Perform the Import

Test the Process

  • Conduct a test run in a sandbox environment with a subset of the data to verify mappings and resolve issues.

Execute the Import

  • Load data into Salesforce using the selected tool.
  • Follow a logical order:
    • Parent objects (e.g., Accounts) first.
    • Child objects (e.g., Contacts, Opportunities) next.

Monitor Progress

  • Monitor the progress of the import through job statuses in Salesforce or the tool's interface.

5. Post-Import Activities

Validate Imported Data

  • Run reports to compare imported data with source data to ensure accuracy.
  • Verify key relationships and calculated fields.

Re-enable Automation

  • Re-enable workflows, triggers, and validation rules.
  • Test to ensure that automation works correctly with the imported data.

Address Errors

  • Review error logs to resolve failed records and re-import them if needed.

6. Best Practices for Bulk Imports

  • API Limits Awareness: Monitor API limits to ensure you don’t exceed daily quotas.
  • Avoid Duplicates: Use Salesforce Duplicate Rules or external tools to detect and prevent duplicates.
  • Data Quality Tools: Consider using tools like DemandTools for deduplication and cleansing before importing.
  • Stakeholder Communication: Inform stakeholders about changes to avoid disruptions.
  • Audit Trail: Maintain documentation of the import process for compliance and troubleshooting.

By preparing data properly, selecting the right tool, and following these best practices, you can handle bulk data imports in Salesforce with high efficiency and accuracy.

4o

 

Saturday, December 30, 2023

How TO SENDING SALESFORCE REPORT AS ATTACHMENT

 Here we will show number of ways we can send the report as an attachment in email.

public class ReportsController{

        //Generate report Based on parameter

    public void generateReport(string reportage)

    {

        try

        {

            List <Report> reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName =:reportName];

            if(reportList.size()>0)

            {

                String reportId = (String)reportList.get(0).get('Id');                

                //Get Report Name

                string reportName=(String)reportList.get(0).get('Name');                

                //get SalesForce instance Url

                String instanceName = URL.getSalesforceBaseUrl().toExternalForm();                

                string url=instanceName+'/servlet/PrintableViewDownloadServlet?isdtp=p1&reportId='+reportId;                

                ApexPages.PageReference objPage = new ApexPages.PageReference(url);

                Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();               

                Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();

                objMsgEmailAttach.setFileName(reportName+'.csv');

                objMsgEmailAttach.setBody(objPage.getContent());

                objMsgEmailAttach.setContentType('text/csv');

                email.setSubject(reportName);                

                List<Messaging.EmailFileAttachment> attach=new List<Messaging.EmailFileAttachment>();

                attach.add(objMsgEmailAttach);

                email.setFileAttachments(attach);  

                 EmailService service=new EmailService(email);

                service.body='Hello, <br/><br/> Here is attached '+reportName+'.<br/><br/>Thank You.<br/>Admin';

                service.isHtml=true;

                service.toAddresses=new List<string>();

                service.toAddresses.add('info@technoeric.com');   

                service.displayName='Technoeric';

                service.subject=reportName;

                service.sendMail();

            }

        }

        catch(Exception ex)

        {

            system.debug(ex.getMessage());

        }

    }

  

}

Friday, December 29, 2023

How to HANDLE HEAP SIZE By APEX CODE OPTIMIZATION


Load size is how much memory expected to store the different articles, factors, and condition of the Summit exchange in memory. This size is covered at 6MB for simultaneous activities and is multiplied to 12 MB for offbeat cycles. In the event that the size is expanded past this covered size, we get the mistake "Apex size excessively huge". This post will drill down ways of dealing with Heap size for Apex code advancement.


 1. Bring just required records

In the above model, we are returning the total request list utilizing SOQL which probably won't be needed. Utilize some channel models so that main required records will be gotten. in the event that we have less records, it will take lesser memory.


We can utilize customerid as a channel standards which will channel records and will diminish heap size. Look at our other post Use Channel in SOQL for the advantages of sifting records.


2. Get just required fields

In the above model, we have brought such countless fields yet we are not utilizing a large portion of them in the code. That multitude of gotten fields will find opportunity to recover from the data set and will hold memory also when saved in any factor like requests in the above model.


We ought to just get the necessary fields from the data set. It will lessen the possibilities of Heap size mistakes in addition to the code will work quicker too.


3. Use for circle for SOQL

In the above model, we have performed gotten information from the data set and saved total information in orders variable. This variable is utilized exclusively in for circle and there could be no other use. We can stay away from such sort of SOQL and on second thought of this, we can straightforwardly utilize it in for circle.


List<string> orderNumbers=new List<string>();

for(AcctSeedERP__Sales_Order__c ord:[Select id,AcctSeedERP__Customer__r.Name from AcctSeedERP__Sales_Order__c])

{

    orderNumbers.add(ord.Name);

}

system.debug( 'Stack Status ' + (limits.getHeapSize()>limits.getLimitHeapSize()));

system.debug( ': Stack size is ' + limits.getHeapSize() + ' upheld is ' + limits.getLimitHeapSize());

view rawHeapIssueFix.apxc facilitated with ❤ by GitHub

One more significant advantage of involving SOQL in for circle is it will be getting just 200 records in one go and this manner SOQL limit exemptions won't be tossed. So utilize this idea any place you can apply it in zenith code. I have eliminated all unused fields in the above model too, this will likewise diminish memory size.


4. Stay away from system.debug

Stay away from the System.Debug in code. It can make issues underway. Checkout our other post for Enhance Code by Impairing Troubleshoot Mode


5. Stay away from class-level factors and utilize neighborhood variable

Keep away from class-level factors as they will be put away in memory until the total exchange for that class isn't done. It will be smarter to utilize a nearby factor that will consequently deliver after that block execution.


6. Rather than a major strategy utilize more modest techniques

According to best practice, we ought to make more modest techniques around 20 lines. We ought to keep away from greater techniques as they will make intricacy and reliance in code. A variable made in the strategy will be alive till the technique execution so in the event that we have more modest strategies, the variable will be cleaned early soon after strategy execution so our store size will have more space to hold different items.


On account of a greater technique, the variable will be in the load until the total strategy isn't executed, it will take some space and may be a justification for the store size special case. So better will make more modest strategies.


7. Free memory when not being used

Any place we are utilizing objects to store information, attempt to eliminate them from memory following use. We shouldn't pass on everything for the framework to clean. Make a training to clean it right away and free some memory from the store. This will help in putting away different information for usefulness. In the above model, we have an orders variable that can be cleaned after for circle. Dole out invalid to clean object memory.


8. Utilize Asynchronous Techniques

Some of the time we really want to do a few weighty tasks and it could have to utilize a lot of memory, all things considered, we can utilize nonconcurrent strategies so the store size will be expanded to 12MB and we can finish our pinnacle exchange. We can utilize Future callouts, queueable strategies, or bunch occupations in light of the prerequisite. We can utilize this methodology for callout, document dealing with, and so on.

Thursday, December 14, 2023

Salesforce exceptions and possible solutions

 Salesforce, being a complex platform, can encounter various exceptions or errors. Here are some common Salesforce exceptions and possible solutions:

  1. NullPointerException:

    • Cause: Trying to access or manipulate an object that is null.
    • Solution: Ensure that the object is initialized before performing any operations on it. Use conditional statements to check for null values.
  2. QueryException:

    • Cause: Issues with SOQL (Salesforce Object Query Language) queries.
    • Solution: Verify the syntax of your SOQL query. Ensure that field names, object names, and relationships are accurate. Also, be mindful of governor limits related to queries.
  3. DMLException:

    • Cause: Errors during Database Manipulation Language operations (insert, update, delete, undelete).
    • Solution: Wrap DML operations in try-catch blocks to catch exceptions. Check for validation rules, triggers, and workflow rules that might be causing the issue.
  4. LimitException:

    • Cause: Exceeding governor limits imposed by Salesforce.
    • Solution: Review and optimize your code to avoid hitting limits. Use asynchronous processing, bulk processing, and implement best practices to minimize resource consumption.
  5. CalloutException:

    • Cause: Errors during web service callouts.
    • Solution: Ensure that the external service is reachable and the request format is correct. Handle exceptions and implement retry mechanisms for callouts.
  6. Apex CPU Time Limit Exceeded:

    • Cause: Apex code exceeding the CPU time limit.
    • Solution: Optimize your code, use bulk processing, and consider moving complex operations to asynchronous processing (such as batch Apex or future methods).
  7. System.LimitException: Too many SOQL queries: 101:

    • Cause: Exceeding the limit of 100 SOQL queries in a single transaction.
    • Solution: Optimize your code to reduce the number of queries. Consider using relationships and aggregate queries to retrieve data more efficiently.
  8. System.QueryException: Non-selective query against large object type:

    • Cause: Performing a non-selective query on a large object.
    • Solution: Index fields used in WHERE clauses to make queries selective. Consider using custom indexing for large objects.
  9. System.DmlException: Insert failed: FIRST_EXCEPTION: ...:

    • Cause: Validation rule, trigger, or workflow rule preventing the record from being inserted.
    • Solution: Identify and address the issue in your validation rules, triggers, or workflow rules that is blocking the record insertion.
  10. System.AsyncException: Future method cannot be called from a future or batch method:

    • Cause: Trying to call a future method from another future method or batch method.
    • Solution: Ensure that you are not making asynchronous calls from within asynchronous methods. Restructure your code to avoid such scenarios.

Thursday, June 2, 2022

How to Fire Platform Events from Batch Apex

Today we will discuss about, How to Fire Platform Events from Batch Apex

Batch class needs to implement "Database.RaisesPlatformEvents" interface in order to fire platform event.

global class SK_AccountProcessBatch implements Database.Batchable<sObject>,Database.RaisesPlatformEvents{

   //batch logic

}

below is the sample code for the same:-

global with sharing class PlatformEventRaise implements Database.Batchable<SObject>, Database.RaisesPlatformEvents{

    // class implementation

    global Database.QueryLocator start(Database.BatchableContext BC){

        return Database.getQueryLocator('Select Id ,Name,Rating,Industry, BillingAddress,BillingStreet,BillingCity, BillingCountry, BillingPostalCode,BillingState,Phone from Account  where BillingStreet!=NULL');

    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){

         List<Account> accs =(List<Account>) scope ; 

        List<Cloud_News__e> cnewList = new List<Cloud_News__e>();

        for(Account a : accs){

            // Create an instance of the event and store it in the newsEvent variable

            Cloud_News__e newsEvent = new Cloud_News__e(

                Location__c=a.BillingStreet, 

                Urgent__c=true, 

                News_Content__c=a.BillingStreet);

              cnewList.add(newsEvent) ;

         }      

// Call method to publish events

        Database.SaveResult sr =  EventBus.publish(cnewList);

        // Inspect publishing result 

        if (sr.isSuccess()) {

            System.debug('Successfully published event.');

        } else {

            for(Database.Error err : sr.getErrors()) {

                System.debug('Error returned: ' +  err.getStatusCode() +' - ' + err.getMessage());

            }

        }       

    }    

    global void finish(Database.BatchableContext BC){

    }

}

Monday, May 30, 2022

Difference between SObject and Platform Events

 

Difference between SObject and Platform Events

SObjects__cPlatform_Events__e
DMLs (Insert, Update, Delete)Publish (Insert only)
SOQLStreaming API
TriggersSubscribers
Parallel context executionGuaranteed order of execution

Considerations :-

  1. Platform event is appended with__e suffix for API name of the event.
  2. You can not query Platform events through SOQL or SOSL.
  3. You can not use Platform in reports, list views, and search. Platform events don’t have an associated tab
  4. Published platform events can’t be rolled back.
  5. All platform event fields are read-only by default
  6. Only after insert Triggers Are Supported
  7. You can access platform events both through API and declaratively
  8. You can control platform events though Profiles and permissions

Summary

Platform events simplify the process of communicating changes and responding to events. Platform events can be used to Overcome Salesforce Governor Limits.