1

I've a Rest Resource in salesforce which creates records in salesforce. And i've a demo canvas app hosted on Heroku. On that app, i'm authenticating using Single Request and trying to query data(which is working fine). But, when i try to call the rest resource, it is not calling and sending the status back as '0'. Can some one help me calling the rest resource. Below is: 1) Rest Resource
2) Working code: Querying data.
3) Not working: Hitting Rest Service/Resource.

1) Rest Resource Code:

@RestResource(urlMapping='/insertEXTArt/*')
global class InsertArticles 
{
    @HttpPost
    global static String newArticles()
    {
      System.debug('___ping happened___');
        RestRequest req = RestContext.request;

         Account obj = new Account();

       obj.Name='articleNamefff';
       obj.PR_DEV__Reference__c = 'fffreference';

       insert obj;

        return  'success';       
    }  
}

2) Single Request Authentication(Working):

<%
    // Pull the signed request out of the request body and verify/decode it.
    Map<String, String[]> parameters = request.getParameterMap();
    String[] signedRequest = parameters.get("signed_request");
    if (signedRequest == null) {%>
        This App must be invoked via a signed request!<%
        return;
    }
    String yourConsumerSecret=System.getenv("CANVAS_CONSUMER_SECRET");
    String signedRequestJson = SignedRequest.verifyAndDecodeAsJson(signedRequest[0], yourConsumerSecret);
%>

3) Querying(Working):

function doFunction(){
                var csId =sr.context.environment.parameters.caseId;
                  var queryUrl= sr.context.links.queryUrl+"?q=SELECT+CaseNumber,+Type,+Subject+from+case+where+id='"+csId+"'";
                  Sfdc.canvas.client.ajax(queryUrl,
                          {client : sr.client,
                          success : function(data){
                        //alert(data.payload.records[0].caseNumber);
                                          }});
              } }

**4) Calling Rest Resource(Not Working):**

        function doFunctionAttachRest(){

        var url = "https://ap1.salesforce.com/services/apexrest/PR_DEV/insertEXTArt/newArticles";
        var body = null;
           Sfdc.canvas.client.ajax(url,
             {client : sr.client,
               token : sr.oauthToken,
               method: 'POST',
               contentType: "application/json",
               data: '{"Name" : "Californias"}',
               success : function(data) {
                  alert('requst sent'+data.status);
               if (201 == data.status) {
                    alert("Success");
                    }
               }
             });
}}
4
  • Did you make sure that rest resource is working properly?
    – Ashwani
    May 19, 2014 at 14:01
  • Don't you mean 200 ==, not 201 ==?
    – sfdcfox
    May 19, 2014 at 15:13
  • the alert placed before if statement itself alerting "requestsent0"
    – user7913
    May 20, 2014 at 6:45
  • @regal - how to make sure whether the rest resource is working properly or not?
    – user7913
    May 20, 2014 at 6:46

1 Answer 1

1

My guess is that the REST Resource is not set up correctly, and that you are getting an error on the salesforce server side that you are not catching (just a guess). Then, perhaps the connection is getting closed instead of returning a response (which is the only way a status code of 0 could be returned AFAIK).

It also looks like you are constructing the POST incorrectly. The sr.oauthToken, is not a valid object, and is likely undefined. If you wanted to send the OAuth token, you would use sr.client.oauthToken instead. However, since you are sending the client object, you do not need to also send the token (the token is in the client).

I was able to get this to work without issue with the following:

Apex REST

@RestResource(urlMapping='/testRest/*')
global with sharing class licenseRest {
    // Set up as HTTP GET
    @HttpPOST
    global static String doPost() {
        return 'Why, Hello There POST!';         
    }
}

Client Side Code

function apexRestPost() { 
    var url = "/services/apexrest/jde_canvas/islicensed";
    Sfdc.canvas.client.ajax(url,
        {client : sr.client,
         method: 'POST',
         contentType: "application/json",
         success : function(data) {
             Sfdc.canvas.byId('arstatuspost').innerHTML = data.status;
         }
        }
    );
};

Hope this helps.

You must log in to answer this question.

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