• Pravin K Yadav
  • NEWBIE
  • 25 Points
  • Member since 2021

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Pretty new to Apex in general, and completely new to Batch Apex, but that's what I'll need to use in order to do the job before me so I'm tyring to get my head around the differences. The Tralihead module on Async Apex has a good example, but doesn't answer my question. The developer guide (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm) shows a couple of different examples, but still doesn't address the quesiton.

So in its shortest form, my quesiotn is what's the difference between these two class declarations? Besides the obvious I mean.
public class SearchAndReplace implements Database.Batchable<sObject>{}
and 
public class batchClass implements Database.batchable{}
One specifiec <sObject> in the protitype, the other doesn't. That suggests it's optional, but I've yet to find any docuemntation that outlines when to use it and when not to.

The difference in the classes themselves is mostly in the start method -- the first returns a QueryLocator and the second an Iterator (the other difference being that I mostly understand what the first one is doing). But it doesn't explain why the <sObject> exists in one prototype and not the other. There may be a use case where you'd use Batch Apex for something besides processing large numebrs of database records, but neiter of these examples does that -- they just differ in how (and presumably how many) they process the records in question.

That's not the only question these examples raise, but trying to tsneak in another one woudl be cheating. So I'll start with this one and see if it helps with the others. 

 
Hi,i have created a lightning components,where for one field i want a link to open to the record detail page..

my components:
<aura:component controller="WP_dRMA_SubProducts" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="gridColumns" type="List"/>
    <aura:attribute name="gridData" type="Object"/>
    <aura:attribute name="sku" type="string"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    
    <lightning:accordion aura:id="accordion" activeSectionName="B">
        <lightning:accordionSection name="B" label="Sub Components">
            <lightning:treeGrid aura:id="subProdTreegridID" 
                                columns="{!v.gridColumns }"
                                data="{!v.gridData }"                               
                                keyField="KeyField"
             />
        </lightning:accordionSection>
    </lightning:accordion>
    
</aura:component>
my controller:
({
    doInit : function(cmp, event, helper){
        cmp.set('v.gridColumns',[
            {label:'Product Name', fieldName:'productName', type:'text'},
            {label:'Product Code', fieldName:'productCode',  type: 'url',
            typeAttributes: {label: { fieldName: 'productCode' },value:{fieldName: 'productCode'}, target: '_blank'}
               },            
        ]);
            
        helper.getSubProducts(cmp, event);            
    },
    
})
my helper:

({
    getSubProducts : function(cmp, event) {
        var skuVal = cmp.get('v.sku');
        var action = cmp.get('c.getRelatedProducts');
        action.setParams({sku:skuVal});
        action.setCallback(this,function(response){
            var state = response.getState();
            console.log('**state-->'+state);
            if(state === "SUCCESS"){
                var data = response.getReturnValue();
              // data.forEach(function(data){
                  //  data.productCode = '/'+data.Id;
               // });
                console.log('**data-->'+data);
                //JSON.parse(response.getReturnValue());                        
                //var temojson = JSON.parse(JSON.stringify(data).split('items').join('_children'));
                //console.log('**temojson-->'+temojson);
                for(var i=0;i<data.length;i++){
                    data[i]._children = data[i]['items'];
                    delete data[i].items;
                }
                console.log('**data after changing to _children-->'+data);
                cmp.set('v.gridData',data);
                //cmp.set('v.gridData',temojson);
                console.log('**gridData-->'+cmp.get('v.gridData'));
            }else if(state === "ERROR"){
                var errors = action.getError();
                if(errors){
                    if(errors[0] && errors[0].message){
                        console.log('**error Message-->'+errors[0].message);
                    }
                }
            }
        });            
        $A.enqueueAction(action);  
    }    
})
Pretty new to Apex in general, and completely new to Batch Apex, but that's what I'll need to use in order to do the job before me so I'm tyring to get my head around the differences. The Tralihead module on Async Apex has a good example, but doesn't answer my question. The developer guide (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm) shows a couple of different examples, but still doesn't address the quesiton.

So in its shortest form, my quesiotn is what's the difference between these two class declarations? Besides the obvious I mean.
public class SearchAndReplace implements Database.Batchable<sObject>{}
and 
public class batchClass implements Database.batchable{}
One specifiec <sObject> in the protitype, the other doesn't. That suggests it's optional, but I've yet to find any docuemntation that outlines when to use it and when not to.

The difference in the classes themselves is mostly in the start method -- the first returns a QueryLocator and the second an Iterator (the other difference being that I mostly understand what the first one is doing). But it doesn't explain why the <sObject> exists in one prototype and not the other. There may be a use case where you'd use Batch Apex for something besides processing large numebrs of database records, but neiter of these examples does that -- they just differ in how (and presumably how many) they process the records in question.

That's not the only question these examples raise, but trying to tsneak in another one woudl be cheating. So I'll start with this one and see if it helps with the others. 

 
Hi, I have a VF page with an input text box when I enter some string it should check whether the record exists or not. I was able to achieve the functionality but in my test class, I was not able to cover a few lines.
public class searchAndLinkRecordsToAccount {
    public String searchString{get;set;}
    public Integer count;
    public List<Account_Relation__c> accRelList{get;set;}
    public searchAndLinkRecordsToAccount(ApexPages.StandardController stdController) {
    }
    public void search() {
        List<Account_Relation__c> updatedRecordsList = new List<Account_Relation__c>();
        List<Account_Relation__c> insertedRecordsList = new List<Account_Relation__c>();
        accRelList = [SELECT Id, Name, Count__c, Latest_Submitted_Date__c 
                      FROM Account_Relation__c 
                      WHERE Account__c =:ApexPages.currentPage().getParameters().get('id') AND Name =:searchString];
        if(accRelList.size() > 0) {
            for(Account_Relation__c rel : accRelList) {
                rel.Count__c = rel.Count__c + 1;
                rel.Latest_Submitted_Date__c = Datetime.now();
                updatedRecordsList.add(rel);
                ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,'Record found with the Search String'));
            }
        }
        if(accRelList.size() == 0){
            Account_Relation__c accRel = new Account_Relation__c();
            accRel.Name = searchString;
            accRel.Account__c = ApexPages.currentPage().getParameters().get('id');
            accRel.Count__c = 1;
            accRel.Latest_Submitted_Date__c = Datetime.now();
            insertedRecordsList.add(accRel);
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,'Record inserted and sent for Approval'));
        }
        if(updatedRecordsList.size() > 0) {
            update updatedRecordsList;
        }
        if(insertedRecordsList.size() > 0) {
            insert insertedRecordsList;
        }
    }
}
//Test Class

@isTest
public class searchAndLinkRecordsToAccountTest {
    public static testMethod void method1() {
        Account acc = new Account(Name='Test Acc');
        insert acc;

        Account_Relation__c accRel1 = new Account_Relation__c(Name='Test Relation');
        insert accRel1;
        acc.AnnualRevenue = 500;
        update acc;
        
        Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(acc);
        searchAndLinkRecordsToAccount recsToAcc = new searchAndLinkRecordsToAccount(stdController);
        recsToAcc.search();
        Test.stopTest();
    }
}
I was not able to cover the highlighted lines of code. Please help. Thanks in Advance.
  • March 09, 2021
  • Like
  • 0