function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AntonyasLenAntonyasLen 

Call an URL/Web Service from Salesforce to fill up a field

Hi,

 

I'm a beginner with APEX, i just finished to administrate my company Salesforce but we need some APEX class and Trigger to be able to use Salesforce as we wish.

 

We have the basic product, and when the salesman will add a product to an opportunity we will ask him to put a "mpn number". So we want to use the mpm number who will define the real price of the product(it's a particular number linked to a product depends on the customization of this product)

 

The administrator of ou e-commerce give me an URL as this one

 

http://mycompany.com/mpn.getprice?id=[produit_uad_id]&mpn=[code_mpn]

 

i saw this:

public class HttpCalloutSample {
    // Pass in the endpoint to be used using the string url
   
    public String getContent(String url) {
        // Instantiate a new http object
        Http h = new Http();
        
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        return res.getBody();
    }
}

 but i still don't undsertand of i can use it with my opportunity product.

 

I'll try to find by myself, but if someone an help me it would be great!

Noam.dganiNoam.dgani
The sample That you posted should do the trick. Look at your response.getbody() using system.debug to understand the format, extract the data that you want from it and populate the appropriate field in opp li e item. You will want to perform the fallout on insert/update of the line item so a trigger would work great (callout will be asynchronous though).
AntonyasLenAntonyasLen

Hi,

I tried but it look like not working.

I wanted to use the code below:

 

trigger GetRealPrice on OpportunityLineItem (after insert) {
    Decimal uad;
    String mpn;
    Double finalprice;
    
    List<OpportunityLineItem> productline = [SELECT Id_UAD__c, MPN__c,UnitPrice FROM OpportunityLineItem] ;
   
    for(OpportunityLineItem op : productline){
        finalprice = getThePrice(op.Id_UAD__c,op.MPN__c);
        op.UnitPrice = finalprice;
        update op;
    }
    
    public Double getThePrice(Decimal idUad, String mpnProduct){
        
        // Build the http request
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://usineadesign.com/mpn.getprice?id='+ idUad +'&mpn='+ mpnProduct);
        req.setMethod('GET'); 
        
        String result;
        HttpResponse res = h.send(req);
        result = res.getBody();
        Double price = Double.valueof(result);
        return price;
    }   
}

 and i got this error message:

*************************************************

 

Apex script unhandled trigger exception by user/organization: 005E0000000iaWr/

00DE0000000Z60w

GetRealPrice: execution of BeforeInsert

caused by: System.CalloutException: Callout from triggers are currently not supported.

Trigger.GetRealPrice: line 23, column 1
Trigger.GetRealPrice: line 9, column 1
***************************************************
Noam DganiNoam Dgani

There are some fundemental issues with your code and what you are trying to do:

 

  •  look at the error message - it tells you that you are not allowed to perform callouts (like httprequest) from a       trigger. the way to do that is in a method defined as @future called from your trigger                                                                  (read a little about @future to get familiar).
  • why r u querying for all opportunity line items in the system - you have the newly inserted line items in a list called      'trigger.new' (read about that as well). 
  • you are iterating over all your line items and performing a callout for each - that wont work becuase you will hit                   the10 callouts max per transaction limit really quick - for that you have two options:

 

1. find out if the API that you are calling to get the price has bulk mode (you send 50 items and you get                                     data for 50 items back (or 100 or 200, you get the point - more than one at a time).

2. perform a callout for each but you have have to use batch apex (also some good reading material).

 

 

Hope this sets you on the right path.

 

AntonyasLenAntonyasLen

i tried with a method alled @future but it still doesn't work.

 

I undestood, so my request is pretty bad, i was thinking that it will only take the opportunity line items from my current opportunity.

So i should make an other request , hmm for example

Id_UAD__c, MPN__c,UnitPrice FROM OpportunityLineItem 

WHERE Opportunity IN (SELECT OpportunityLineItem  FROM Opportunity )

 

??

AntonyasLenAntonyasLen

my request still bad :s i tried to understand how can i get the OpportunityLineItem of my current Opportunity

Noam DganiNoam Dgani

what do you mean by current opportunity?

 

you are trying to update OpportunityLineItems - why do you need the opportunity?

 

Additionally, what if there are more than 10 line items in the trigger?

 

The correct way to do what you want (if there is no bulk API for whoever gives you prices)

is using batch apex - the rest of the attempts would really be a waste of your time.

AntonyasLenAntonyasLen

Ok,

 

so every product on Salesforce are our "basic" products.

When a salesman will add a product to an opportunity he will also need to put the mpn( = unique ID for a product) who will call our website to get the real price, because the real price depend of every option set on the product .Salesforce doesn't allow us to put all of our product (because we have so many option on product).

 

So when i was talking about "the current opportunity" my meaning was the opportunity where my salesman is currently adding new product.

 

In trigger we can just work on 10 line?

 

Batch look the same isn't it?( i learn by working on the APEX workbook but i guess that i didn't really understood all in an half day)

 

AntonyasLenAntonyasLen
for(OpportunityLineItem op : Trigger.new){
    RequestTest.getThePrice(op.Id_UAD__c,op.MPN__c);  
    }

 in the Trigger will call this new class

public class RequestTest {
    //Future annotation to mark the method as async.
    @Future(callout=true)
    public static void getThePrice(Decimal idUad, String mpnProduct){
        
        // Build the http request
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://usineadesign.com/mpn.getprice?id='+ idUad +'&mpn='+ mpnProduct);
        req.setMethod('GET'); 
        
        String result;
        HttpResponse res = h.send(req);
        result = res.getBody();
        System.debug(result);
        Decimal price = Decimal.valueof(result);
        
        OpportunityLineItem op = [SELECT Id_UAD__c, MPN__c,UnitPrice FROM OpportunityLineItem WHERE Id_UAD__c = :idUad] ;
        op.UnitPrice = price;
        update op;
    }
}

 but i know that something still doesn't work


AntonyasLen wrote:

Ok,

 

so every product on Salesforce are our "basic" products.

When a salesman will add a product to an opportunity he will also need to put the mpn( = unique ID for a product) who will call our website to get the real price, because the real price depend of every option set on the product .Salesforce doesn't allow us to put all of our product (because we have so many option on product).

 

So when i was talking about "the current opportunity" my meaning was the opportunity where my salesman is currently adding new product.

 

In trigger we can just work on 10 line?

 

Batch look the same isn't it?( i learn by working on the APEX workbook but i guess that i didn't really understood all in an half day)