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

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 6
    Replies
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.
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.