Ask Search:
praveen sharmapraveen sharma 

Platform Developer I Certification Maintenance (Summer '19) Challenge Code


Modify an existing batch Apex job to raise BatchApexErrorEvents

Take an existing batch Apex job class and update it to implement the Database.RaisesPlatformEvents interface. Then, add a trigger on BatchApexErrorEvent that logs exceptions in the batch job to a custom object.
Update the BatchLeadConvert class to implement the Database.RaisesPlatformEvents marker interface.

Create an Apex trigger called BatchApexErrorTrigger on the BatchApexErrorEvent SObject type. For each event record, capture the following fields and save them to the corresponding fields in a new BatchLeadConvertErrors__c record.
AsyncApexJobId: AsyncApexJobId__c
JobScope: Records__c
StackTrace: StackTrace__c
To make the trigger bulk safe, use a single DML statement to insert a list of new records at the end.
****************************************************************
Best Answer chosen by praveen sharma
praveen sharmapraveen sharma
/* batch class:-BatchLeadConvert  */



public with sharing class BatchLeadConvert implements Database.Batchable<SObject>, Database.RaisesPlatformEvents{

    private final String CONVERTED_STATUS = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel;

    public Database.QueryLocator start(Database.BatchableContext ctx){
        return Database.getQueryLocator([SELECT Id FROM Lead WHERE ConvertedContactId = null]);
    }

    public void execute(Database.BatchableContext ctx, List<Lead> records){
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
        for(Lead record:records){
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setConvertedStatus(CONVERTED_STATUS);
            lc.setLeadId(record.Id);

            leadConverts.add(lc);
        }
        Database.convertLead(leadConverts, true);
    }

    public void finish(Database.BatchableContext ctx){

    }
}
/* write trigger on  BatchApexErrorEvent  object*/

trigger BatchApexErrorTrigger on BatchApexErrorEvent (after insert) {
 list<BatchLeadConvertErrors__c> bcr= new List<BatchLeadConvertErrors__c>();
    
    for(BatchApexErrorEvent event: trigger.new){
        
        BatchLeadConvertErrors__c  evrterror= new BatchLeadConvertErrors__c ();
        
        evrterror.AsyncApexJobId__c= event.AsyncApexJobId;
        evrterror.Records__c=event.JobScope;
        evrterror.StackTrace__c=event.StackTrace;     
        bcr.add(evrterror);    
    }
    
    if(bcr.size()>0){
        
        insert bcr;
    }

}


 

All Answers

praveen sharmapraveen sharma
/* batch class:-BatchLeadConvert  */



public with sharing class BatchLeadConvert implements Database.Batchable<SObject>, Database.RaisesPlatformEvents{

    private final String CONVERTED_STATUS = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel;

    public Database.QueryLocator start(Database.BatchableContext ctx){
        return Database.getQueryLocator([SELECT Id FROM Lead WHERE ConvertedContactId = null]);
    }

    public void execute(Database.BatchableContext ctx, List<Lead> records){
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
        for(Lead record:records){
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setConvertedStatus(CONVERTED_STATUS);
            lc.setLeadId(record.Id);

            leadConverts.add(lc);
        }
        Database.convertLead(leadConverts, true);
    }

    public void finish(Database.BatchableContext ctx){

    }
}
/* write trigger on  BatchApexErrorEvent  object*/

trigger BatchApexErrorTrigger on BatchApexErrorEvent (after insert) {
 list<BatchLeadConvertErrors__c> bcr= new List<BatchLeadConvertErrors__c>();
    
    for(BatchApexErrorEvent event: trigger.new){
        
        BatchLeadConvertErrors__c  evrterror= new BatchLeadConvertErrors__c ();
        
        evrterror.AsyncApexJobId__c= event.AsyncApexJobId;
        evrterror.Records__c=event.JobScope;
        evrterror.StackTrace__c=event.StackTrace;     
        bcr.add(evrterror);    
    }
    
    if(bcr.size()>0){
        
        insert bcr;
    }

}


 
This was selected as the best answer
VANGAPALLI SAIPRANEETHVANGAPALLI SAIPRANEETH
Hi @Praveen sharma,
I am facing with an error while installing package with regarding this. Could you please help me with that.
Error while installing package
praveen sharmapraveen sharma
try in new trailhead playground
Radha Krishna SharmaRadha Krishna Sharma
Hi Praveen,
Even i am getting the same error ,I have follow the below steps, Can you please help me out.

1) In Chrome, open an incognito browser window.
2) Select and copy this link: https://login.salesforce.com/packaging/installPackage.apexp?p0=04t4P000002EMv0
In the incognito window, paste the link into the address bar to install the package.
Error
 
Radha Krishna SharmaRadha Krishna Sharma
Below link for Install Apps and Packages in Your Trailhead Playground
https://trailhead.salesforce.com/en/content/learn/modules/trailhead_playground_management/install-apps-and-packages-in-your-trailhead-playground 
VANGAPALLI SAIPRANEETHVANGAPALLI SAIPRANEETH
Hi Radha krishna,

I got the solution, Try Creating a new playground and install package in teh new playground and please be noted that us incognito window to do it succesfully.

Thanks,
Saipraneeth.
Radha Krishna SharmaRadha Krishna Sharma
Thanks a lot, Its fix for me as well
Davide GammoneDavide Gammone
public with sharing class BatchLeadConvert implements Database.Batchable<SObject>, Database.RaisesPlatformEvents{

    private final String CONVERTED_STATUS = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel;

    public Database.QueryLocator start(Database.BatchableContext ctx){
        return Database.getQueryLocator([SELECT Id FROM Lead WHERE ConvertedContactId = null]);
    }

    public void execute(Database.BatchableContext ctx, List<Lead> records){
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
        for(Lead record:records){
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setConvertedStatus(CONVERTED_STATUS);
            lc.setLeadId(record.Id);

            leadConverts.add(lc);
        }
        Database.convertLead(leadConverts, true);
    }

    public void finish(Database.BatchableContext ctx){

    }
}
Davide GammoneDavide Gammone
trigger BatchApexErrorTrigger on BatchApexErrorEvent (after insert) {
    
    List<BatchLeadConvertErrors__c> listleadConverterrs = new List<BatchLeadConvertErrors__c>();
    for (BatchApexErrorEvent evt:Trigger.new)
    {
        BatchLeadConvertErrors__c batchleadconerror= new BatchLeadConvertErrors__c();
        batchleadconerror.AsyncApexJobId__c=evt.AsyncApexJobId;
        batchleadconerror.Records__c=evt.JobScope;
        batchleadconerror.StackTrace__c=evt.StackTrace;
        listleadConverterrs .add(batchleadconerror);
    }
    insert listleadConverterrs;    
}
Carlos SiqueiraCarlos Siqueira
@Praveen Sharma:

If you had an issue with the badge, why not open a case with Salesforce?
Do you think you bring value by providing the answer to the question?
Many people spend months studying for the certification and posts like this just devaluate the merits of certification.
Harsha vardhanHarsha vardhan
Hi @praveen Sharma,
I'm getting the below error when executing. I had a run BatchLeadConvertTest and then executed. Kindly tell me what is the issue.

User-added image
Harsha vardhanHarsha vardhan
Hi @praveen Sharma.
When I'm running BatchLeadConvertTest the assertion is keep on failing.

User-added image
Rama InjetyRama Injety
Hi @Myla Sireesha,
Check whether any validation rules are present and active on the Lead object.
After disabling those BatchLeadConvertTest class ran successful for me.

Thanks,
Rama
Tania ChattopadhyayTania Chattopadhyay
Hi All,

Those who are not able to pass BatchLeadConvertTest.Please make sure that the objects (BatchApexErrorEvent, BatchLeadConvertErrors__c ) and it's fields that you are mentioning in the Trigger, are there in the Summer '19 Hands-on Challenge in your trailhead playground or the developer org that you have selected during checking challenges. Check once the playground where you are writing these classes and triggers is the one you have selected during clicking on Check Challenge. 

And also you have proper code coverage for the Trigger BatchApexErrorTrigger and BatchLeadConvertTest. 

Thanks,
Tania 
Linda PradaLinda Prada
Hey everybody, I have an account on Focus on Force for PLATFORME DEV I certification PRACTICE EXAMS.
If anyone here Has an account for ADM we can ex change our accounts (Ps : I got my certification and I'm done with the account)
Shivam TiwariShivam Tiwari
Hi all,
I am still unable to install the package. I have created a new playground and using anonymous window. Still it just wont install. Any suggestion?

Thanks,
Shivam
Evaldas ZarankaEvaldas Zaranka
Shivam use your dev org instead of playground that should solve your issue. 

How I passed: 

1. Select and copy this link: https://login.salesforce.com/packaging/installPackage.apexp?p0=04t4P000002EMv0 and install to your dev org - not to the playground. 
2. Create an Apex trigger called BatchApexErrorTrigger on the BatchApexErrorEvent SObject type. Paste into dev console code below: 
trigger BatchApexErrorTrigger on BatchApexErrorEvent (after insert) {
    
   list<BatchLeadConvertErrors__c> bcr= new List<BatchLeadConvertErrors__c>();
    
    for(BatchApexErrorEvent event: trigger.new){
        
        BatchLeadConvertErrors__c  evrterror= new BatchLeadConvertErrors__c ();
        
        evrterror.AsyncApexJobId__c= event.AsyncApexJobId;
        evrterror.Records__c=event.JobScope;
        evrterror.StackTrace__c=event.StackTrace;     
        bcr.add(evrterror);    
    }
    
    if(bcr.size()>0){
        
        insert bcr;
    }

}

3. Create the BatchLeadConvert apex class and paste the code below: 

public with sharing class BatchLeadConvert implements 
Database.Batchable<SObject>, Database.RaisesPlatformEvents{

    private final String CONVERTED_STATUS = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel;

    public Database.QueryLocator start(Database.BatchableContext ctx){
        return Database.getQueryLocator([SELECT Id FROM Lead WHERE ConvertedContactId = null]);
    }

    public void execute(Database.BatchableContext ctx, List<Lead> records){
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
        for(Lead record:records){
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setConvertedStatus(CONVERTED_STATUS);
            lc.setLeadId(record.Id);

            leadConverts.add(lc);
        }
        Database.convertLead(leadConverts, true);
    }

    public void finish(Database.BatchableContext ctx){

    }
}

4. Check if there is no errors and try to verify the challenge. 


 
Honey SachdevaHoney Sachdeva
User-added image

How to resolve the error please suggest...
Jagdish ShahaniJagdish Shahani
I am getting an error Verify the 'implements' list on BatchLeadConvert includes 'Database.RaisesPlatformEvents'
Tuhinangshu MukherjeeTuhinangshu Mukherjee
Everything is done as specified. But, I m geting error message like this ... 

There was an unexpected error in your org which is preventing this assessment check from completing: System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://ap15.salesforce.com/services/Soap/s/46.0

can anyone please help me?
Chandra Varna VChandra Varna V
@Jagdish Shahani add Database.RaisesPlatformEvents in first line of your class after Database.Batchable<SObject>

your class should be like this:

public with sharing class BatchLeadConvert implements Database.Batchable<SObject>, Database.RaisesPlatformEvents{

    private final String CONVERTED_STATUS = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel;

    public Database.QueryLocator start(Database.BatchableContext ctx){
        return Database.getQueryLocator([SELECT Id FROM Lead WHERE ConvertedContactId = null]);
    }

    public void execute(Database.BatchableContext ctx, List<Lead> records){
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
        for(Lead record:records){
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setConvertedStatus(CONVERTED_STATUS);
            lc.setLeadId(record.Id);

            leadConverts.add(lc);
        }
        Database.convertLead(leadConverts, true);
    }

    public void finish(Database.BatchableContext ctx){

    }
}
snth kumarsnth kumar
Hi All,

After the code completion with no errors, while clicking the "Check Challenge to earn..."  getting the following error message

There was an unexpected error in your org which is preventing this assessment check from completing: System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://na85.salesforce.com/services/Soap/s/46.0

Any Idea, what's wrong with my org? thanks in advance for your help.

 
Paul KaraffaPaul Karaffa
@snth kumar, I'm getting the same error:

There was an unexpected error in your org which is preventing this assessment check from completing: System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://na114.salesforce.com/services/Soap/s/46.0

If anyone has resolved this, please let me know how you did it!

What I've tried: Trailhead (new), old developer org, brand new developer org.
Tuhinangshu MukherjeeTuhinangshu Mukherjee
hey paul,  i resolved it. You need to go to setup from front end, then security -> Remote site settings there at the url paste " https://na114.salesforce.com/services/Soap/s/46.0". and in the name write ' endpoint' and save and take the challenge. it will be solved.
snth kumarsnth kumar
Gerat , that worked by creating the Remote setting "endPoint" with the URL you are getting in the error message. Thanks Mukherjee.
Paul KaraffaPaul Karaffa
Tuhinangshu/snth, thanks. That resolved the bug.
Kensley HickmanKensley Hickman
I don't see an object called BatchApexErrorEvent to put the second block of code into. What am I missing?User-added image