Skip to main content
Vivekh R ha fatto una domanda in #Data Management
I need to upsert field values from Product to Deal. I dont know how to get ID from Product and update the field in Deal and to insert new records if ID does not exist.

 

This is my code:

 

public class sample

 

{

 

public void sam()

 

{

 

     Country__c period = [select Id,CurrentSystemMonth__c,CurrentSystemYear__c from Country__c where CountryCode__c='UK'];

 

     List<Product__c> prod= new List<Product__c>();

 

     prod=[select Id,Amount__c,Customer__c,Period__c from Product__c

 

      where Status_code__c = 'APR' and Syetem_Month__c =: period.CurrentSystemMonth__c and

 

      System_Year__c =: period.CurrentSystemYear__c];

 

     List<Deal__c> ded = new List<Deal__c>();

 

     Deal__c pay=new Deal__c();

 

   

 

       for(Product__c adj : prod)

 

        {

 

          pay.Adjustment_value__c=adj.Amount__c;

 

          pay.Adjustment_VAT__c=adj.VAT__c;

 

          pay.CustomerNo__c=adj.ERS_Customer__c;

 

          ded.add(pay);

 

        }

 

       if(ded.size()>0)

 

      {

 

          upsert ded;

 

      }

 

        

 

   

 

    }

 

}
9 risposte
  1. 6 gen 2014, 13:36
    Hi Vivekh, you need to specify the external key for the upsert statement....otherwise it will read as insert operation.

     

    So for example, let's say its the customer no field the upsert should read:

     

    update ded customerno__c;

     

    Another note...when you initialise the Deal object (Deal__c pay=new Deal__c();)...do that inside the loop if you are wanting to initalize multiple objects to add to the list. 

     

    reference for apex: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_upsert.htm
0/9000