• p sonwane
  • NEWBIE
  • 30 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 9
    Replies
I Have Cleared PD2 certification on 5-oct-2023, also completed all the modules and superbadges required for certification. i have raised case as well but didnt received any certificate yet. 
Hello Team , 
I have implemented email send functuonality using lightning email template . my code is working fine . i checked in log also everything is correct but i am not getting email . below is my code .


public class ApplicationService{
    
    public static void createApplications(){
        List<Application__c> appList2= new List<Application__c>();
        List<Application__c> appList=[Select Id, Status__c,Contact__r.Email,Contact__r.Id, Application_Status__c 
                                          from Application__c where Status__c = 'Publish in Progress' 
                                          AND(Application_Status__c ='Admitted' or Application_Status__c ='Not Admitted')];
                                          
        EmailTemplate TemplateId= [SELECT Id, Body FROM EmailTemplate WHERE Name = 'Learner_Approved_Template'];
        for(Application__c app:appList){
            List<String> toAddressList=new List<String>();
            toAddressList.add(app.Contact__r.Email);
                        System.debug('**Contact Email:'+app.Contact__r.Email);
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(toAddressList);
            email.setTargetObjectId(app.Contact__r.id); //contact.id
            email.setWhatId(app.Id);
            email.setSaveAsActivity(false);
            email.setUseSignature(false);
            
            if(app.Application_Status__c =='Admitted'){
                email.setTemplateId(templateId.Id);
                email.setHTMLBody(templateId.Body);
            }
            else if(app.Application_Status__c =='Not Admitted'){
                //EmailTemplate TemplateId= [SELECT Id, Body FROM EmailTemplate WHERE Name = 'LXP_Learner_Rejection_Template'];
                email.setTemplateId(templateId.Id);
                email.setHTMLBody(templateId.Body);
            }
            
            Messaging.SendEmailResult[] sendResults = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ email });
            System.debug('sendResults**'+sendResults);
            if(sendResults[0].isSuccess()) {
                app.Status__c ='Published';
                system.debug('Email sent');
                appList2.add(app);
            } 
            else{
                system.debug('Email not send');} 
        }
        if(appList2.size()>0){
            update appList2;
        }
    }
}
checkbox should be checked if the logged in user sales org and Product Sales org matches. sales org is picklist feild in user object and multislect feild in product object . i have written below formula but getting error
formula :
IF($User.Sales_Org__c = Sales_Org__c , true, false)

error : 
 Error: Field Sales_Org__c is a picklist field. Picklist fields are only supported in certain functions. Tell me more
i need help on fromula field , i have same feild that is sales org on two object . on one object it is picklist and other it is multislelect picklist .checkobox should be return true if both value is matching
Apex code
public class CountContactHandler {
    public static void CountContactHelper(List<contact> newcontact, List<contact> oldcontact){
        set<id> accIds= new set<id>();
        if(newcontact != null){
            for(Contact c : newcontact){
                if(c.AccountId!=null){
                    accids.add(c.accountid);
                }
            }      
        }
         
        if(oldcontact != null){
            for(Contact c : oldcontact){
                accids.add(c.accountid);
            }
        }
         
        List<Account> accList = [Select Id, NoofContacts__c, (Select id from Contacts) from Account where Id IN :accIds];
         
        if(accList!=null){
            for(Account acc : accList){
                acc.NoofContacts__c = acc.Contacts.size();
            } 
        }
         
        if(!accList.isEmpty()){
            update accList;
        }
    } 
}




Test class : 

@isTest
public class CountContactHandlerTest {
    @isTest
    public static void CountContactHelpertest(){ 
        //Create sample data 
        List<Account> accList = new List<Account>();
        
        for(Integer i=1;i<=5;i++){
            Account acc = new Account();
            acc.Name='Test'+i;
            acc.Industry='Energy';
            accList.add(acc);
        } 
        
        // insert data
        Test.startTest();
        insert accList;
        Test.stopTest();
        List<Contact> conList = [Select Id from Contact where AccountId =:accList[0].Id];
        System.assert(conList!=null, 'Contact is not created');
        
    }
}
 
global class SendEmailToAccountOwner implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT Id,Name,Account__r.id,Account__r.name,Account__r.Owner.email FROM Training__c where CreatedDate = Today';
       System.debug('** query'+query);
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc , List<Training__c> batch){
        List<Contact> conlist= new List<Contact>();
        
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        List<String> sendTo = new List<String>();
        
        for (Training__c tr : batch) {
            String s ='';
            sendTo.add(tr.Account__r.Owner.email);
            System.debug('send email' +tr.Account__r.Owner.email);
            
            Contact con = new Contact();
            con.lastname= tr.name;
            con.AccountId= tr.Account__c;
            conlist.add(con);
            System.debug('conlist ** '+conlist);
            
            s+=Con.lastname +' has been created for'+ tr.Account__r.name;
            System.debug('s**'+s);
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');
            mail.setSubject('Contact Created  for Account Reated to Training');
            String body = 'Account Related Contacts==> '+s.removeEnd(',');
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
        }
        insert conlist;
        Messaging.SendEmail(mails);  
    }
    global void finish(Database.BatchableContext bc){
        
    }
}

Create Custom Object  named "Training" with Fields Name, Email, Phone Number, Age, Account and Status. 
Name, Account are required fields and Account should be lookup with Account Object.
Create a Batch class to verify the records created in last 24 hours in training object and if any, create a respective contact record and send out an email to Account Owner saying that related contact is created.
I Have Cleared PD2 certification on 5-oct-2023, also completed all the modules and superbadges required for certification. i have raised case as well but didnt received any certificate yet. 
Hello Team , 
I have implemented email send functuonality using lightning email template . my code is working fine . i checked in log also everything is correct but i am not getting email . below is my code .


public class ApplicationService{
    
    public static void createApplications(){
        List<Application__c> appList2= new List<Application__c>();
        List<Application__c> appList=[Select Id, Status__c,Contact__r.Email,Contact__r.Id, Application_Status__c 
                                          from Application__c where Status__c = 'Publish in Progress' 
                                          AND(Application_Status__c ='Admitted' or Application_Status__c ='Not Admitted')];
                                          
        EmailTemplate TemplateId= [SELECT Id, Body FROM EmailTemplate WHERE Name = 'Learner_Approved_Template'];
        for(Application__c app:appList){
            List<String> toAddressList=new List<String>();
            toAddressList.add(app.Contact__r.Email);
                        System.debug('**Contact Email:'+app.Contact__r.Email);
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(toAddressList);
            email.setTargetObjectId(app.Contact__r.id); //contact.id
            email.setWhatId(app.Id);
            email.setSaveAsActivity(false);
            email.setUseSignature(false);
            
            if(app.Application_Status__c =='Admitted'){
                email.setTemplateId(templateId.Id);
                email.setHTMLBody(templateId.Body);
            }
            else if(app.Application_Status__c =='Not Admitted'){
                //EmailTemplate TemplateId= [SELECT Id, Body FROM EmailTemplate WHERE Name = 'LXP_Learner_Rejection_Template'];
                email.setTemplateId(templateId.Id);
                email.setHTMLBody(templateId.Body);
            }
            
            Messaging.SendEmailResult[] sendResults = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ email });
            System.debug('sendResults**'+sendResults);
            if(sendResults[0].isSuccess()) {
                app.Status__c ='Published';
                system.debug('Email sent');
                appList2.add(app);
            } 
            else{
                system.debug('Email not send');} 
        }
        if(appList2.size()>0){
            update appList2;
        }
    }
}
checkbox should be checked if the logged in user sales org and Product Sales org matches. sales org is picklist feild in user object and multislect feild in product object . i have written below formula but getting error
formula :
IF($User.Sales_Org__c = Sales_Org__c , true, false)

error : 
 Error: Field Sales_Org__c is a picklist field. Picklist fields are only supported in certain functions. Tell me more
Apex code
public class CountContactHandler {
    public static void CountContactHelper(List<contact> newcontact, List<contact> oldcontact){
        set<id> accIds= new set<id>();
        if(newcontact != null){
            for(Contact c : newcontact){
                if(c.AccountId!=null){
                    accids.add(c.accountid);
                }
            }      
        }
         
        if(oldcontact != null){
            for(Contact c : oldcontact){
                accids.add(c.accountid);
            }
        }
         
        List<Account> accList = [Select Id, NoofContacts__c, (Select id from Contacts) from Account where Id IN :accIds];
         
        if(accList!=null){
            for(Account acc : accList){
                acc.NoofContacts__c = acc.Contacts.size();
            } 
        }
         
        if(!accList.isEmpty()){
            update accList;
        }
    } 
}




Test class : 

@isTest
public class CountContactHandlerTest {
    @isTest
    public static void CountContactHelpertest(){ 
        //Create sample data 
        List<Account> accList = new List<Account>();
        
        for(Integer i=1;i<=5;i++){
            Account acc = new Account();
            acc.Name='Test'+i;
            acc.Industry='Energy';
            accList.add(acc);
        } 
        
        // insert data
        Test.startTest();
        insert accList;
        Test.stopTest();
        List<Contact> conList = [Select Id from Contact where AccountId =:accList[0].Id];
        System.assert(conList!=null, 'Contact is not created');
        
    }
}
 
global class SendEmailToAccountOwner implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT Id,Name,Account__r.id,Account__r.name,Account__r.Owner.email FROM Training__c where CreatedDate = Today';
       System.debug('** query'+query);
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc , List<Training__c> batch){
        List<Contact> conlist= new List<Contact>();
        
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        List<String> sendTo = new List<String>();
        
        for (Training__c tr : batch) {
            String s ='';
            sendTo.add(tr.Account__r.Owner.email);
            System.debug('send email' +tr.Account__r.Owner.email);
            
            Contact con = new Contact();
            con.lastname= tr.name;
            con.AccountId= tr.Account__c;
            conlist.add(con);
            System.debug('conlist ** '+conlist);
            
            s+=Con.lastname +' has been created for'+ tr.Account__r.name;
            System.debug('s**'+s);
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');
            mail.setSubject('Contact Created  for Account Reated to Training');
            String body = 'Account Related Contacts==> '+s.removeEnd(',');
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
        }
        insert conlist;
        Messaging.SendEmail(mails);  
    }
    global void finish(Database.BatchableContext bc){
        
    }
}

Create Custom Object  named "Training" with Fields Name, Email, Phone Number, Age, Account and Status. 
Name, Account are required fields and Account should be lookup with Account Object.
Create a Batch class to verify the records created in last 24 hours in training object and if any, create a respective contact record and send out an email to Account Owner saying that related contact is created.
Hi All,

I receive the following error when attempting to check the challenge for Apply Unit of Work

Challenge Not yet complete... here's what's wrong: 
The 'challangeComplete' method in the 'UnitOfWorkTest' class has not successfully passed all tests. Ensure that you run the tests and it passes successfully before attempting this challenge again.

However, when I run my code in Developer Console, my test passes.  Any ideas on the problem?  Here is the code:

@isTest
public class UnitOfWorkTest {
    @isTest static void challengeComplete(){
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );
        
        for (Integer i=0 ; i<100 ; i++) {
            Account a = new Account(Name= 'Test' + i);
            uow.registerNew(a);
            
            for (Integer j=0 ; j<5 ; j++) {
                Contact c = new Contact(LastName = 'Test'+i + ' ' +j);
                uow.registerNew(c, Contact.AccountId, a);
                
                Note n = new Note(Body='Test '+i + '' + j, Title='Test'+i+j);
                uow.registerRelationship(n, Note.ParentId, a);
                uow.registerNew(n, Note.ParentId, a);
            }
        }

        uow.commitWork();
 
        fflib_SObjectUnitOfWork uow2 = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );        
        for (Account a : [SELECT Id, Name, (SELECT Id, LastName FROM Contacts), (SELECT Id, ParentId, Title, Body FROM Notes) FROM Account]) {
            a.Name = 'Test';
            uow2.registerDirty(a);
            
            Integer i = 0;
            for (Contact c : a.Contacts) {
                c.LastName = 'Test';
                uow2.registerDirty(c);
                
                a.Notes[i].Body='Test';
                uow2.registerDirty(a.Notes[i]);
                i++;
            }
        }        
        
        test.startTest();
        uow2.commitWork();
        test.stopTest();
        
        System.assertEquals(100, [Select Id from Account].size());
        System.assertEquals(500, [Select Id from Contact].size());
        System.assertEquals(500, [Select Id from Note].size());
    }
}