1

I am creating new case records from the values on custom object record, however it is throwing error for lookup field value. below is my code

global class caseToBe implements Database.Batchable<sObject>, Database.Stateful {

    Integer totalRecordsInserted = 0;
    Integer totalRecordsFailed = 0;



    global Iterable<sObject> start(database.BatchableContext BC)
    {
        system.debug('Start Called');
        return Database.getQueryLocator([select Name,Accounting_Date__c,Balance_Due__c,Temp__c from CaseToBe__c where Name != NULL]);
    }

    global void execute(Database.BatchableContext BC, List<CaseToBe__c> scope)
    {
        system.debug('Execute Called');
        List<Case> ToInsertCaseList = new List<Case>();
        for(CaseToBe__c ctb : scope)
        {
            system.debug('In Loop');
            ToInsertCaseList.add(
                new case(
                    Account_Number__c = ctb.Name,
                    Balance_Due__c  = ctb.Balance_Due__c,
                    Account = ctb.Agency_Name__c,
                    Status = 'Working',
                    Origin = 'Email'));
        }
        insert ToInsertCaseList;

    } 

    global void finish(database.BatchableContext BC)
    {
        system.debug('Finish Called');
        AsyncApexJob a = [Select Id, Status, NumberOfErrors,JobItemsProcessed, TotalJobItems, CreatedBy.Email 
                          from AsyncApexJob where Id = 
                          :BC.getJobId()];


        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {a.Createdby.Email};
            mail.setToAddresses(toAddresses);
        mail.setSubject('Apex sharing Recalculation' + a.Status);
        mail.setplainTextBody('The Batch Apex Job has been processed' + a.TotalJobItems + 'batches with' + a.NumberOfErrors + 'failures');
        Messaging.sendemail(new Messaging.SingleEmailMessage[] {mail});
    } 
}

Error

Invalid initial expression type for field Account, expecting: Account (or single row query result of that type)

2
  • Can you please provide more of the context code? What you have provided does not seem sufficient to diagnose the problem.
    – Adrian Larson
    Feb 6, 2017 at 21:00
  • Ya sure, I have updated the question with full class.
    – Ricky
    Feb 6, 2017 at 21:01

1 Answer 1

2

You need to set the AccountId field, for which you provide an Id. The Account field is a name pointing reference which you can use to set a relationship based on an External Id field.

Joy

AccountId = ctb.Agency_Name__c // assuming this value is the Id

Alternative Joy

Account = new Account(Some_External_Id_Field__c = someExternalId)

No Joy

Account = ctb.Agency_Name__c
3
  • If I use "Joy" solution and assign AccountId value will that auto populate Account Name on Account lookup filed of case object ?
    – Ricky
    Feb 6, 2017 at 21:11
  • That's a front-end mechanism of how Page Layouts work, yes (and also the <apex:outputField> tag). Just make sure you're setting an actual Id value!
    – Adrian Larson
    Feb 6, 2017 at 21:12
  • Oh excellent, That solved my problem. Thank you so for your help.
    – Ricky
    Feb 6, 2017 at 21:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .