• Dosbol T
  • NEWBIE
  • 69 Points
  • Member since 2020

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 15
    Replies
I want to query all fields in the start method of the batch class. Below is my code which is giving error(also giving error on type casting). Can you please suggest if there is any way to achieve this?
global class BatchClass implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) { 
    Map<String, Schema.SObjectField> CaseMap = Case.sObjectType.getDescribe().fields.getMap();        
    Set<String> setFieldNames = CaseMap.keySet();
    list<String> lstFieldNames = new List<String>(setFieldNames);                
    String query = Database.query('SELECT ' + String.join(lstFieldNames, ',') + ' FROM Case limit 2');
    return Database.getQueryLocator(query);
    }
}
 
Hi there, I am trying to write a code which would help me to grant read accesses to the fields on Permission sets. Please help me with this code below. It does not show any error but also it is not working.
Any advice, help will be highly appreciated.
public class GrantFieldAccess {

    public void grantFieldReadAccess() {
        List<String> fieldAPINames = new List<String>{
            'CustomeField1',
            'CustomeField2'
        };

        List<PermissionSet> permissionSets = [SELECT Id FROM PermissionSet WHERE Name IN ('PermissionSet1', 'PermissionSet2', 'PermissionSet3' )];

        List<FieldPermissions> fieldPermissionsList = new List<FieldPermissions>();

        for (PermissionSet ps : permissionSets) {
            for (String fieldAPIName : fieldAPINames) {
                FieldPermissions fieldPerm = new FieldPermissions(
                    SobjectType = 'Account',
                    Field = fieldAPIName,
                    ParentId = ps.Id,
                    PermissionsRead = true
                );
                fieldPermissionsList.add(fieldPerm);
            }
        }

        try {
            upsert fieldPermissionsList;
            System.debug('Field access granted successfully.');
        } catch (Exception ex) {
            System.debug('An error occurred: ' + ex.getMessage());
        }
    }
}

 
Hello, please help me with this. 
Validation Rule: prevents Case to be closed if the CasePicklist1__c is not filled AND CasePicklist2__c = Value 1 & Value 2.
AND(
    ISCHANGED(Status),
    ISPICKVAL(Status,"Closed"),
      OR(ISPICKVAL( CasePicklist2__c ,"Value1")),
        ISPICKVAL( CasePicklist2__c  ,"Value2"),
        ISBLANK(TEXT( CasePicklist1__c ))
)

Much appreciated.
Hello,
I am trying to update all my leads which were created before I have added a new field. Now, after having all deployed onto UAT I need to perform post deployment action. But having an issue with the code, what is wrong with it?
For (List <Event> eventList: [SELECT Id, WhoId, TypeOf     
 WHO 
 WHEN Lead 
 THEN FirstInterviewdate__c 
 END, Subject, ActivityDate 
 FROM Event 
 WHERE Subject = 'First Interview'
AND
 WHOID != null
 LIMIT 200]){
    List<Lead> leadlist = new List<Lead>(); 
    For(Event evt: eventList){
        leadlist.add(new Lead (Id=evt.WhoId, FirstInterviewdate__c = evt.ActivityDate));
      }
    update leadList;
}
and the issue is:

User-added image
 
Please help with this test class, I cant get 100%, dont know how to improve it. Thanks,
@isTest
private class AutoPopDateTest {
  @isTest
  private static void AutoPopDate() {
    Billing__C bil = new Billing__C();
    bil.Name = 'NewBill';
    bil.Billing_Date__c = system.today();
    bil.Accounting_date__c = bil.Billing_Date__c.addDays(20);
    try {
      insert bil;
    } catch (NullPointerException n) {
    }
  }
}
and trigger:
trigger AutoPopAccPerDate on Billing__c(before insert, before update) {
   for (Billing__c billing : Trigger.new) {
    if (billing.Accounting_Date__c == null) {
      billing.Accounting_Date__c = billing.Billing_Date__c.adddays(30); // '30 Days' - Depends on the terms.
    }
  }
}

Appreciate any support!
 
what is wrong with this code? It says - "Arithmetic expressions must use numeric arguments (4:43)",
  Please help.
trigger AutoPopAccPerDate on Billing__c(before insert, before update) {
  for (Billing__c billing : Trigger.new) {
    if (billing.Accounting_Date__c == null) {
      billing.Accounting_Date__c = Billing__c.Billing_Date__c + 30; 
    }
  }
}

 
Dear Community, would you please help me with creating a trigger for scheduling email reminders for overdue invoices with the the associated invoices being generated? Will appreciate any support. Thanks in advance. 
How to get account status via contact from case , I want Account status (field in account object ) Case >contact>account>account status using soql query
Hi, I need help.
I received the below error
EXCEPTION_THROWN [22]|System.QueryException: unexpected token: s0000014qrPAAQ

Here is the Apex code
public with sharing class toApproved {
    @AuraEnabled(cacheable=true)
    //public static list<ProcessInstanceWorkitem> getItemApprove(){
    public static list<ProcessInstanceWorkitem> getItemApprove(id ActorId){
        id actorid2 = ActorId;
        system.debug('Actor: '+ActorId);
        system.debug('Actor2: '+actorid2);

        String condition = 'ActorId = '+ActorId;
        system.debug('condition: '+condition);

        string query = 'select ProcessInstance.TargetObject.name, CreatedDate, CreatedBy.name, ActorId, Actor.name from ProcessInstanceWorkitem where ' +condition + ' order by CreatedDate limit 100';
        system.debug('query: '+query);

        list<ProcessInstanceWorkitem> records = Database.query(query);
        system.debug('records '+records);

        return records;
    }
}


 
  • January 06, 2023
  • Like
  • 0
trigger getRelatedOpportunitiesCount on Opportunity (after insert,after update, after delete, after undelete){
    Set<Id> accID = new Set<Id>();
    if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
        for(Opportunity opp : Trigger.New){
            accID.add(opp.AccountId);
        }
        updateAcc(accID);
    }
    else if(Trigger.isDelete){
        for(Opportunity opp : Trigger.old){
            accID.add(opp.AccountId);
        }
        updateAcc(accID);
    }
    private void updateAcc(Set<Id> accIds){
        List<Account> accList = [select id, Close_Won_Count__c from Account where Id in :accIds];
        List<Opportunity> oppsList = [select id from Opportunity where AccountId in :accIds and StageName='Closed Won'];
        for(Account a : accList){
            a.Close_Won_Count__c= oppsList.size();
        }
        update accList;
    }
}


This is my code, could anyone tell me how to write the trigger handler for this?
 
  • January 06, 2023
  • Like
  • 0
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');
        
    }
}
 
if i have 5 users in profile . for that profile i have given CRED access. OWD is private. i want to  take out the create access for 1 user from that profiel . can we do that, how?
I want to query all fields in the start method of the batch class. Below is my code which is giving error(also giving error on type casting). Can you please suggest if there is any way to achieve this?
global class BatchClass implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) { 
    Map<String, Schema.SObjectField> CaseMap = Case.sObjectType.getDescribe().fields.getMap();        
    Set<String> setFieldNames = CaseMap.keySet();
    list<String> lstFieldNames = new List<String>(setFieldNames);                
    String query = Database.query('SELECT ' + String.join(lstFieldNames, ',') + ' FROM Case limit 2');
    return Database.getQueryLocator(query);
    }
}
 

Hello! I'm having trouble figuring this one out. So any help will be highly appreciated!

I have a custom picklist field in Order.

I wish to retrieve all the most recent records for each value in the picklist.

Let's say that there are 10 Order records and that the picklist has values A,B,C. What I would like to retrieve (in a single query to avoid SOQL limits) is a list of the 3 most recent records that have each value.

How would you achieve this?

 

Hello,
I am trying to update all my leads which were created before I have added a new field. Now, after having all deployed onto UAT I need to perform post deployment action. But having an issue with the code, what is wrong with it?
For (List <Event> eventList: [SELECT Id, WhoId, TypeOf     
 WHO 
 WHEN Lead 
 THEN FirstInterviewdate__c 
 END, Subject, ActivityDate 
 FROM Event 
 WHERE Subject = 'First Interview'
AND
 WHOID != null
 LIMIT 200]){
    List<Lead> leadlist = new List<Lead>(); 
    For(Event evt: eventList){
        leadlist.add(new Lead (Id=evt.WhoId, FirstInterviewdate__c = evt.ActivityDate));
      }
    update leadList;
}
and the issue is:

User-added image
 
Please help with this test class, I cant get 100%, dont know how to improve it. Thanks,
@isTest
private class AutoPopDateTest {
  @isTest
  private static void AutoPopDate() {
    Billing__C bil = new Billing__C();
    bil.Name = 'NewBill';
    bil.Billing_Date__c = system.today();
    bil.Accounting_date__c = bil.Billing_Date__c.addDays(20);
    try {
      insert bil;
    } catch (NullPointerException n) {
    }
  }
}
and trigger:
trigger AutoPopAccPerDate on Billing__c(before insert, before update) {
   for (Billing__c billing : Trigger.new) {
    if (billing.Accounting_Date__c == null) {
      billing.Accounting_Date__c = billing.Billing_Date__c.adddays(30); // '30 Days' - Depends on the terms.
    }
  }
}

Appreciate any support!
 
what is wrong with this code? It says - "Arithmetic expressions must use numeric arguments (4:43)",
  Please help.
trigger AutoPopAccPerDate on Billing__c(before insert, before update) {
  for (Billing__c billing : Trigger.new) {
    if (billing.Accounting_Date__c == null) {
      billing.Accounting_Date__c = Billing__c.Billing_Date__c + 30; 
    }
  }
}