尝试拥有独特的OpportunityLineItem APEX-CODE

时间:2012-06-29 13:56:47

标签: triggers salesforce apex-code

我目前正在开发OpportunityLineItem的触发器,Salesforce上的每个产品都是我们的“基本”产品。

当销售人员将产品添加到商机时,他还需要输入mpn(=产品的唯一ID)来调用我们的网站以获得实际价格,因为实际价格取决于设置的每个选项产品。 我的触发器是调用一个类来发出请求,到目前为止它正在工作! 但是,当我想添加相同的productID和mpn时,它将无法工作。

问题:

推销员会添加一个产品OpportunityLineItem,但是这个产品已经在他当前的机会的OpportunityLineItem中,所以它不起作用。

首先,我的触发器不会得到价格,因为我的SOQL请求将返回多个结果。

这是我的触发器:

trigger GetRealPrice on OpportunityLineItem (after insert) {
    for(OpportunityLineItem op : Trigger.new){
        RequestTest.getThePrice(op.Id_UAD__c,op.MPN__c,op.OpportunityId);  
    }
}

这里是被叫类;

public class RequestTest {
    //Future annotation to mark the method as async.
    @Future(callout=true)
    public static void getThePrice(Decimal idUad, String mpnProduct,String opID){
        // Build the http request
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://www.site.com/mpn.getprice?id='+idUad+'&mpn='+mpnProduct);
        req.setMethod('GET'); 

         String result;
         HttpResponse res = http.send(req);
         System.debug(res.getBody());
         result = res.getBody();
          Decimal price = Decimal.valueof(result);

         System.debug(opID);
         OpportunityLineItem op = [SELECT UnitPrice FROM OpportunityLineItem 
                                  WHERE Id_UAD__c = :idUad
                                  AND OpportunityId = :opID 
                                  AND MPN__c = :mpnProduct] ;
        System.debug('you went through step1');

        op.UnitPrice = price;
        System.debug('This is the opportunity price'+op.UnitPrice);
        update op;
    }
}

1 个答案:

答案 0 :(得分:0)

由于您循环遍历触发器中的每个订单项,因此请传递订单项ID。然后更改您的方法以处理每个订单项的更新:

trigger GetRealPrice on OpportunityLineItem (after insert) {
  for(OpportunityLineItem op : Trigger.new){
    RequestTest.getThePrice(op.Id_UAD__c,op.MPN__c,op.Id);  
  }
}

...

public class RequestTest {
  //Future annotation to mark the method as async.
  @Future(callout=true)
  public static void getThePrice(Decimal idUad, String mpnProduct,String opLineID){
    ...
    Decimal price = Decimal.valueof(result);
    System.debug(opID);
    OpportunityLineItem op = [SELECT UnitPrice FROM OpportunityLineItem 
                              WHERE Id = :opLineID] ;

    op.UnitPrice = price;
    System.debug('This is the opportunity price'+op.UnitPrice);
    update op;
  }
}