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
Dev Rana 16Dev Rana 16 

Lookup filed not update through Batch apex

Hello evey one i want to Update my Lookup filed wich is avilable in opportunity.
i m trying to update my Account name which is conected to opportunity. so can u plz give me suggation how to achive this.
global class b1 implements Database.Batchable<Sobject>{

  global final string query;
  global final string s_object;
  global final string Field;
  global final string field_value;
  
  global b1(string q, string s,string f, string v){
  
           Query = q;
           s_object = s;
           Field = f;
           field_value = v;
  }
    //start method has a return type of database query locater it will takes a query and return data
     global Database.QueryLocator start(Database.BatchableContext BC){
       return Database.getQueryLocator(query);
     }
     
     //this method processing the data
     global void execute(Database.BatchableContext BC, List<sObject> batch ){
        for(sObject o : batch){
         o.put(Field, field_value); 
        }   
        update batch;
     }
     
     global void finish(Database.BatchableContext BC){
       
     
     }
     

}
String q = 'SELECT Id,name,Account.name FROM Opportunity where Account.name = Wadia';
String s = 'Opportunity';
String f = 'Account.name';
String v = 'Bai Jerbai Wadia Hospital for children';
Id batchInstanceId = Database.executeBatch(new b1(q,s,f,v), 5);
User-added image
i want to change my Account name from wadia to Bai Jerbai Wadia Hospital for children
plz help me

In Advance Thanks

 
Nayana KNayana K
We can't update parent and child in one DML statement in this case, this has to be seperately done.

You can directly query on Account :
String q = 'SELECT Id,name FROM Account where Name = \'Wadia\''; 
String s = 'Account'; 
String f = 'Name'; 
String v = 'Bai Jerbai Wadia Hospital for children'; 
Id batchInstanceId = Database.executeBatch(new b1(q,s,f,v), 5);

If you want to update only those accounts whose Name = Wadia and have atleast one child opportunity then,
 
String q = 'SELECT Id,name FROM Account where Name = \'Wadia\' AND Id IN (SELECT AccountId FROM Opportunity)'; 
String s = 'Account'; 
String f = 'Name'; 
String v = 'Bai Jerbai Wadia Hospital for children'; 
Id batchInstanceId = Database.executeBatch(new b1(q,s,f,v), 5);

 
Dilip_VDilip_V
Rana,

Your query is malformed at where condition(Account.name = Wadia).
Account.name = 'Wadia'


use below query .
String q = 'SELECT Id,name,Account.name FROM Opportunity where Account.name = \'Wadia\'';

Make it as best answer if it helps.
Thanks.
 
Dev Rana 16Dev Rana 16
Hey Nayana,
       thanks for your suggation, But as per your suggation i done changes it will update both Account As well as Opportunity.
  i want to update only in Opportunity.
 so can u please give me suggation for that.