0

I'm trying to save an AuraDefinition from within a native Visualforce app. Therefore I following @MartyC. suggestion here: Lightning ToolingAPI BETA Documentation

As an unfortunate PATCH requests seems not to be possible from APEX according to this: How can I make a PATCH HTTP Callout from Apex?

So I'm trying to go with JavaScript but having issues with the endpoint URL. Assume 0Ad20000000PAw7CAG is an valid Id for an AuraDefinition TestApp.css of the Bundle TestApp and app.session.idMergeField contains a valid session-Id.

saveAura = function() {
    var binding = new XMLHttpRequest();
    var request = ' { "Source" : ".THIS { color: red; }" } ';
    binding.open('PATCH', 'https://elfcodefusion.eu0.visual.force.com/sobjects/AuraDefinition/0Ad20000000PAw7CAG/');
    //binding.open('PATCH', '   https://emea.salesforce.com/sobjects/AuraDefinition/0Ad20000000PAw7CAG/');
    binding.setRequestHeader('Authorization', 'Bearer ' + app.session.idMergeField );
    binding.setRequestHeader('Content-Type', 'application/json');
    binding.onreadystatechange = function() { 
        if(this.readyState==4) { console.log(this.response); }
    }
    binding.send(request);
}

No matter what URL I try, I got one of the following errors:

  • https://elfcodefusion.eu0.visual.force.com ==> 302 Found (it seems that it tries to reroute to https://emea.salesforce.com/sobjects/AuraDefinition/ but fails)
  • https://emea.salesforce.com ==> Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://emea.salesforce.com/sobjects/AuraDefinition/0Ad20000000PAw7CAG/

Any ideas on how to make that PATCH work? Is it possible at all? If not, what would be the next best approach to try? SOAP Tooling flavor? Or only MetadataAPI?

1 Answer 1

2

I made a very stupid mistake composing the URL, now it works.

Wrong:

binding.open('PATCH', 'https://elfcodefusion.eu0.visual.force.com/sobjects/AuraDefinition/0Ad20000000PAw7CAG/');

Right:

binding.open('PATCH', 'https://elfcodefusion.eu0.visual.force.com/services/data/v32.0/tooling/sobjects/AuraDefinition/0Ad20000000PAw7CAG/');
3
  • Glad to see that you figured it out - was just about to start looking into this :-) Commented Dec 1, 2014 at 16:31
  • I was only messing up with the URL, thats all... it seems now to work very well, so CodeFusion is now able to read and write Lightning :-) and by the way it's fast as hell! The PATCH via javascript saves AuraDefinitions in 0.2 to 0.4 seconds. With Apex, VF-pages and StaticResources we were happy if we could make 2.0 to 3.0 seconds or so.
    – Uwe Heim
    Commented Dec 1, 2014 at 17:36
  • We implemented the Tooling API for Lightning Components backend differently that those others because I refused to go with the de facto slower approach and I hope other meta objects will follow our example :-) Commented Dec 1, 2014 at 23:49

You must log in to answer this question.

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