• CEO Sahoo
  • NEWBIE
  • -1 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 19
    Questions
  • 12
    Replies
create a contact field checkbox name Isactive , create batch apex every contact of the contact fields are checked
give the batch apex code

this is the Batch apex code of the question
public class UpdateContactFieldsBatch implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext context) {
        return Database.getQueryLocator([SELECT Id, IsActive__c FROM Contact WHERE IsActive__c = false]);
    }

    public void execute(Database.BatchableContext context , List<Contact> contacts) {
        for (Contact con : contacts) {
            con.IsActive__c = true;
        }
        update contacts;
    }

    public void finish(Database.BatchableContext context ) {
       system.debug('Ashutosh');
    }
}
give the test class code which have  100% codecoverage 
i wrote this code
global class UpdateContactFieldsBatch implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext context) {
        // Query all Contacts where IsActive is true
        return Database.getQueryLocator([SELECT Id, IsActive__c FROM Contact WHERE IsActive__c = true]);
    }

    global void execute(Database.BatchableContext context, List<Contact> contacts) {
        // Loop through the selected Contacts and update the desired field(s)
        for (Contact contact : contacts) {
            contact.IsActive__c = true; // Update Email Opt Out field
        }

        // Update the Contacts
        update contacts;
    }

    global void finish(Database.BatchableContext context) {
        // Any post-processing steps can go here
    }
}


and wrote this code for debugging
UpdateContactFieldsBatch batch = new UpdateContactFieldsBatch();
Database.executeBatch(batch);


in apex job show staus completed but in out put in contact field every field in contact are not checked please help mee please modify the code
this is apex class code 
public class CreateCreditMemo  {
    public static void creditMemoOnInsert(List<opportunity>oppList){
        List<Credit_Memo__c> creList = new List<Credit_Memo__c>();
        for(opportunity opp : oppList) {
            if(opp.Create_Credit_Memo__c == true) {
               Credit_Memo__c cre = new Credit_Memo__c();
                cre.StageName__c = opp.StageName;
                cre.oppName__c = opp.Name;
                cre.Opportunity__c = opp.id;
                creList.add(cre);
            }
            insert creList;
        }
    }

    public static void creditMemoOnUpdate(List<Opportunity> oppList,Map<Id,Opportunity> oldMap){
        set<String> cmToDelete = new set<String>();
        set<String> cmToCreate = new set<String>();
        List<opportunity> filteredOpportunity = new List<Opportunity>();
        for(opportunity opp : oppList){
            if(opp.Create_Credit_Memo__c != oldMap.get(opp.Id).create_credit_Memo__c){
                if(opp.Create_Credit_Memo__c){
                    cmToCreate.add(opp.Id);
                }else{
                    cmToDelete.add(opp.Id);
                }
                filteredopportunity.add(opp);
            }
        }
        if(filteredopportunity.size() > 0){
            List<Credit_Memo__c> cmListToCreate = new List<Credit_Memo__c>();
            List<Credit_Memo__c> cmListToDelete = new List<Credit_Memo__c>();
            Map<string,Credit_Memo__c> oppIdWithcm = new Map<string,Credit_Memo__c>();
            List<Credit_Memo__c> cmList = [SELECT Id,Opportunity__c FROM Credit_Memo__c WHERE Opportunity__c IN : filteredOpportunity];
            for(Credit_Memo__c cm : cmList){
                oppIdWithCm.put(cm.Opportunity__c, cm);
            }
            for(Opportunity opp : filteredOpportunity){
                if(cmToCreate.contains(opp.Id) && !oppIdWithCm.containsKey(opp.Id)){
                    Credit_Memo__c cre = new Credit_Memo__c();
                    cre.StageName__c = opp.StageName;
                    cre.oppName__c = opp.Name;
                    cre.Opportunity__c = opp.id;
                    cmListToCreate.add(cre);
                }else if(cmToDelete.contains(opp.Id) && oppIdWithCm.containsKey(opp.Id)){
                    cmListToDelete.add(oppIdWithCm.get(opp.Id));
                }
            }
            if(cmListToCreate.size() > 0){
                insert cmListToCreate;
            }
            if(cmListToDelete.size() > 0){
                delete cmListToDelete;
            }
        }
    }
}

and this is apex trigger code
trigger Opportunitytrigger on Opportunity (after insert, after update) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
          CreateCreditMemo.creditMemoOnInsert(trigger.new);
        }
        else if (Trigger.IsUpdate){
    CreateCreditMemo.creditMemoOnUpdate(trigger.new,trigger.oldMap);
        }
     }
}

please give me the 100% test coverage code
public class CreateCreditMemo  {
    public static void creditMemoOnInsert(List<opportunity>oppList){
        List<Credit_Memo__c> creList = new List<Credit_Memo__c>();
        for(opportunity opp : oppList) {
            if(opp.Create_Credit_Memo__c == true) {
               Credit_Memo__c cre = new Credit_Memo__c();
                cre.StageName__c = opp.StageName;
                cre.oppName__c = opp.Name;
                cre.Opportunity__c = opp.id;
                creList.add(cre);
            }
            insert creList;
        }
    }

    public static void creditMemoOnUpdate(List<Opportunity> oppList,Map<Id,Opportunity> oldMap){
        set<String> cmToDelete = new set<String>();
        set<String> cmToCreate = new set<String>();
        List<opportunity> filteredOpportunity = new List<Opportunity>();
        for(opportunity opp : oppList){
            if(opp.Create_Credit_Memo__c != oldMap.get(opp.Id).create_credit_Memo__c){
                if(opp.Create_Credit_Memo__c){
                    cmToCreate.add(opp.Id);
                }else{
                    cmToDelete.add(opp.Id);
                }
                filteredopportunity.add(opp);
            }
        }
        if(filteredopportunity.size() > 0){
            List<Credit_Memo__c> cmListToCreate = new List<Credit_Memo__c>();
            List<Credit_Memo__c> cmListToDelete = new List<Credit_Memo__c>();
            Map<string,Credit_Memo__c> oppIdWithcm = new Map<string,Credit_Memo__c>();
            List<Credit_Memo__c> cmList = [SELECT Id,Opportunity__c FROM Credit_Memo__c WHERE Opportunity__c IN : filteredOpportunity];
            for(Credit_Memo__c cm : cmList){
                oppIdWithCm.put(cm.Opportunity__c, cm);
            }
            for(Opportunity opp : filteredOpportunity){
                if(cmToCreate.contains(opp.Id) && !oppIdWithCm.containsKey(opp.Id)){
                    Credit_Memo__c cre = new Credit_Memo__c();
                    cre.StageName__c = opp.StageName;
                    cre.oppName__c = opp.Name;
                    cre.Opportunity__c = opp.id;
                    cmListToCreate.add(cre);
                }else if(cmToDelete.contains(opp.Id) && oppIdWithCm.containsKey(opp.Id)){
                    cmListToDelete.add(oppIdWithCm.get(opp.Id));
                }
            }
            if(cmListToCreate.size() > 0){
                insert cmListToCreate;
            }
            if(cmListToDelete.size() > 0){
                delete cmListToDelete;
            }
        }
    }
}  useing this give me the test class of that


please help me
Create a custom object that name is credit memo then their fields are oppName_c, opportunity_c, 
StageName__c
Opportunity check box name Create_Credit_memo
__c 
When check box is check credit memo record create when the check box is unchecked then the credit memo record delete give the Apexclass code related that question        this is the right question please give the right apexclass code
this is apex class code 
public class CreateCreditMemo  {
    public static void creditMemoOnInsert(List<opportunity>oppList){
        List<Credit_Memo__c> creList = new List<Credit_Memo__c>();
        for(opportunity opp : oppList) {
            if(opp.Create_Credit_Memo__c == true) {
               Credit_Memo__c cre = new Credit_Memo__c();
                cre.StageName__c = opp.StageName;
                cre.oppName__c = opp.Name;
                cre.Opportunity__c = opp.id;
                creList.add(cre);
            }
            insert creList;
        }
    }

    public static void creditMemoOnUpdate(List<Opportunity> oppList,Map<Id,Opportunity> oldMap){
        set<String> cmToDelete = new set<String>();
        set<String> cmToCreate = new set<String>();
        List<opportunity> filteredOpportunity = new List<Opportunity>();
        for(opportunity opp : oppList){
            if(opp.Create_Credit_Memo__c != oldMap.get(opp.Id).create_credit_Memo__c){
                if(opp.Create_Credit_Memo__c){
                    cmToCreate.add(opp.Id);
                }else{
                    cmToDelete.add(opp.Id);
                }
                filteredopportunity.add(opp);
            }
        }
        if(filteredopportunity.size() > 0){
            List<Credit_Memo__c> cmListToCreate = new List<Credit_Memo__c>();
            List<Credit_Memo__c> cmListToDelete = new List<Credit_Memo__c>();
            Map<string,Credit_Memo__c> oppIdWithcm = new Map<string,Credit_Memo__c>();
            List<Credit_Memo__c> cmList = [SELECT Id,Opportunity__c FROM Credit_Memo__c WHERE Opportunity__c IN : filteredOpportunity];
            for(Credit_Memo__c cm : cmList){
                oppIdWithCm.put(cm.Opportunity__c, cm);
            }
            for(Opportunity opp : filteredOpportunity){
                if(cmToCreate.contains(opp.Id) && !oppIdWithCm.containsKey(opp.Id)){
                    Credit_Memo__c cre = new Credit_Memo__c();
                    cre.StageName__c = opp.StageName;
                    cre.oppName__c = opp.Name;
                    cre.Opportunity__c = opp.id;
                    cmListToCreate.add(cre);
                }else if(cmToDelete.contains(opp.Id) && oppIdWithCm.containsKey(opp.Id)){
                    cmListToDelete.add(oppIdWithCm.get(opp.Id));
                }
            }
            if(cmListToCreate.size() > 0){
                insert cmListToCreate;
            }
            if(cmListToDelete.size() > 0){
                delete cmListToDelete;
            }
        }
    }
}

and this is apex trigger code
trigger Opportunitytrigger on Opportunity (after insert, after update) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
          CreateCreditMemo.creditMemoOnInsert(trigger.new);
        }
        else if (Trigger.IsUpdate){
    CreateCreditMemo.creditMemoOnUpdate(trigger.new,trigger.oldMap);
        }
     }
}

please give me the 100% test coverage code
public class CreateCreditMemo  {
    public static void creditMemoOnInsert(List<opportunity>oppList){
        List<Credit_Memo__c> creList = new List<Credit_Memo__c>();
        for(opportunity opp : oppList) {
            if(opp.Create_Credit_Memo__c == true) {
               Credit_Memo__c cre = new Credit_Memo__c();
                cre.StageName__c = opp.StageName;
                cre.oppName__c = opp.Name;
                cre.Opportunity__c = opp.id;
                creList.add(cre);
            }
            insert creList;
        }
    }

    public static void creditMemoOnUpdate(List<Opportunity> oppList,Map<Id,Opportunity> oldMap){
        set<String> cmToDelete = new set<String>();
        set<String> cmToCreate = new set<String>();
        List<opportunity> filteredOpportunity = new List<Opportunity>();
        for(opportunity opp : oppList){
            if(opp.Create_Credit_Memo__c != oldMap.get(opp.Id).create_credit_Memo__c){
                if(opp.Create_Credit_Memo__c){
                    cmToCreate.add(opp.Id);
                }else{
                    cmToDelete.add(opp.Id);
                }
                filteredopportunity.add(opp);
            }
        }
        if(filteredopportunity.size() > 0){
            List<Credit_Memo__c> cmListToCreate = new List<Credit_Memo__c>();
            List<Credit_Memo__c> cmListToDelete = new List<Credit_Memo__c>();
            Map<string,Credit_Memo__c> oppIdWithcm = new Map<string,Credit_Memo__c>();
            List<Credit_Memo__c> cmList = [SELECT Id,Opportunity__c FROM Credit_Memo__c WHERE Opportunity__c IN : filteredOpportunity];
            for(Credit_Memo__c cm : cmList){
                oppIdWithCm.put(cm.Opportunity__c, cm);
            }
            for(Opportunity opp : filteredOpportunity){
                if(cmToCreate.contains(opp.Id) && !oppIdWithCm.containsKey(opp.Id)){
                    Credit_Memo__c cre = new Credit_Memo__c();
                    cre.StageName__c = opp.StageName;
                    cre.oppName__c = opp.Name;
                    cre.Opportunity__c = opp.id;
                    cmListToCreate.add(cre);
                }else if(cmToDelete.contains(opp.Id) && oppIdWithCm.containsKey(opp.Id)){
                    cmListToDelete.add(oppIdWithCm.get(opp.Id));
                }
            }
            if(cmListToCreate.size() > 0){
                insert cmListToCreate;
            }
            if(cmListToDelete.size() > 0){
                delete cmListToDelete;
            }
        }
    }
}  useing this give me the test class of that


please help me
Create a custom object that name is credit memo then their fields are oppName_c, opportunity_c, 
StageName__c
Opportunity check box name Create_Credit_memo
__c 
When check box is check credit memo record create when the check box is unchecked then the credit memo record delete give the Apexclass code related that question        this is the right question please give the right apexclass code