0

I need the send PUT request to my SalesforceMiddleware project using with javascript button, with that situation i used the resource below:

sforce.connection.remoteFunction https://resources.docs.salesforce.com/sfdc/pdf/apex_ajax.pdf

can anyone help me ?

{!RequireScript("/soap/ajax/11.1/connection.js")}

var productCode = '{!Product2.ProductCode}';

if (productCode != '') {
    sforce.connection.remoteFunction({
    url : "{DELETED}/"+productCode,
    requestHeaders: {"Content-Type":"application/json"},
    method: "PUT",
    requestData: productCode,
    onSuccess : function (result){console.log('success')},
    onFailure : function (error) {console.log('error')},
 });
} else {
    alert('Select one or more Contacts');
}

Edit: by the way i getting HTTP 400 Bad Request message

Console log

1 Answer 1

2

I could not get any answer to my question that's why i found another way. It's some challenging, but working.

I'm sharing my HttpRequestPostOrPut apex class:

global class HttpRequestPostOrPut {
webservice static string request(string url, string method, string contentType)
{
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(url);
    request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    request.setHeader('Content-Type', contentType);
    request.setMethod(method);
    HttpResponse response = http.send(request);
    
    system.debug(response.getBody());
    
    if(response.getStatusCode() == 200) {
        return response.getBody();
    }
    
    return null;
    }
}

And mockTestClass

    @isTest(SeeAllData=true)
global class HttpRequestPostOrPutMock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) 
        {
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setStatusCode(200);
            return res; 
        }
    
    @IsTest
    public static void HttpRequestPostOrPutTest() 
    {
    Test.setMock(HttpCalloutMock.class, new HttpRequestPostOrPutMock());
   
    HttpRequestPostOrPut.request('{DELETED}/{sku}', 'PUT', 'application/json');
    }
}

And js button execute command

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

requestUrl = "{Deleted}/{!Product2.ProductCode}";

var result = sforce.apex.execute("HttpRequestPostOrPut","request",{url: requestUrl, method:"PUT", contentType: "application/json"});

location.reload();

I hope I can help someone with this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .