Skip to main content

For example I have object Om  and I wanted To pass value of address filed from object Om to Rom object in Rom object's Address Filed but there is no Relationship between them how can I achieve through Apex?

3 个回答
  1. 2023年1月10日 13:19

    Hi

     

    Yes, it is possible to update a field in another object without a direct relationship using Apex in Salesforce. You can do this by using a query to retrieve the records you want to update, and then using a for loop to iterate through the records and update the desired field.

     

    Example code:

    List<Om__c> omRecords = [SELECT Id, Address__c FROM Om__c WHERE ...];

    List<Rom__c> romRecordsToInsert = new List<Rom__c>();

    for (Om__c omRecord : omRecords) {

    Rom__c newRomRecord = new Rom__c();

    newRomRecord.Address__c = omRecord.Address__c;

    romRecordsToInsert.add(newRomRecord);

    }

    insert romRecordsToInsert;

    Please note that if you are updating a large number of records, you should use the proper governor limits to avoid hitting any of the Salesforce Governor limits such as 'Too many DML rows: 10001' or 'Too many SOQL queries: 101'.

     

    You can also use the External Id to update records with relationships as well.

     

    Please mark it as the best answer, if it helps you to solve the query.

     

    Thanks!

0/9000