Sunday, 19 June 2016

How to handle a 301 redirect response to my HTTP callout

While trying out https callounts we typically see response with Status=Moved Permanently, StatusCode=301. Below is the code sample which gives us error:
Http h = new Http();
HttpRequest req = new HttpRequest();
     
req.setHeader('Authorization', 'bearer ' + 'xxxxxxxxxx');
req.setEndpoint('https://www..com/v1.2/accounts');        
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json; charset=UTF-8');
req.setTimeout(60*1000);
HttpResponse res = h.send(req);
String resString = res.getBody();
This errors generally occurs when target point we hit for, has permanently moved to a new location so in this case response should include this location. to fix this error we just need to get location out of first response and hit it again.So Finally code should be like below:

Http h = new Http();
HttpRequest req = new HttpRequest();
       
req.setHeader('Authorization', 'bearer ' + 'xxxxxxxxxx');
req.setEndpoint('https://www..com/v1.2/accounts');        
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json; charset=UTF-8');
req.setTimeout(60*1000);
HttpResponse res = h.send(req);
String resString = res.getBody();
String loc = res.getHeader('Location'); // get location of the redirect
req = new HttpRequest();
req.setEndpoint(loc);
req.setMethod('GET');
req.setHeader('Authorization', 'bearer ' + 'xxxxxxxxxx');
        res = h.send(req);

Now it gives Status code 200 with Status "OK"

Tuesday, 15 March 2016

Delete Components before and after Component Updates in a deployment in Salesforce

Guys, As we all know that Ant is such a great migration tool. I have described how we can set-up ANT on our local machine and migrate desired component to Salesforce org.

Today I am going to describe how we can delete component from salesforce org using ANT. Salesforce has provided a way of achieving it through some well defined desctructiveChanges.xml .

Later on, it would took two at least migration attempts for clearing-up dependencies between components and deleting those respectively.
For example if one custom field is being used by an apex class then we can not delete that field until we remove that field's active presence from apex class. For this purpose, Salesforce unveiled two new type of xmls

1. destructiveChangesPre.xml
2. destructiveChangesPost.xml

Lets understand with an example. Let say there are two fields amount__c,department__c on Account Object and there is one class ShowingResult that use one field amount__c.
Public class ShowingResult {

   Public void ShowingResult()
   {
       Account acc = new Account{Name = 'testAcc', amount__c = 1232};
       insert acc;    
   }

}

In order to delete these two fields we need to make some required changes in apex class first. then system would allow us to delete amount__c field. Generally it would take two steps but the way is described below would do our job in one attempt.

Here is your modified Apex Class:
Public class ShowingResult {

   Public void ShowingResult()
   {
       Account acc = new Account{Name = 'testAcc'}; // with modification
       insert acc;    
   }

}

Now we just need to create three xml files(as below) and run these.System would run desctructiveChangesPre.xml then package.xml for updating component where we have removed dependencies. finally it would run desctructiveChangesPost.xml.
Your desctructiveChangesPre.xml

    
        Account.department__c
        CustomField
    
    36.0

Your Package.xml:

    
        ShowingResult
        ApexClass
    
    36.0

Your desctructiveChangesPost.xml:

    
        Account.amount__c
        CustomField
    
    36.0

Keep these xml in src folder and run migration.