• guptach45
  • NEWBIE
  • 35 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
global class ItalyPriceBookIntegrationBatch implements Database.Batchable<sObject>{
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        Datetime lastOneHour = Datetime.now().addMinutes(-60);
        string finalQuery = 'Select Id, Sales_Org__c, Product_SKU_Calculated__c from Product2 where (NOT Material_Master_ID__c like \'' +'%SEP%' + '\')' 
            +' and LastModifiedDate >= ' +string.valueOf(lastOneHour.Date())+'T'+string.valueOf(lastOneHour.Time());
        system.debug('Final Query --> ' +finalQuery);
        return Database.getQueryLocator(finalQuery);
    }
    
    global void execute(Database.BatchableContext BC,List<Product2> scope){
        set<Id> prodIdSet = new set<Id>();
        List<PriceBookEntry> newPBEList = new List<PriceBookEntry>();
        List<PriceBookEntry> updatePBEList = new List<PriceBookEntry>();
        map<Id,PriceBookEntry> pbeProdMap = new map<Id,PriceBookEntry>();
        string it01PBid = System.Label.IT01_PriceBookId;
        for(Product2 prod : scope){
            prodIdSet.add(prod.Id);
        }
        System.debug('the Price Book Entries are -->'+prodIdSet);
        for(PriceBookEntry pbeList : [Select Id,Name,Product2Id,IsActive from PriceBookEntry where Pricebook2Id =:it01PBid and Product2Id In :prodIdSet]){
            pbeProdMap.put(pbeList.Product2Id,pbeList);
        }
        System.debug('the Price Book Entries are -->'+pbeProdMap);
        for(Product2 prodIT : scope){
            if(prodIT.Sales_Org__c == 'IT01'){
                if(pbeProdMap.get(prodIT.Id)!= Null){
                    PriceBookEntry oldPBE = pbeProdMap.get(prodIT.Id);
                    System.debug('the PBE is -->'+oldPBE.Id+oldPBE.Name);
                    if(!oldPBE.IsActive){
                        oldPBE.IsActive = True;
                        
                        updatePBEList.add(oldPBE);
                    }
                    
                }else {
                    PriceBookEntry newPBE = new PriceBookEntry();
                    newPBE.Pricebook2Id = System.Label.IT01_PriceBookId;
                    newPBE.Product2Id = prodIT.Id;
                    newPBE.IsActive = True;
                    newPBE.PBE_External_Id__c = 'EPC-IT01-'+prodIT.Product_SKU_Calculated__c;
                    newPBE.UnitPrice = 0.00;
                    newPBE.CurrencyIsoCode = 'EUR';
                    
                    newPBEList.add(newPBE);
                }
            }else {
                if(pbeProdMap.get(prodIT.Id)!= Null){
                    PriceBookEntry oldPBE = pbeProdMap.get(prodIT.Id);
                    System.debug('the PBE is -->'+oldPBE.Id+oldPBE.Name);
                    if(oldPBE.IsActive){
                        oldPBE.IsActive = False;
                        
                        updatePBEList.add(oldPBE);
                    }
                }
            }
        }
        system.debug('The update list is -->'+updatePBEList.size());
        if(updatePBEList.size() > 0){
            Database.SaveResult[] oldPBE = Database.update(updatePBEList,false);
            for(Database.SaveResult opbe:oldPBE){
                if(!opbe.isSuccess()){
                    system.debug(' Error-->'+opbe.getErrors());        
                }
            }
        }
        system.debug('The new list is -->'+newPBEList.size());
        if(newPBEList.size() > 0){
            Database.SaveResult[] newPBE = Database.insert(newPBEList,false);
            for(Database.SaveResult npbe:newPBE){
                if(!npbe.isSuccess()){
                    system.debug(' Error -->'+npbe.getErrors());        
                }
            }
        }
    }
    
    global void finish(Database.BatchableContext BC){
        
    }
    
}

Test Class--

@isTest
public class ItalyPriceBookIntegrationBatch_Test {
@testsetup
    static void setup(){
        Product2 prodOne =  DivAC_00_TestFactory.getProduct('Test Product');
        prodOne.Sales_Org__c = 'IT01';
        prodOne.Material_Master_ID__c = '12345_EPC';
        insert prodOne;
        Pricebook2 standardPricebook = DivAC_00_TestFactory.getStdPriceBookId();
        Map<id,PricebookEntry> pbeProdMap = new Map<id,PricebookEntry>();
        PricebookEntry pbe = DivAC_00_TestFactory.getPriceBookEntry(String.ValueOf(standardPricebook.Id),String.ValueOf(prodOne.Id),2);
        pbe.Product2Id = prodOne.id;
        pbe.IsActive = true;
        insert pbe;
        pbeProdMap.put(pbe.Product2Id,pbe);
        pbeProdMap.get(prodOne.Id);
        System.debug('the Price Book Entries are -->'+pbeProdMap);
        PriceBookEntry oldPBE = pbeProdMap.get(prodOne.Id);
        
    }
    static testmethod void test(){
        Test.startTest();
        ItalyPriceBookIntegrationBatch ipb = new ItalyPriceBookIntegrationBatch();
        ID batchId = Database.executeBatch(ipb);
         Test.stopTest();
   
    }
}

 
public class DivAC_14_IntegrationController {
   
    public static HttpResponse makeCallOut(String requestBody,String END_POINT){
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(END_POINT);
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setMethod('POST');
        request.setBody(requestBody);
        request.setTimeout(45000);
        HttpResponse response = http.send(request);
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            system.debug('body--->'+response.getBody());
        }else{
            system.debug('response--->'+response);
        }
        return response;
    }
    
}
global class CSTeamCountryCaseUpdateBatch implements Database.Batchable<sObject>{
   global Database.QueryLocator start(Database.BatchableContext bc) {
      return Database.getQueryLocator([SELECT ID, Status, LastModifiedDate,LastModifiedById,Last_Modified_By_Previous_Day__c,CS_Team_Country__c,Days_Since_Last_Modified_Date__c FROM Case Where Status !='Closed' AND (CS_Team_Country__c ='United States' OR CS_Team_Country__c ='Canada')]);
    }
    global void execute(Database.BatchableContext bc, List<Case> records){
        system.debug('records--'+records.size());
       List<Case> caseList = new List<Case>();
        for(Case c:records){
            system.debug('test--'+c.Days_Since_Last_Modified_Date__c);
            if(c.Days_Since_Last_Modified_Date__c == null){
                c.Days_Since_Last_Modified_Date__c =0;
            }
            c.Days_Since_Last_Modified_Date__c =c.Days_Since_Last_Modified_Date__c+1;
            if(c.LastModifiedById != system.label.apiuserid){
            c.Last_Modified_By_Previous_Day__c =c.LastModifiedById;
            }
            caseList.add(c);
        }
        system.debug('caseList--'+caseList);
        if(caseList.size()>0){
            update caseList;
        }
        system.debug('caseList--'+caseList);
    }    
    global void finish(Database.BatchableContext bc){
        
    }   
}
Global class CSTeamCountryCaseUpdateSchedular implements Schedulable {
   Global void execute(SchedulableContext sc){
       
        CSTeamCountryCaseUpdateBatch batchClassInstance= new CSTeamCountryCaseUpdateBatch();
        Database.executeBatch(batchClassInstance);
       
    }
}

Hello Experts,

I recevied one request from Client that, just like Admin can edit List View's Filters, Client also wants to give permission to particular profile to Edit List View's Filters.

Possible?? Any Way??

User-added image

 

global class CSTeamCountryCaseUpdateBatch implements Database.Batchable<sObject>{
   global Database.QueryLocator start(Database.BatchableContext bc) {
      return Database.getQueryLocator([SELECT ID, Status, LastModifiedDate,LastModifiedById,Last_Modified_By_Previous_Day__c,CS_Team_Country__c,Days_Since_Last_Modified_Date__c FROM Case Where Status !='Closed' AND (CS_Team_Country__c ='United States' OR CS_Team_Country__c ='Canada')]);
    }
    global void execute(Database.BatchableContext bc, List<Case> records){
        system.debug('records--'+records.size());
       List<Case> caseList = new List<Case>();
        for(Case c:records){
            system.debug('test--'+c.Days_Since_Last_Modified_Date__c);
            if(c.Days_Since_Last_Modified_Date__c == null){
                c.Days_Since_Last_Modified_Date__c =0;
            }
            c.Days_Since_Last_Modified_Date__c =c.Days_Since_Last_Modified_Date__c+1;
            if(c.LastModifiedById != system.label.apiuserid){
            c.Last_Modified_By_Previous_Day__c =c.LastModifiedById;
            }
            caseList.add(c);
        }
        system.debug('caseList--'+caseList);
        if(caseList.size()>0){
            update caseList;
        }
        system.debug('caseList--'+caseList);
    }    
    global void finish(Database.BatchableContext bc){
        
    }   
}
Global class CSTeamCountryCaseUpdateSchedular implements Schedulable {
   Global void execute(SchedulableContext sc){
       
        CSTeamCountryCaseUpdateBatch batchClassInstance= new CSTeamCountryCaseUpdateBatch();
        Database.executeBatch(batchClassInstance);
       
    }
}