Triggers 尝试拥有唯一的机会LineItem APEX-CODE

Triggers 尝试拥有唯一的机会LineItem APEX-CODE,triggers,salesforce,apex-code,Triggers,Salesforce,Apex Code,我目前正在为OpportunityLineItem开发触发器,Salesforce上的每个产品都是我们的“基本”产品 当销售人员将产品添加到opportunity中时,他还需要输入mpn(=产品的唯一ID),他们将致电我们的网站获取实际价格,因为实际价格取决于产品上设置的每个选项。 我的触发器正在调用一个类来发出请求,到目前为止它还在工作! 但是,当我想添加相同的productID和mpn时,它将不起作用 问题: 销售人员将添加一个产品OpportunityLineItem,但此产品已在其Opp

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

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

问题:

销售人员将添加一个产品OpportunityLineItem,但此产品已在其Opportunity的当前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;
    }
}

由于您在触发器中循环遍历每个行项目,请传递行项目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;
  }
}

由于您在触发器中循环遍历每个行项目,请传递行项目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;
  }
}