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
ForceLoverForceLover 

Update failed. first error: MISSING_ARGUMENT, Id not specified in an update call: []

Hi can any one suggest me the chanfes to mu code where it is necessary.

This was the complete error

First error: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

 

Here is my code

 

global class MRUpdate2 implements Database.Batchable<sobject>,Schedulable {

global final string query= 'select id,Amount__c,name,End_Date__c,know_date__c,Today__c,contract__c,Email__c,Status__c,Start_Date__c from Maintanence_Renewal__c';
global final string email;
global MRUpdate2()
{

System.debug('--- Followers list: ' + query);
email='attelli.anilkumar@gmail.com';
}
global void execute(SchedulableContext SC) {
// invoke business logic - this method is strictly to invoke
// the method from the scheduler...
MRUpdate2 MR2 = new MRUpdate2();
ID batchprocessid = Database.executeBatch(MR2,10);

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

return Database.getQueryLocator(this.query);
}

global void execute(Database.BatchableContext BC, List<Maintanence_Renewal__c> scope)
{
List<Maintanence_Renewal__c> MRSToUpdate = new List<Maintanence_Renewal__c>();

for(Maintanence_Renewal__c m :scope)
{
if(m.Today__c==m.know_date__c)
{
Maintanence_Renewal__c c2= new Maintanence_Renewal__c(Start_Date__c=m.End_Date__c,
Status__c='Pending',
contract__c=m.contract__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 contractor notifying about contract.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {email};
mail.setToAddresses(toAddresses);
mail.setSubject('Renew your contract');
mail.setPlainTextBody('Dear Sir/Madam please Renew Your Contract it will expire in 60 days' );

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

}

 

Tnanks & Regards

Attelli

Best Answer chosen by Admin (Salesforce Developers) 
Sunil02KumarSunil02Kumar

Hi attelli,

 

If you want to use email templates then you have to specify settargetobjectid(contactid or userid or leadid) attributes.

If you details for all atributes and sample code then visit below url and enter your valid email id and click on send email button. You will recieve sample code in email.

 

send email through apex

 

This will solve your problem related to sending email.

 

As you said you want to send email when particular criteris is met. please refer below code

global void finish(Database.BatchableContext BC)

{

       if(condition met)

      {

         // send email code

      }

     else

    {

        //any other operation you want to perform

    }

}

 

As you are mentioning that you are getting mails daily means your criteria is getting match. Check your condition and implement code for sending email. It will work fine.

 

Please let me know if you still face any problem.

 

Hope this will help you.

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

All Answers

sunil_kumarsunil_kumar

Hi Attelli,

 

Actually you are creating new records and adding them in to list. So when you are performing update, Id is missing.

 

try in this way

List<Maintanence_Renewal__c> MRSToUpdate = new List<Maintanence_Renewal__c>();

for(Maintanence_Renewal__c m :scope)
{
if(m.Today__c==m.know_date__c)
{
m.Start_Date__c=m.End_Date__c
Status__c='Pending',
contract__c=m.contract__c,Email__c=m.Email__C,
Amount__c=m.Amount__c+(m.Amount__c *5/100));
MRSToUpdate.add(m);
}

}

if(MRSToUpdate.size()>0)

{

     update MRSToUpdate;

}

 

 

In this way you are modifying existing records and adding them in list and performing update on existing records.

 

Hope this will help you.

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

 

ForceLoverForceLover

Hi Sunil

               Thanks alot for reply...yeah i did that mistake i changed my code...now the record is created but thing is i want to add an email template to the mail can you suggest me in this regard....here is my revised code.and i scheduled it daily so iam getting every day a mail which is not required it should come only once when today --know date help me in this regard

 

 

global class MRUpdate2 implements Database.Batchable<sobject>,Schedulable {

global final string query= 'select id,Amount__c,name,End_Date__c,know_date__c,Today__c,contract__c,Email__c,Status__c,Start_Date__c from Maintanence_Renewal__c';
global final string email;
global MRUpdate2()
{
email='attelli.anilkumar@gmail.com';
}
global void execute(SchedulableContext SC) {
// invoke business logic - this method is strictly to invoke
// the method from the scheduler...
MRUpdate2 MR2 = new MRUpdate2();
ID batchprocessid = Database.executeBatch(MR2,10);
}
global Database.QueryLocator start(Database.BatchableContext BC)
{

return Database.getQueryLocator(this.query);
}

global void execute(Database.BatchableContext BC, List<Maintanence_Renewal__c> scope)
{
List<Maintanence_Renewal__c> MRSToUpdate = new List<Maintanence_Renewal__c>();

for(Maintanence_Renewal__c m :scope)
{
if(m.Today__c==m.know_date__c)
{
Maintanence_Renewal__c c2= new Maintanence_Renewal__c(Start_Date__c=m.End_Date__c,
Status__c='Pending',
contract__c=m.contract__c,Email__c=m.Email__C,
Amount__c=m.Amount__c+(m.Amount__c *5/100));
MRSToUpdate.add(c2);
}
}
insert MRSToUpdate;
}
global void finish(Database.BatchableContext BC){

//Maintanence_Renewal__c m1= [select //id,Amount__c,name,End_Date__c,know_date__c,Today__c,contract__c,Email__c,Status__c,Start_Date__c from //Maintanence_Renewal__c] ;
//if(m1.Today__c==m1.know_date__c)
//{

// Send an email to the contractor notifying about contract.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {email};
mail.setToAddresses(toAddresses);
mail.setSubject('Renew your contract');
mail.setPlainTextBody('Dear Sir/Madam please Renew Your Contract it will expire in 60 days' );
mail.setTemplateId('00X90000000WLTIEA4');// when iam adding this it is giving an error like missing template id
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
//}
}
}

Sunil02KumarSunil02Kumar

Hi Attelli,

 

1. email template id should be valid and it should be available for use.

2. if you want to use email template then do not specify mail.setSubject('Renew your contract') and mail.setPlainTextBody attributes.Specify subject and body in email template.

ForceLoverForceLover

Hi Sunil,

 

 

                 I tried like that also can you send me a sample code so that i can make necessary changes to mine....and i want to use the template of my custom object maintenanace renewal ...as i schedule it daily it is sending daily a mail but it should send email only once when the condition met...please make necessary changes to my code and send me back

 

Thanks& Regards

Anil kumar 

Sunil02KumarSunil02Kumar

Hi attelli,

 

If you want to use email templates then you have to specify settargetobjectid(contactid or userid or leadid) attributes.

If you details for all atributes and sample code then visit below url and enter your valid email id and click on send email button. You will recieve sample code in email.

 

send email through apex

 

This will solve your problem related to sending email.

 

As you said you want to send email when particular criteris is met. please refer below code

global void finish(Database.BatchableContext BC)

{

       if(condition met)

      {

         // send email code

      }

     else

    {

        //any other operation you want to perform

    }

}

 

As you are mentioning that you are getting mails daily means your criteria is getting match. Check your condition and implement code for sending email. It will work fine.

 

Please let me know if you still face any problem.

 

Hope this will help you.

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

This was selected as the best answer
ForceLoverForceLover

Hi Sunil

             

              Thanks for replying me.i made changes as you said but it is not recognising the etemp.id it is giving invalid.i send code to your gmail can you go through that please.i mentioned a valid template unique name.