function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bhagibhagi 

Initial term of field expression must be a concrete SObject

i have maintenence renewal objectwhen end date reaches to 60days before the end date it has to create a new record and send an email with some certain conditions which i mentioned in the code..

 

please help out this

 

global class MRUpdate implements  Database.Batchable<sObject> {

global final string query;

global MRUpdate (String q)
{
   query = q;
}

global Database.QueryLocator start(Database.BatchableContext BC){

   return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Maintence_Renewal__c> scope)
{
  List<Maintence_Renewal__c> MRSToUpdate = new List<Maintence_Renewal__c>();
   MRSToUpdate=[select id,name,Start_Date__c,End_Date__c,testcontract__c,Email__C,
                 Amount__c from Maintence_Renewal__c ];
  for(Maintence_Renewal__c m :MRSToUpdate)
  {
  if(m.End_Date__c==(m.know_date__c))
    {
     m.Start_Date__c=MRSToUpdate.End_Date__c;
     m.testcontract__c=MRSToUpdate.testcontract__c;
     m.Email__c=MRSToUpdate.Email__C;
     m.Amount__c=MRSToUpdate.Amount__c+(MRSToUpdate.Amount__c *5/100);
    }
  }
}
global void finish(Database.BatchableContext BC){
  // Get the ID of the AsyncApexJob representing this batch job  
  // from Database.BatchableContext.    
  // Query the AsyncApexJob object to retrieve the current job's information.  

 AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
   TotalJobItems, CreatedBy.Email
   from AsyncApexJob where Id =:BC.getJobId()];

  // Send an email to the Apex job's submitter notifying of job completion.  
  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 processed ' + a.TotalJobItems +
    ' batches with '+ a.NumberOfErrors + ' failures.');

  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

}

 

Thanks&Regards

bhagi

Prafull G.Prafull G.

In your code the valiable MRSToUpdate is of type list... therefore the below highlighted expression is wrong and you have to access elements of list by iterating it of using index.

 

if(m.End_Date__c==(m.know_date__c))
    {
     m.Start_Date__c=MRSToUpdate.End_Date__c;
     m.testcontract__c=MRSToUpdate.testcontract__c;
     m.Email__c=MRSToUpdate.Email__C;
     m.Amount__c=MRSToUpdate.Amount__c+(MRSToUpdate.Amount__c *5/100);
    }

 

Let me know if it helps!

bhagibhagi

hi crmtech21

 

the problem is solved but my need is when the end date of maintenance renewal reaches to  60 days before  end date ,i need to get a new record along a mail to the person...and this is my code..

  please help out this

global class MRUpdate implements  Database.Batchable<sObject> {

global final string query;
global final string email;   
global MRUpdate(String q)
{
   query = q;
   System.debug('--- Followers list: ' + query);
}

global Database.QueryLocator start(Database.BatchableContext BC)
{

   return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Maintence_Renewal__c> scope)
{
  List<Maintence_Renewal__c> MRSToUpdate = new List<Maintence_Renewal__c>();
   
  for(Maintence_Renewal__c m :scope)
  {
    if(m.End_Date__c==m.know_date__c)
    {
     Maintence_Renewal__c c2= new Maintence_Renewal__c(Start_Date__c=m.End_Date__c,
                                Status__c='Pending',
                                testcontract__c=m.testcontract__c,Email__c=m.Email__C,
                                Amount__c=m.Amount__c+(m.Amount__c *5/100));
     MRSToUpdate.add(c2);                         
    }
  }
  update MRSToUpdate;
}
global void finish(Database.BatchableContext BC){
  // Get the ID of the AsyncApexJob representing this batch job  
  // from Database.BatchableContext.    
  // Query the AsyncApexJob object to retrieve the current job's information.  

 AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
   TotalJobItems, CreatedBy.Email
   from AsyncApexJob where Id =:BC.getJobId()];

  // Send an email to the Apex job's submitter notifying of job completion.  
  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 processed ' + a.TotalJobItems +
    ' batches with '+ a.NumberOfErrors + ' failures.');

  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

}

 

 

Thanks&Regards

bhagi

sunil_kumarsunil_kumar

Hi bhagi,

 

Use upsert instead of update. In for loop, check if your condition met then create new record and add it to list and if it doesn't met your condition then do modification on record if you want and add that record to list. So when you will perform upsert, the records which are having Id means modified record will be update and new records will be inserted.

 

note : if you are not specifying any external id field with upsert, then it will check with record id in order decide for update or insert.

 

Below is sample code:

 

  List<Maintence_Renewal__c> MRSToUpdate = new List<Maintence_Renewal__c>();
   
  for(Maintence_Renewal__c m :scope)
  {
    //if condition is met

   if(m.End_Date__c==m.know_date__c)
    {
     //these records will get inserted

      Maintence_Renewal__c c2= new Maintence_Renewal__c(Start_Date__c=m.End_Date__c,
                                Status__c='Pending',
                                testcontract__c=m.testcontract__c,Email__c=m.Email__C,
                                Amount__c=m.Amount__c+(m.Amount__c *5/100));
     MRSToUpdate.add(c2);                         
    }

    else

    {

         //your logic to update say changing status

         //these records will be updated

         Status__c='Still Pending';

          MRSToUpdate.add(c2); 

    }

 }
  upsert MRSToUpdate;

 

Hope this will help you.

[If it solves your problem, please mark it as solution]