Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to convert lead into account? #64

Open
sfdaniele opened this issue Dec 4, 2017 · 14 comments
Open

Ability to convert lead into account? #64

sfdaniele opened this issue Dec 4, 2017 · 14 comments

Comments

@sfdaniele
Copy link

Hi! I wasn't sure what was the best way to contact you.

Is there a way to convert a lead into an account through the client? If so, would you be able to add it to the readme as an example?

Thanks!

@jesperfj
Copy link
Owner

jesperfj commented Dec 4, 2017

I don't think this is supported by REST API. A quick Google brings up workarounds like this one. If you think there's a way to do it in REST API, let me know and I'll take a look at docs.

@sfdaniele
Copy link
Author

Thanks for the quick answer!

I had seen that, what's unclear to me is whether this class is just patching a lead and triggering a conversion or whether it's doing something more than that through the API upon calling Database.convertLead.

@jesperfj
Copy link
Owner

jesperfj commented Dec 4, 2017 via email

@sfdaniele
Copy link
Author

Ok. The answer here seems to suggest that there is an endpoint called your_instance_url/services/apexrest/convertlead where I can POST {"leadId" : lead_id}. Is my understanding correct? Do the endpoints under Apex Rest use the same authentication mechanism as the Force API?

If so, I'd have to re-use your authentication and client infrastructure to make that call. That most likely would require me to send a PR to you and change the client.

If all of the above is correct, could I do that? I'd have to dig into your code to figure out where to plug in.

Thanks!

@jesperfj
Copy link
Owner

jesperfj commented Dec 4, 2017 via email

@sfdaniele
Copy link
Author

Got it! I'll play around with it and report back, if it works perhaps we can add it to the README for future reference.

@sfdaniele
Copy link
Author

sfdaniele commented Dec 4, 2017

Alright, I'm making some progress but encountering issues as well.

First, I had to temporarily work around ForceApi because the public request method prepends uriBase() to the path (session.getApiEndpoint()+"/services/data/"+config.getApiVersionString()).

The work around is ugly and it involves calling the private method apiRequest directly (it looks like the interface of ForceApi would need to change for this to work properly).

The second issue is that I can't seem to be able to call Apex.

  @Override public void convertLead(String salesforceLeadId) {
    try {
      // This is the ugly workaround
      Method method = forceApi.getClass().getDeclaredMethod("apiRequest", HttpRequest.class);
      method.setAccessible(true);
      // For simplicity, just try to GET a Lead through Apex. 
      HttpRequest httpRequest = new HttpRequest()
          .url(forceApi.getSession().getApiEndpoint() + "/services/apexrest/Lead/" + salesforceLeadId)
          .method("GET")
          .header("Accept", "application/json")
          .header("Content-Type", "application/json");
      Object result = method.invoke(forceApi, httpRequest);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
      throw Throwables.propagate(e);
    }
  }

Invoking this causes a Caused by: com.force.api.ApiException: [{"errorCode":"NOT_FOUND","message":"Could not find a match for URL"}]. I've Googled it and it looks like sometimes this error is related to the existence of a namespace that needs to be included in the Apex URL. However, my organization doesn't have a namespace as far as I call tell by looking at the result of forceApi.query("SELECT NamespacePrefix FROM Organization").

@jesperfj I realize that this is getting in the weeds and I really appreciate your help here. It looks like there aren't many resources online to do this in Java so there isn't much documentation to go after.

@sfdaniele
Copy link
Author

To add to my comment above, after reading a bit more about Apex it looks like I'll have to wire up the endpoint to convert a lead on the Salesforce side before I can call it, is that so?

If that's the case, then the solution would be to:

  1. Get a SF Developer account
  2. Create an Apex class that converts a lead to an account and add a rest resource for it
  3. Modify the code above to call this newly created endpoint

@jesperfj
Copy link
Owner

jesperfj commented Dec 4, 2017 via email

@jesperfj
Copy link
Owner

jesperfj commented Dec 4, 2017

Oh, I didn't see your updated response. Yes, you will have to deploy the Apex class first.

@sfdaniele
Copy link
Author

Ok!

@sfdaniele
Copy link
Author

Hi,

I was finally able convert a lead into an account and it works! I'm still having to work around the fact that apiRequest is not public and request prefixes a /services/data path. Right now the only way to use the Apex API is to use apiRequest through reflection.

If possible, it would be awesome to change the ForceApi's interface so that there is a way to call arbitrary (non /services/data) endpoints.

The hardest part of this entire endeavor was figuring out how to create the Apex class and deploy it to production as I'm not that familiar with Salesforce. If you want to add information on how to integrate with Apex in your readme, I've written down some notes here. Feel free to use those.

Thanks!

@jesperfj
Copy link
Owner

jesperfj commented Dec 5, 2017

I see.

When I did the raw crud methods, I reused apiBase() which appends /services/data/<version>. I wonder if Apex REST is the only type of endpoint that doesn't need a version number? That would affect how this should be addressed.

This is certainly a bug. The crud methods were intended to be used with Apex REST for sure.

@sfdaniele
Copy link
Author

I don't know. I'm new to the Salesforce API but I wouldn't be surprised if there were more endpoints under services.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants