【问题标题】:praogramatically fetch any particular object's related objects from its related list以编程方式从其相关列表中获取任何特定对象相关对象
【发布时间】:2013-11-06 00:56:11
【问题描述】:

我对 SFDC 很陌生。我正在尝试实现自定义对象的克隆功能,当我克隆对象时,该对象及其相关列表中的所有对象都将被克隆。我已经实现了克隆对象的部分,但卡住了如何获取与对象的相关列表关联的对象列表。请告诉我,如何实现。

谢谢

【问题讨论】:

  • 请提供相关代码。

标签: salesforce salesforce-service-cloud


【解决方案1】:

你可以试试这个……

公共类 PurchaseOrderCloneWithItemsController {

//added an instance varaible for the standard controller
private ApexPages.StandardController controller {get; set;}
 // add the instance for the variables being passed by id on the url
private Purchase_Order__c po {get;set;}
// set the id of the record that is created -- ONLY USED BY THE TEST CLASS
public ID newRecordId {get;set;}

// initialize the controller
public PurchaseOrderCloneWithItemsController(ApexPages.StandardController controller) {

    //initialize the stanrdard controller
    this.controller = controller;
    // load the current record
    po = (Purchase_Order__c)controller.getRecord();

}

// method called from the VF's action attribute to clone the po
public PageReference cloneWithItems() {

     // setup the save point for rollback
     Savepoint sp = Database.setSavepoint();
     Purchase_Order__c newPO;

     try {

          //copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
         po = [select Id, Name, Ship_To__c, PO_Number__c, Supplier__c, Supplier_Contact__c, Date_Needed__c, Status__c, Type_of_Purchase__c, Terms__c, Shipping__c, Discount__c from Purchase_Order__c where id = :po.id];
         newPO = po.clone(false);
         insert newPO;

         // set the id of the new po created for testing
           newRecordId = newPO.id;

         // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
         List<Purchased_Item__c> items = new List<Purchased_Item__c>();
         for (Purchased_Item__c pi : [Select p.Id, p.Unit_Price__c, p.Quantity__c, p.Memo__c, p.Description__c From Purchased_Item__c p where Purchase_Order__c = :po.id]) {
              Purchased_Item__c newPI = pi.clone(false);
              newPI.Purchase_Order__c = newPO.id;
              items.add(newPI);
         }
         insert items;

     } catch (Exception e){
         // roll everything back in case of error
        Database.rollback(sp);
        ApexPages.addMessages(e);
        return null;
     }

    return new PageReference('/'+newPO.id+'/e?retURL=%2F'+newPO.id);
}

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2017-11-05
    • 2018-04-04
    • 1970-01-01
    • 2011-02-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    相关资源
    最近更新 更多