• AntonPavlov
  • NEWBIE
  • 60 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 8
    Replies
I need move variables to a file and use it in apex classes. How do this?
I want to create a class where I will have all the variables and then call the variables in other classes where I need to reolize it in the apex. I only need it in the apex classes. In other programming languages there are imports, but how can I do this??
Apex code 
this code not cover for 100%
global class closeAllDealsMonthAgo implements 
   Database.Batchable<SObject>, Database.Stateful{ 

   List<Opportunity> listRecords = new List<Opportunity>();
   global Database.QueryLocator start(Database.BatchableContext BC){
         String query = 'Select Id, Name, CloseDate,createdDate,StageName From Opportunity WHERE CloseDate=THIS_MONTH';
         return Database.getQueryLocator(query);
   }
   global void execute(Database.BatchableContext BC, List<SObject> scope){
      for(Opportunity obj : (Opportunity[]) scope){  
         if(Date.Today()>obj.CloseDate && (obj.StageName != 'Closed Won' || obj.StageName !='Closed Lost')){
            obj.StageName = 'Closed Lost';
            listRecords.add(obj);
         }
         if(Date.Today()>obj.createdDate.addMonths(1) && (obj.StageName != 'Closed Won' || obj.StageName !='Closed Lost')){
            obj.StageName = 'Closed Lost';
            obj.CloseDate= Date.today();
            listRecords.add(obj);
         }
      }
   }
   global void finish(Database.BatchableContext BC){
      if(!listRecords.isEmpty()){
         update listRecords;
      }
   }      
}
test code 
@isTest             
private class closeAllDealsMonthAgoTest {
    static testmethod void testbatch() {
        Integer count = 200;
        Account acc = new Account(Name='Test Account');
        insert acc;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (integer i=0; i<count; i++){
            Opportunity opp = new Opportunity();
            opp.AccountId=acc.Id;
            opp.Name='My Opportunity '+i;
            opp.StageName='Qualification';
            opp.CloseDate= Date.today().addDays(-5);
            opps.add(opp);
        }
        insert opps;
        System.assertEquals(count, opps.size());
       
       Test.startTest();
           closeAllDealsMonthAgo obj = new  closeAllDealsMonthAgo();
           Database.executeBatch(obj);
       Test.stopTest();
      
         List<Opportunity> oppUpdatedList = [SELECT Id,StageName,CloseDate FROM Opportunity];
        for(Opportunity o : oppUpdatedList){
            if(Date.Today()>o.CloseDate && (o.StageName != 'Closed Won' || o.StageName !='Closed Lost')){
                o.StageName = 'Closed Lost';
                System.assertEquals('Closed Lost', o.StageName);    
            }
        }
    }
}

here apex code 
global with sharing class schedularCloseAllDealsMonthAgo implements Schedulable{
      global void execute(SchedulableContext sc) {  
            closeAllDealsMonthAgo closeDeals = new closeAllDealsMonthAgo(); 
            database.executebatch(closeDeals);
      }
      public static void schedulerMethod(){
            schedularCloseAllDealsMonthAgo sched = new schedularCloseAllDealsMonthAgo();
            string con_exp= '0 0 10 * * ?';
            System.schedule('schedularCloseAllDealsMonthAgo', con_exp, sched);
      }
}
This is code of test but not work for 100%
@isTest   
public class schedularCloseAllDealsMonthAgoTest {
    public static testMethod void testschedule() {
        Test.StartTest();
            schedularCloseAllDealsMonthAgo sched = new schedularCloseAllDealsMonthAgo();
            String CRON_EXP = '0 0 23 * * ?';
            System.schedule('schedularCloseAllDealsMonthAgo', CRON_EXP, sched);
        Test.stopTest();
    }
}
This is code apex 
public class displayImageController {
   @AuraEnabled  
    public static List<String> getImage(Id CurrentId){
          List<String> files =new List<String>();
        List<ContentDocumentLink> cdLinks =[SELECT Id,ContentDocument.LatestPublishedVersionID,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId=:CurrentId];
        for(ContentDocumentLink cdl: cdLinks){
             String FileExtension =[SELECT FileExtension FROM ContentVersion WHERE ContentDocumentId=:cdl.ContentDocumentId].get(0).FileExtension;
            if(Pattern.matches('(png|jpg|jpeg|gif|svg)', FileExtension)){
                  files.add(URL.getSalesforceBaseUrl().toExternalForm() +'/sfc/servlet.shepherd/version/download/'+cdl.ContentDocument.LatestPublishedVersionID);  
            }
        }
        return files;
    }
}

Apex test I don't know what write next

@isTest
public class displayImageControllerTest {
    private static testMethod void testCreate() {
        Account testAccount = new Account();
        testAccount.Name='Test Account' ;
        insert testAccount;
        
        Contact cont = new Contact ();
        cont.FirstName = 'FirstName';
        cont.LastName = 'LastName';
        cont.Email='email233@email.com';
        cont.phone='12345678';
        insert cont;
        
        Account acct = new Account(Name='TEST');
        insert acct;
        
        ContentVersion contentVersion = new ContentVersion(
            Title = 'Penguins',
            PathOnClient = 'Penguins.jpg',
            VersionData = Blob.valueOf('Test Content'),
            IsMajorVersion = true
        );
        insert contentVersion;    
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = acct.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
  }
}
Help to write test class for htttp request  I i can't figure it out
public  with sharing class currencyRates {
    @AuraEnabled
    public static List<Object> getRates(){
        
        String requestData='https://www.nbrb.by/api/exrates/rates?periodicity=0';
        
        Http http =new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint(requestData);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        List<Object> result =null;
        if(response.getStatusCode()==200){
            result =(List<Object>)JSON.deserializeUntyped(response.getBody());
        }else{
            ApexPages.Message mes =new ApexPages.Message(ApexPages.Severity.ERROR,'There was an error in reading Data');
        }
        return result;
    }
}
This is code trriger
trigger changeFieldProduct on Product2 (after insert,after update) { 
    List<Task> taskList = new List<Task>();
    for(Product2 prod: Trigger.New){
        if(prod.IsActive__c == false){
         List<OpportunityLineItem> obj = [SELECT OpportunityId,Opportunity.Account.Phone, Product2Id FROM OpportunityLineItem WHERE Product2Id=:prod.id];
            for(OpportunityLineItem  opp :obj){
                 taskList.add(new Task(
                                    Subject='new',
                                    whatId = opp.OpportunityId,
                                       Status='New',
                                    Auto_Created__c=true,
                                    ActivityDate = date.today,
                                    Phone__c = opp.Opportunity.Account.Phone,
                                    Priority = 'High'));
            }
        }
   }
    if(taskList.size()>0){
        insert taskList;
    }
}
Test class write only to 40% but I need get 100% help to solve 

@isTest
public class changeFieldProductTest {
@isTest
    public static void testing(){
       List<Task> taskList = new List<Task>();
       List<Product2> prod = new List<Product2>();
        for(Integer i=0;i<10;i++){
           Product2 p = new Product2(Name='test ' +i, IsActive__c = false);
            prod.add(p);
        }
        Test.startTest();
            insert prod;
        Test.stopTest();
        
       System.assertEquals(10, prod.size()); 
        
       List<OpportunityLineItem> obj = [SELECT OpportunityId,Opportunity.Account.Phone, Product2Id FROM OpportunityLineItem WHERE Product2Id IN:prod];
       
       System.assertEquals(10, prod.size());
    }
}
I need display data as a list receiving via class FindDuplicateCase to aura component. But I have empty page, although the class returns values when debugging 
Class Apex
public class FindDuplicateCase {
@AuraEnabled
    public static List<Case> getCaseListDuplicate(){
        List<Case> caseFields =[SELECT Id,CaseNumber,Origin,Subject,SuppliedEmail,Status,Reason,OwnerId FROM Case WHERE Status!='Closed'];
        List<Case> duplicateCaseList = new List<Case>();
        for(Case case1:caseFields){
           for(Case case2:caseFields){
               if(case1.Subject==case2.Subject && case1.SuppliedEmail==case2.SuppliedEmail && case1.Reason==case2.Reason){
                    duplicateCaseList.add(case1);
               }
            }
        }
        return duplicateCaseList; 
    }
}
Aura component 
<aura:component implements="force:lightningQuickAction" controller="FindDuplicateCase" access="global">
    <aura:handler name="init" action="{!c.doInit}" value="{!this}" />
    <aura:attribute name="duplicateCaseList" type="List" />
        <aura:iteration items="{!v.duplicateCaseList}" var="item">
                <ul>
                <li>Case Number{!item.CaseNumber}</li>
            </ul>
           </aura:iteration>     
</aura:component>
Javascript 
({
    doInit: function(component, event, helper) {
        const list = component.get('c.getCaseListDuplicate');
        component.set("v.duplicateCaseList",list);
        $A.enqueueAction(list);
    }
})
trigger countTack on MyTask__c (after update) {
   List<MyTask__c> myTask = [SELECT Id,(SELECT Id,CountMyTask__с FROM Contact) FROM MyTask__c WHERE Id IN :Trigger.New];
   for(MyTask__c my :myTask){
      List<Contact> contactList = my.Contacts;
      my.CountMyTask__c = contactList.size();
   }
}
Dont work
I need move variables to a file and use it in apex classes. How do this?
I need move variables to a file and use it in apex classes. How do this?

here apex code 
global with sharing class schedularCloseAllDealsMonthAgo implements Schedulable{
      global void execute(SchedulableContext sc) {  
            closeAllDealsMonthAgo closeDeals = new closeAllDealsMonthAgo(); 
            database.executebatch(closeDeals);
      }
      public static void schedulerMethod(){
            schedularCloseAllDealsMonthAgo sched = new schedularCloseAllDealsMonthAgo();
            string con_exp= '0 0 10 * * ?';
            System.schedule('schedularCloseAllDealsMonthAgo', con_exp, sched);
      }
}
This is code of test but not work for 100%
@isTest   
public class schedularCloseAllDealsMonthAgoTest {
    public static testMethod void testschedule() {
        Test.StartTest();
            schedularCloseAllDealsMonthAgo sched = new schedularCloseAllDealsMonthAgo();
            String CRON_EXP = '0 0 23 * * ?';
            System.schedule('schedularCloseAllDealsMonthAgo', CRON_EXP, sched);
        Test.stopTest();
    }
}
This is code apex 
public class displayImageController {
   @AuraEnabled  
    public static List<String> getImage(Id CurrentId){
          List<String> files =new List<String>();
        List<ContentDocumentLink> cdLinks =[SELECT Id,ContentDocument.LatestPublishedVersionID,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId=:CurrentId];
        for(ContentDocumentLink cdl: cdLinks){
             String FileExtension =[SELECT FileExtension FROM ContentVersion WHERE ContentDocumentId=:cdl.ContentDocumentId].get(0).FileExtension;
            if(Pattern.matches('(png|jpg|jpeg|gif|svg)', FileExtension)){
                  files.add(URL.getSalesforceBaseUrl().toExternalForm() +'/sfc/servlet.shepherd/version/download/'+cdl.ContentDocument.LatestPublishedVersionID);  
            }
        }
        return files;
    }
}

Apex test I don't know what write next

@isTest
public class displayImageControllerTest {
    private static testMethod void testCreate() {
        Account testAccount = new Account();
        testAccount.Name='Test Account' ;
        insert testAccount;
        
        Contact cont = new Contact ();
        cont.FirstName = 'FirstName';
        cont.LastName = 'LastName';
        cont.Email='email233@email.com';
        cont.phone='12345678';
        insert cont;
        
        Account acct = new Account(Name='TEST');
        insert acct;
        
        ContentVersion contentVersion = new ContentVersion(
            Title = 'Penguins',
            PathOnClient = 'Penguins.jpg',
            VersionData = Blob.valueOf('Test Content'),
            IsMajorVersion = true
        );
        insert contentVersion;    
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = acct.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
  }
}
Help to write test class for htttp request  I i can't figure it out
public  with sharing class currencyRates {
    @AuraEnabled
    public static List<Object> getRates(){
        
        String requestData='https://www.nbrb.by/api/exrates/rates?periodicity=0';
        
        Http http =new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint(requestData);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        List<Object> result =null;
        if(response.getStatusCode()==200){
            result =(List<Object>)JSON.deserializeUntyped(response.getBody());
        }else{
            ApexPages.Message mes =new ApexPages.Message(ApexPages.Severity.ERROR,'There was an error in reading Data');
        }
        return result;
    }
}
This is code trriger
trigger changeFieldProduct on Product2 (after insert,after update) { 
    List<Task> taskList = new List<Task>();
    for(Product2 prod: Trigger.New){
        if(prod.IsActive__c == false){
         List<OpportunityLineItem> obj = [SELECT OpportunityId,Opportunity.Account.Phone, Product2Id FROM OpportunityLineItem WHERE Product2Id=:prod.id];
            for(OpportunityLineItem  opp :obj){
                 taskList.add(new Task(
                                    Subject='new',
                                    whatId = opp.OpportunityId,
                                       Status='New',
                                    Auto_Created__c=true,
                                    ActivityDate = date.today,
                                    Phone__c = opp.Opportunity.Account.Phone,
                                    Priority = 'High'));
            }
        }
   }
    if(taskList.size()>0){
        insert taskList;
    }
}
Test class write only to 40% but I need get 100% help to solve 

@isTest
public class changeFieldProductTest {
@isTest
    public static void testing(){
       List<Task> taskList = new List<Task>();
       List<Product2> prod = new List<Product2>();
        for(Integer i=0;i<10;i++){
           Product2 p = new Product2(Name='test ' +i, IsActive__c = false);
            prod.add(p);
        }
        Test.startTest();
            insert prod;
        Test.stopTest();
        
       System.assertEquals(10, prod.size()); 
        
       List<OpportunityLineItem> obj = [SELECT OpportunityId,Opportunity.Account.Phone, Product2Id FROM OpportunityLineItem WHERE Product2Id IN:prod];
       
       System.assertEquals(10, prod.size());
    }
}
trigger countTack on MyTask__c (after update) {
   List<MyTask__c> myTask = [SELECT Id,(SELECT Id,CountMyTask__с FROM Contact) FROM MyTask__c WHERE Id IN :Trigger.New];
   for(MyTask__c my :myTask){
      List<Contact> contactList = my.Contacts;
      my.CountMyTask__c = contactList.size();
   }
}
Dont work