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
pradeepkumar battapradeepkumar batta 

Regarding Batch class

Hi Everyone,

I have a requirement that is there is two objects ObjX,ObjY but no relation between objects ,My requirement is to update a amount field value in objX with Cash field value in ObjY,

ObjX                           ObjY

amount__c                Cash__c

Please suggest me how to write only batch class not trigger .
Shashi PatowaryShashi Patowary
Hi Pradeep,

Your requirement is not so much clear. To understand this, let us take an scenario - Customers buy apartment/flat from builders and they take loan from Banks to get the money.
To relate to your case - your ObjX is Builder and ObjY is Bank.
Builder__c                Bank__c
amount__c                Cash__c
Customer__c             Customer__c
Here even if Builder and Bank are not directly related, yet they have a common field - Customer

If this is the scenario, you need to query Cash__c.Account__c field from all Bank object records in your batch (Batch can hadle upto 50 millions records).This will be your batch query. Based on your incoming bank records, prepare a Map like this -

bankMap (Customer__c, Bank__c)
Then query the raletd bulders record based on this maps key value (customers) .Loop though all the builder records and update related bulilder amount__c fiels with related Cash__c field from bank somewhat like this -
//Create a builder list to be update
List<Builder__c> builderList = New List<Builder__c>();
//Fetch relevant builder records
for (Builder__c build : [SELECT amount__c, Customer__c FROM Builder__c where Customer__c in :bankMap.keySet()]) {
  if (build.Customer__c.containsKey(bankMap)) {
     build.amount__c = bankMap.get(build.Customer__c).Cash__c;
     builderList.add(build);
  }
}
//update builder records
If (builderList.size() > 0) {
  update builderList;
}

Hope I was able to get your requirement. Please let me know if it works for you.

Thanks,
Shashi
Raja236Raja236
Hi,

creat a externalid text field on object x and in batch class pass some unique value such as a ObjY's ID to make a relation ship and update the cash to amount 

 
pradeepkumar battapradeepkumar batta
Thanks 
Shashi Patowary

i mapped my two objects and updated, but one more requirement is when we update fields on objY successfully then i need to update some field on objX .please let me know logic .