2

I have three objects, namely Tests, Grant, and Grant Contracts.

Here in Tests object I have given a lookup to Grant and Grant Contract. I have also given a lookup in Grant Contract to the Grant object. In the lookup fields in all the test records, I have the grant record id.

My aim is to update all the grant contract fields in the test records, and these grant contracts should be of the parent grant object. So if I have a Tests record with Grant G and Grant Contract GC, I should get the tests record's grant contract field filled with the GC child record present in the Grant G.

I am planning to use anonymous code for updating as there is a bulk amount of records. Please provide an input on how to start. I have started this way.

       List<Test__c> TestList =[Select id,name,Grant__c from Test__c where Grant__c!= null];
    System.debug('Test==>'+TestList);
    System.debug('TestList==>'+TestList.size());
    Set<Id> grantId = new set<Id>();
    Set<Id> grantContractId = new set<Id>();
    
    For(Test__c t :TestList) {
    grantId.add(t.Grant__c);
    }
    System.debug('grantId==>'+grantId);
    System.debug('grantId Set==>'+grantId.size());




***Modified code and debug logs after the valuable answers from the comments.***




List<Test__c> TestList =[Select id,name,Grant__c,Grant_Contract__c from Test__c where Grant__c!= null];
System.debug('Test==>'+TestList);
System.debug('TestList==>'+TestList.size());

Map<Id, Grant_Contract__c> grantContractRecordsById = new Map<Id, Grant_Contract__c>();
System.debug('grantContractRecordsById');
    for(Test__c record: [
    SELECT Grant__c,Grant_Contract__c
    FROM Test__c
    WHERE Grant__c!=null AND Grant_Contract__r.Grant_Name__c=null
    LIMIT 10000])
    System.debug('record>=='+record);
        {
  grantContractRecordsById.put(
    record.Grant_Contract__c,
    new Grant_Contract__c(
      Id=record.Grant_Contract__c,
      Grant_Name__c=record.Grant__c
    )
  );
}
update grantContractRecordsById.values();  

enter image description here

1 Answer 1

2

That code would go something like this:

// Keep a collection of Grant_Contract__c records by Id, in case of duplicates
Map<Id, Grant_Contract__c> grantContractRecordsById = new Map<Id, Grant_Contract__c>();
// SOQL for loop 
for(Test__c record: [
    SELECT Grant__c,Grant_Contract__c 
    FROM Test__c 
    WHERE Grant__c != null AND 
          Grant_Contract__c != null AND 
          Grant_Contract__r.Grant__c = null
    LIMIT 10000]) {
  // Put a Grant_Contract__c record in the collection for updates.
  // Storing by Id ensures we don't get duplicates.
  grantContractRecordsById.put(
    record.Grant_Contract__c, 
    new Grant_Contract__c(
      Id=record.Grant_Contract, 
      Grant__c=record.Grant__c
    )
  );
}
// Save the changes to the database.
update grantContractRecordsById.values();

Be aware there's a 50k row limit for queries and 10k row limits for direct DML operations, which means you may need to run this script multiple times and/or adjust the limits.

7
  • Should I just add this with my code.Can you please provide the merged code.I have understood till the query.Can you explain from the grantContractRecordsById,put(
    – JK123
    May 16 at 14:41
  • 1
    @JK123 I edited in some comments that should help clear up some confusion.
    – sfdcfox
    May 16 at 14:45
  • @JK123 Your question, at least to me, was worded in a way that suggested you were not using any standard objects. Of course, if you're standard objects, you'd change the names appropriately.
    – sfdcfox
    May 16 at 15:13
  • Thanks Sir I have figured and corrected it.Only one error remains. Line: 20, Column: 1 System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
    – JK123
    May 16 at 15:16
  • @JK123 That means that, for whatever reason, you don't have an Id in the record. Make sure that you're including the ID.
    – sfdcfox
    May 16 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.