• Mohith Kumar Shrivastava
  • SMARTIE
  • 710 Points
  • Member since 2011
  • Lead Developer Evangelist
  • Salesforce


  • Chatter
    Feed
  • 24
    Best Answers
  • 0
    Likes Received
  • 3
    Likes Given
  • 3
    Questions
  • 226
    Replies
In addition to what query title says, when making callout via URL it seems to be working. Please find the code below:
EinsteinOCRPredictionHandler.cls
public class EinsteinOCRPredictionHandler {

    final static String VISION_API = 'https://api.einstein.ai/v2/vision';
    final static String OCREndpoint = VISION_API + '/ocr';
    final static String cmdtDeveloperName = 'Einstein_Vision_API_JWT';
    
    public class EinsteinOCRPredictionHandlerException extends Exception {}

    public static EinsteinOCRPredictionResponseWrapper predictViaURL(String fileURL, String detectionType, String accessToken, String model) {
        String response = detectText(fileURL, detectionType, accessToken, model, false);
        return parseResponse(response);
    }

    public static EinsteinOCRPredictionResponseWrapper predictViaBase64(String base64String, String detectionType, String accessToken, String model) {
        String response = detectText(base64String, detectionType, accessToken, model, true);
        return parseResponse(response);
    }
    
    public static EinsteinOCRPredictionResponseWrapper predictViaBlob(Blob fileBlob, String detectionType, String accessToken, String model) {
        String response = detectText(EncodingUtil.base64Encode(fileBlob), detectionType, accessToken, model, true);
        return parseResponse(response);
    }
    
    public static String detectText(String sample, String detectionType, String accessToken, String model, boolean isBase64) {
        string contentType = HttpFormBuilder.GetContentType();
        //  Compose the form
        string form64 = '';
        form64 += HttpFormBuilder.WriteBoundary();
        form64 += HttpFormBuilder.WriteBodyParameter('modelId', EncodingUtil.urlEncode(model, 'UTF-8'));
        form64 += HttpFormBuilder.WriteBoundary();
        form64 += HttpFormBuilder.WriteBodyParameter('task', EncodingUtil.urlEncode(detectionType, 'UTF-8'));
        form64 += HttpFormBuilder.WriteBoundary();
        if(isBase64) {
            form64 += HttpFormBuilder.WriteBodyParameter('sampleBase64Content', sample);
        } else {
            form64 += HttpFormBuilder.WriteBodyParameter('sampleLocation', sample);
        }
        form64 += HttpFormBuilder.WriteBoundary(HttpFormBuilder.EndingType.CrLf);
        blob formBlob = EncodingUtil.base64Decode(form64);
        string contentLength = string.valueOf(formBlob.size());
        //  Compose the http request
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setBodyAsBlob(formBlob);
        httpRequest.setHeader('Connection', 'keep-alive');
        httpRequest.setHeader('Content-Length', contentLength);
        httpRequest.setHeader('Content-Type', contentType); 
        httpRequest.setMethod('POST');
        //httpRequest.setTimeout(120000);
        httpRequest.setHeader('Authorization','Bearer ' + accessToken);
        httpRequest.setEndpoint(OCREndpoint);
        
        Http http = new Http();
        HTTPResponse res = http.send(httpRequest);
        System.debug(res.getStatus());
        String responseBody;
        Integer statusCode = res.getStatusCode();
        String requestStatus = res.getStatus();
        if ( statusCode == 200) {
            responseBody = res.getBody();
        } else {
            throw new EinsteinOCRPredictionHandlerException('Callout unsuccessful! Status code: '+statusCode + '. Status: '+requestStatus);
        }
        return responseBody;
    }

    static EinsteinOCRPredictionResponseWrapper parseResponse(String response) {
        if(String.isEmpty(response)) {
            throw new EinsteinOCRPredictionHandlerException('Empty response received');
        }
        EinsteinOCRPredictionResponseWrapper obj = new EinsteinOCRPredictionResponseWrapper();
        obj = (EinsteinOCRPredictionResponseWrapper)JSON.deserialize(response, EinsteinOCRPredictionResponseWrapper.class);
        return obj;
    }
}
HttpFormBuilder.cls
public class HttpFormBuilder {
    //  The boundary is alligned so it doesn't produce padding characters when base64 encoded.
    private final static string Boundary = '1ff13444ed8140c7a32fc4e6451aa76d';

    /**
     *  Returns the request's content type for multipart/form-data requests.
     */
    public static string GetContentType() {
        return 'multipart/form-data; charset="UTF-8"; boundary="' + Boundary + '"';
    }

    /**
     *  Pad the value with spaces until the base64 encoding is no longer padded.
     */
    private static string SafelyPad(
        string value,
        string valueCrLf64,
        string lineBreaks) {
        string valueCrLf = '';
        blob valueCrLfBlob = null;

        while (valueCrLf64.endsWith('=')) {
            value += ' ';
            valueCrLf = value + lineBreaks;
            valueCrLfBlob = blob.valueOf(valueCrLf);
            valueCrLf64 = EncodingUtil.base64Encode(valueCrLfBlob);
        }

        return valueCrLf64;
    }

    /**
     *  Write a boundary between parameters to the form's body.
     */
    public static string WriteBoundary() {
        string value = '--' + Boundary + '\r\n';
        blob valueBlob = blob.valueOf(value);

        return EncodingUtil.base64Encode(valueBlob);
    }

    /**
     *  Write a boundary at the end of the form's body.
     */
    public static string WriteBoundary(
        EndingType ending) {
        string value = '';

        if (ending == EndingType.Cr) {
            //  The file's base64 was padded with a single '=',
            //  so it was replaced with '\r'. Now we have to
            //  prepend the boundary with '\n' to complete
            //  the line break.
            value += '\n';
        } else if (ending == EndingType.None) {
            //  The file's base64 was not padded at all,
            //  so we have to prepend the boundary with
            //  '\r\n' to create the line break.
            value += '\r\n';
        }
        //  Else:
        //  The file's base64 was padded with a double '=',
        //  so they were replaced with '\r\n'. We don't have to
        //  do anything to the boundary because there's a complete
        //  line break before it.

        value += '--' + Boundary + '--';

        blob valueBlob = blob.valueOf(value);

        return EncodingUtil.base64Encode(valueBlob);
    }

    /**
     *  Write a key-value pair to the form's body.
     */
    public static string WriteBodyParameter(
        string key,
        string value) {
        string contentDisposition = 'Content-Disposition: form-data; name="' + key + '"';
        string contentDispositionCrLf = contentDisposition + '\r\n\r\n';
        blob contentDispositionCrLfBlob = blob.valueOf(contentDispositionCrLf);
        string contentDispositionCrLf64 = EncodingUtil.base64Encode(contentDispositionCrLfBlob);
        string content = SafelyPad(contentDisposition, contentDispositionCrLf64, '\r\n\r\n');
        string valueCrLf = value + '\r\n';
        blob valueCrLfBlob = blob.valueOf(valueCrLf);
        string valueCrLf64 = EncodingUtil.base64Encode(valueCrLfBlob);

        content += SafelyPad(value, valueCrLf64, '\r\n');

        return content;
    }

    /**
     *  Helper enum indicating how a file's base64 padding was replaced.
     */
    public enum EndingType {
        Cr,
        CrLf,
        None
    }
}


 
Hi ,
I am trying to use setSortBy() method in apex while using Report API. The method is not sorting the columns. 
Please let me know if i am missing anything in here.TIA
Reports.ReportResults results = Reports.ReportManager.runReport('00O11000000l9wC',true);
  Reports.ReportMetadata RM = results.getReportMetadata();
  System.debug('Detail columns: ' + rm.getDetailColumns());
  //Sorting code
  Reports.SortColumn sortcolumn = new Reports.SortColumn();
  sortcolumn.setSortColumn('CAT_Focal_File__c.Workday_Id__c');
  sortcolumn.setSortOrder(ASCENDING);
  System.debug('@@@1 '+results.getAlldata());	
  list<Reports.SortColumn> lstcolumns = new list<Reports.SortColumn>();
  lstcolumns.add(sortcolumn);
  rm.setSortBy(lstcolumns);
  Reports.ReportFactWithDetails factDetails =
    (Reports.ReportFactWithDetails)results.getFactMap().get('T!T');
  List<Reports.ReportDetailRow> allRows = factDetails.getRows();
  string s = (string)allRows.get(0).getDataCells().get(1).getValue();
  system.debug('@@@@@s '+s)

 
What is the recommended approach by Salesforce for mobile application authentication?
 - SAML based authentication?
 - OAuth User-Agent flow?
 - OAuth Username-Password Flow?
 - Session Id flow?

Here is my understanding:
SAML seems out question, because it involves identity provider and in most cases we don't utilize an ID provider.

In User-Agent flow we need a connected app and don't have to provide password at all and access token is sent to pre-defined callback URL. Which is not happening in all of the mobile applications available, such Salesforce1.

Salesforce1 app seems close to OAuth Username-Password Flow but we don't have to provide security token. We can login to any instance with a valid username and password and our IP is not white listed either. How can we authenticate without a security token? I believe there is a browser plugin running in the Salesforce1 app and we have to tap 'Allow' access after login and have to provide verification code as well (one time only). Tapping 'Allow' and verification code is alternative to security token?

I don't know anything anout session Id flow and didn't find any details.

I am NOT looking for code, just a general opinion about the best approach from the four option above.

Thanks in advance.

Usman
I have the following method returning a changed owner id when a person selects a new value from lightning component. I want to use this ownerId in a method of some other class. The value of the ownerId is available in this method. I am unable to use this return ownerId in a different class. It looks trivial but I don't know how to do it.
    @AuraEnabled
    public static Id getNewRecordOwner(Id ownerId) 
    { 
        System.debug('The value of Owner ID'+ownerId);
        
        leadConvertController lctr = new leadConvertController(ownerId);
        lctr.ownerIdChanged=ownerId;

        return ownerId; 
    }
How can I conditionally render a lightning component/app in my VF page based on a button click ? I have a lightning lookup component which I want to render when I click a search button on my VF page.
I am going through the premiere online course for Lightning Components right now and saw something I was curious about.  

The particular module that I am watching is showing how tag attributes can by dynamically set using expression syntax.  Nothing ground breaking there but what I found interesting was at instead of using a standard IF statement it used a ternary IF statment.  At first I thought it was just a style preference by the developer who wrote the example, but I am also curious if there is some sort of efficiency gain in the lightning framework by doing so.  

Basically he used:
class="{!(v.expenseSum != v.approvedSum ? 'alert-danger' : 'alert-success')}

Instead of:
class="{! IF(v.expenseSum != v.approvedSum , 'alert-danger' , 'alert-success')}

I totally understand they are the exact same thing, but from an efficiency standpoint is there any benefit to doing it this way? 
Hi,

Can you please give me a technical difference for using Lightning over Angular. What are the pros and cons in each of the implementaion and How is lighting different from angular. 

I am not looking for general difference like lighinging is new and angular is tested etc., but more technical understanding of how lightning makes it easier compared to the same implementation done in Angular. what feature do we have in lightning componenets that are not in Angular etc., 

Any insight on this will be really helpful. Thanks!
Hi Hive,

I am using Salesforce Communities (Napili template) and created a custom Community page for custom use

I want to have a page with some parameters as standard topic detail page having  (/topic/:topicId/:topicName) 

I want my page url to be like /customPage/:param1/:param2, but while creating page there is no option to set parameters.

Is there any way or work around for it ?

I already tried by setting  query parameters (/customPage?param1=value&param2=value) through javascript and read it in controller js but in this case these parameters are always there, they are not going away when we navigate to any other pages.
Hi  Folks,

I am new to this salesforce  as am trying to  work on trailhead project Battle Station App

Then   in the process flow  while i am creating the  Post to Chatter step then  its not showing select This Record. Its showing only user and chatter Group.  Pleae help me out   to solve this problem.

 
In a lightning component, I am trying to use the values in 1 array to set the values for a field in a second array. But for whatever reason I always end up with the last value of my array as the field value.

Here is the component:
<aura:attribute name="spaceTypeList" type="string[]" default="Conference/Focus Room,Copy Area,Kitchenette,Loading Dock,Electrical Room,Exterior Building,Janitorial Closet,Mothers Room,Office Area,Parking Garage,Restrooms/Locker Rooms,Stairs/Elevators,Carpet Spots/Vacuuming/Flooring,Lobbies/Corridors"/>
    <aura:attribute name="detailRecord" type="Inspection_Area_Detail__c" 
                        default="{'Inspection_Area__c':'',
                                 'Space_Subset__c':'',
                                 'Rating__c':'0',
                                 }"/>
    <aura:attribute name="detailList" type="Inspection_Area_Detail__c[]"/>
    
    <ui:button label="loop test" press="{!c.loop}"/>
    
    <aura:iteration items="{!v.detailList}" var="det">
        <p> Space {!det.Space_Subset__c}</p>
    </aura:iteration>



And here is the controller:

    
({
        loop: function(component, event, helper) {
            
            var spaceList = component.get("v.spaceTypeList");
            var detail = component.get("v.detailRecord");
            var List =component.get("v.detailList");
            
            for(i in spaceList){
                var space =spaceList[i];
                detail.Space_Subset__c = space;
                console.log("detail space subset "+detail.Space_Subset__c);
                List.push(detail);
                
            }
            component.set("v.detailList",List);
            
        },
    })





Thank you for your help.
HI, i've a problem to complete the "Build a Conference Management App", pat Creating Apex triggers. All steps are completed, functionaltiy works as it should be, but receives this error.

Step not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The speaker is allready booked at that time: [] 
Note: you may run into errors if you've skipped previous steps.

What I'm doing wrong ?
Hey Guys,
I am trying to implement google map using lighting. I really don't have much idea about lighting.
My requirement is to show all nearby accounts on google map. 

Apex Class

public class accController { 
    @AuraEnabled
    public static List<Account> getAccounts() {
        return [select id, Account.BillingStreet, Account.BillingPostalCode, Account.BillingCity,sandbox__Geolocation__Longitude__s,sandbox__Geolocation__Latitude__s, Account.BillingState, Account.BillingCountry from Account where sandbox__Geolocation__Latitude__s != null];
 
    }
 
}
 
Component 

<aura:component implements="force:appHostable" controller="accController">
 
    <ltng:require styles="/resource/leaflet/leaflet.css" />   
    <ltng:require scripts="/resource/leaflet/leaflet.js"
             afterScriptsLoaded="{!c.jsLoaded}" />
 
    <div class="map" id="map"></div>
 
</aura:component>
 
Controller 

({
jsLoaded: function(component, event, helper) {
    debugger;
    var accs = component.get("c.getAccounts");
                debugger;
    accs.setParams({
        "sandbox__Geolocation__Longitude__s": component.get("v.sandbox__Geolocation__Longitude__s")
    });
    debugger;
    setTimeout(function() {

/*Not Working-- this line */
        // var map = L.map('map', {zoomControl: false}).setView([sandbox__Geolocation__Longitude__s,sandbox__Geolocation__Latitude__s], 14);
       
var map = L.map('map', {zoomControl: false}).setView([25.509637,71.091645], 14);
        L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}',
            {
                attribution: 'Tiles © Esri'
            }).addTo(map);
 
        // Add marker
        L.marker([sandbox__Geolocation__Longitude__s,sandbox__Geolocation__Latitude__s]).addTo(map)
            .bindPopup('Simplion Technologies Pvt Ltd');
        });
}
})
 
The problem I am facing is to get dynamic longitude and latitude values from account object. As you can see I have commented out the line. 
If I hardcode the longitude (i.e. 25.509637) and latitude (i.e. 71.091645) values then it works fine for me. 
So I want to make this dynamic. So that it takes automatically longitude and latitude value from the account records.
If anyone can help me on this or give some ideas. It will be really helpful.
 
 
 
  • July 10, 2015
  • Like
  • 0
Hi,

I'm wondering if I made a connection through Lightning connect if I could use that to provide the products that people could pick from in an opportunity.

Is this possible?]

Thanks

I'm working on My First REST Service (soon to be released), leveraging all of the howTo's that I can find, starting with Apex REST Basic Code Sample.

I created the MyRestResource class using the example, brought up curl, and was stopped abruptly on this line:

  • Replace sessionId with the <sessionId> element that you noted in the login response.

There isn't anything preceding this line ^ that describes how to get that initial login response that contains the session Id.

 

Trying a different tack, I reviewed the article Setting Up Authentication with OAuth 2.0, installed the OAuth Playground, and set up the Connected App with the OAuth settings that would be used to access my service.

 

Can someone point me to

  1. An Apex code sample for a method that can access an Apex REST service, using the Consumer Key and Secret (or whatever their OAuth 2.0 labels are now) created in the Connected App
  2. An Apex code sample (if different from 1., above ^) that can retrieve and make use of the sessionId described in the REST Basic Code sample
  3. The Salesforce Request Token URL (for the OAuth Playground)
  4. The Salesforce Access Token URL (for the OAuth Playground)

Any and/or all of the above will be greatly appreciated.

 

Thanks!

 

Duncan

(initially posted under Apex -- moving to API discussion thread)

Hi,

 

I want to notify the case owner whenever a new case comment is being added to the case through a Site that has login enabled for customer portal users. Salesforce has a Support Setting for this particular feature which seems to work when we create case comments normally but not through apex code.

 

Can anyone tell me why this isn't working?

Hi 

 

I am playing around with the streaming api and am looking for some documentation on how to update a pushtopic and delete a pushtopic. I am having a little difficulty finding the right info. Any info would be appreciated. 

 

Thanks

Hi,

          I have a problem with Json parsing and getting a particular value.Please help me how to get. First time I am working on this Json in salesforce.

 

In my controller I wrote a code like this

 

 

string url='http://maps.googleapis.com/maps/api/distancematrix/json?origins='+EncodingUtil.urlEncode(add1,'UTF-8')+'&destinations='+EncodingUtil.urlEncode(add2,'UTF-8')+'&mode=driving&language=en&sensor=false&units=imperial';
Http h = new Http();
HttpRequest req = new HttpRequest();

req.setHeader('Content-type', 'application/x-www-form-urlencoded');
req.setHeader('Content-length', '0');
req.setEndpoint(url);
req.setMethod('POST');

HttpResponse res = h.send(req);
String responseBody = res.getBody();

JSONParser parser =System.JSON.createParser(responseBody);

 

// Here I am unable to parse and get the values using Json.
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'text')) {
System.debug('##########'+parser.getText());
parser.nextToken();

}
}

 

=============================================

In Response Body I am getting the Output of Json Like

 

{
"destination_addresses" : [ "Vijayawada, Andhra Pradesh, India" ],
"origin_addresses" : [ "Hyderabad, Andhra Pradesh, India" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "173 mi",
"value" : 278118
},
"duration" : {
"text" : "4 hours 33 mins",
"value" : 16386
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}

 

 

From the above I have to get the Distance --Text value i.e '173 mi'

 

How to get the Distance value.Can any one Guide me please.

  • February 19, 2013
  • Like
  • 0

hi,

I have following error , my org id is 00Db0000000IqV1,Please let me know ,how to activate ,and its develper edition 

 

Error: call to URL https://eu2.salesforce.com/services/data/v20.0/sobjects/Account/ failed with status 403, response [{"message":"The REST API is not enabled for this Organization.","errorCode":"API_DISABLED_FOR_ORG"}],

We use EmailToCase and are looking to enhance it a bit. Each account has a specific technical consultant assigned to it and they are supposed to handle Tier 1 support for the account, so we would like the case to be owned by them as soon as it is submitted. It seems that the only way of accomplishing this is via an Apex trigger, which I've been trying to write for the past couple of days. I've successfully written one previously and this one seems conceptually simple, but there seem to be some nuances that I'm just not getting. My rudimentary stab at it currently looks like this:

 

trigger AssignTechnicalConsultantToCase on Case (before insert) {
	for (Case c : trigger.new) {
		if (c.AccountId != null) {
			c.Owner = [SELECT Technical_Consultant__c FROM Account WHERE Id =: trigger.new[0].AccountId];
		}
	}	
}

 The current problem (aside from the whole approach possibly being completely wrong) is with the query. I've tried multiple iterations, none of which have compiled without an error of some sort, the latest of which doesn't even make sense to me:

 

Save error: Illegal assignment from LIST<Account> to SOBJECT:Name

I had been trying to reference the "Name" field on account and that error sort of made sense then, but now I don't know what it's trying to tell me. Technical_consultant__c is a lookup field to User on Account.

 

I also question whether this can be a "before insert" trigger -- does the account for EmailToCase cases get looked up before insert, or is it a post insert operation? Obviously if the account hasn't been assigned until after the case is inserted then I'll have to change the trigger type and I'll have more work to do. I'm going to keep pounding my head against this today, but if anyone can offer any suggestions on just how completely wrong I am, I'd appreciate it.

 

Thanks,

Matt

i  create one new custome setting (JobApplication).Any Possible way to create  trigger & Workflow  for custom setting .

https://developer.salesforce.com/trailhead/visualforce_mobile_salesforce1/visualforce_mobile_salesforce1_layouts_cards

I am working on above challenge and code is working in my org and is as below
 
<apex:page standardController="Opportunity"> <apex:outputText value="{!IF(Opportunity.StageName="Prospecting", 'Teststs', IF(Opportunity.StageName="Proposal/Price Quote", 'Teststs', IF(Opportunity.StageName="Proposal/Price Quote", 'Teststs', IF(Opportunity.StageName="Proposal/Price Quote", 'Teststs','Teststs'))))}"/> </apex:page>


I am getting an error as below 

User-added image

Let me know if i am doing anything wrong .
I am attempting challenge in trialhead 

https://developer.salesforce.com/trailhead/lightning_components/lightning_components_events_handle

I am gettting below error 

User-added image

I checked my code and I have tags properly written as below
 
<aura:component >
    <aura:registerEvent name="phevent" type="mohrcks:PhoneNumberEvent"/>
    <ui:inputPhone aura:id="phone" label="phone" />
    <ui:button label="Show Phone" press="{!c.send}"/>
</aura:component>
Also for output I have handler defined as well
 
<aura:component >
    <aura:attribute name="number" type="String" default="No Phone Number"/>
    <ui:outputText aura:id="phone" value="{!v.number}"/>
    <aura:handler event="mohrcks:PhoneNumberEvent" action="{!c.answer}"/>
</aura:component>

My events that is created is as follows
 
<aura:event type="APPLICATION" description="Event template">
    <aura:attribute name="phone" type="String"/>
</aura:event>

My handler ctrl class for input is as below
({
	send : function(component, event, helper) {
		var phone = component.find("phone").get("v.value");
        console.log(phone);
        $A.get("event.mohrcks:PhoneNumberEvent").setParams({
            phone: phone
       }).fire();
	}
})

For output My handler class is 
 
({
	answer : function(component, event, helper) {
		var text = event.getParam("phone");
        component.set("v.number", text);
	}
})

I am able to see this working in my org ,I still get the issue as seen below

User-added image

Is this is a bug in Trailhead ?


 
Hello All,

I tried submitting my work by clicking Check Challenge on "Trialhead" with my developer edition .It keeps spinning and dont show success or dont allocate any points .Is this a bug ?

Thanks
In addition to what query title says, when making callout via URL it seems to be working. Please find the code below:
EinsteinOCRPredictionHandler.cls
public class EinsteinOCRPredictionHandler {

    final static String VISION_API = 'https://api.einstein.ai/v2/vision';
    final static String OCREndpoint = VISION_API + '/ocr';
    final static String cmdtDeveloperName = 'Einstein_Vision_API_JWT';
    
    public class EinsteinOCRPredictionHandlerException extends Exception {}

    public static EinsteinOCRPredictionResponseWrapper predictViaURL(String fileURL, String detectionType, String accessToken, String model) {
        String response = detectText(fileURL, detectionType, accessToken, model, false);
        return parseResponse(response);
    }

    public static EinsteinOCRPredictionResponseWrapper predictViaBase64(String base64String, String detectionType, String accessToken, String model) {
        String response = detectText(base64String, detectionType, accessToken, model, true);
        return parseResponse(response);
    }
    
    public static EinsteinOCRPredictionResponseWrapper predictViaBlob(Blob fileBlob, String detectionType, String accessToken, String model) {
        String response = detectText(EncodingUtil.base64Encode(fileBlob), detectionType, accessToken, model, true);
        return parseResponse(response);
    }
    
    public static String detectText(String sample, String detectionType, String accessToken, String model, boolean isBase64) {
        string contentType = HttpFormBuilder.GetContentType();
        //  Compose the form
        string form64 = '';
        form64 += HttpFormBuilder.WriteBoundary();
        form64 += HttpFormBuilder.WriteBodyParameter('modelId', EncodingUtil.urlEncode(model, 'UTF-8'));
        form64 += HttpFormBuilder.WriteBoundary();
        form64 += HttpFormBuilder.WriteBodyParameter('task', EncodingUtil.urlEncode(detectionType, 'UTF-8'));
        form64 += HttpFormBuilder.WriteBoundary();
        if(isBase64) {
            form64 += HttpFormBuilder.WriteBodyParameter('sampleBase64Content', sample);
        } else {
            form64 += HttpFormBuilder.WriteBodyParameter('sampleLocation', sample);
        }
        form64 += HttpFormBuilder.WriteBoundary(HttpFormBuilder.EndingType.CrLf);
        blob formBlob = EncodingUtil.base64Decode(form64);
        string contentLength = string.valueOf(formBlob.size());
        //  Compose the http request
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setBodyAsBlob(formBlob);
        httpRequest.setHeader('Connection', 'keep-alive');
        httpRequest.setHeader('Content-Length', contentLength);
        httpRequest.setHeader('Content-Type', contentType); 
        httpRequest.setMethod('POST');
        //httpRequest.setTimeout(120000);
        httpRequest.setHeader('Authorization','Bearer ' + accessToken);
        httpRequest.setEndpoint(OCREndpoint);
        
        Http http = new Http();
        HTTPResponse res = http.send(httpRequest);
        System.debug(res.getStatus());
        String responseBody;
        Integer statusCode = res.getStatusCode();
        String requestStatus = res.getStatus();
        if ( statusCode == 200) {
            responseBody = res.getBody();
        } else {
            throw new EinsteinOCRPredictionHandlerException('Callout unsuccessful! Status code: '+statusCode + '. Status: '+requestStatus);
        }
        return responseBody;
    }

    static EinsteinOCRPredictionResponseWrapper parseResponse(String response) {
        if(String.isEmpty(response)) {
            throw new EinsteinOCRPredictionHandlerException('Empty response received');
        }
        EinsteinOCRPredictionResponseWrapper obj = new EinsteinOCRPredictionResponseWrapper();
        obj = (EinsteinOCRPredictionResponseWrapper)JSON.deserialize(response, EinsteinOCRPredictionResponseWrapper.class);
        return obj;
    }
}
HttpFormBuilder.cls
public class HttpFormBuilder {
    //  The boundary is alligned so it doesn't produce padding characters when base64 encoded.
    private final static string Boundary = '1ff13444ed8140c7a32fc4e6451aa76d';

    /**
     *  Returns the request's content type for multipart/form-data requests.
     */
    public static string GetContentType() {
        return 'multipart/form-data; charset="UTF-8"; boundary="' + Boundary + '"';
    }

    /**
     *  Pad the value with spaces until the base64 encoding is no longer padded.
     */
    private static string SafelyPad(
        string value,
        string valueCrLf64,
        string lineBreaks) {
        string valueCrLf = '';
        blob valueCrLfBlob = null;

        while (valueCrLf64.endsWith('=')) {
            value += ' ';
            valueCrLf = value + lineBreaks;
            valueCrLfBlob = blob.valueOf(valueCrLf);
            valueCrLf64 = EncodingUtil.base64Encode(valueCrLfBlob);
        }

        return valueCrLf64;
    }

    /**
     *  Write a boundary between parameters to the form's body.
     */
    public static string WriteBoundary() {
        string value = '--' + Boundary + '\r\n';
        blob valueBlob = blob.valueOf(value);

        return EncodingUtil.base64Encode(valueBlob);
    }

    /**
     *  Write a boundary at the end of the form's body.
     */
    public static string WriteBoundary(
        EndingType ending) {
        string value = '';

        if (ending == EndingType.Cr) {
            //  The file's base64 was padded with a single '=',
            //  so it was replaced with '\r'. Now we have to
            //  prepend the boundary with '\n' to complete
            //  the line break.
            value += '\n';
        } else if (ending == EndingType.None) {
            //  The file's base64 was not padded at all,
            //  so we have to prepend the boundary with
            //  '\r\n' to create the line break.
            value += '\r\n';
        }
        //  Else:
        //  The file's base64 was padded with a double '=',
        //  so they were replaced with '\r\n'. We don't have to
        //  do anything to the boundary because there's a complete
        //  line break before it.

        value += '--' + Boundary + '--';

        blob valueBlob = blob.valueOf(value);

        return EncodingUtil.base64Encode(valueBlob);
    }

    /**
     *  Write a key-value pair to the form's body.
     */
    public static string WriteBodyParameter(
        string key,
        string value) {
        string contentDisposition = 'Content-Disposition: form-data; name="' + key + '"';
        string contentDispositionCrLf = contentDisposition + '\r\n\r\n';
        blob contentDispositionCrLfBlob = blob.valueOf(contentDispositionCrLf);
        string contentDispositionCrLf64 = EncodingUtil.base64Encode(contentDispositionCrLfBlob);
        string content = SafelyPad(contentDisposition, contentDispositionCrLf64, '\r\n\r\n');
        string valueCrLf = value + '\r\n';
        blob valueCrLfBlob = blob.valueOf(valueCrLf);
        string valueCrLf64 = EncodingUtil.base64Encode(valueCrLfBlob);

        content += SafelyPad(value, valueCrLf64, '\r\n');

        return content;
    }

    /**
     *  Helper enum indicating how a file's base64 padding was replaced.
     */
    public enum EndingType {
        Cr,
        CrLf,
        None
    }
}


 
Hi ,
I am trying to use setSortBy() method in apex while using Report API. The method is not sorting the columns. 
Please let me know if i am missing anything in here.TIA
Reports.ReportResults results = Reports.ReportManager.runReport('00O11000000l9wC',true);
  Reports.ReportMetadata RM = results.getReportMetadata();
  System.debug('Detail columns: ' + rm.getDetailColumns());
  //Sorting code
  Reports.SortColumn sortcolumn = new Reports.SortColumn();
  sortcolumn.setSortColumn('CAT_Focal_File__c.Workday_Id__c');
  sortcolumn.setSortOrder(ASCENDING);
  System.debug('@@@1 '+results.getAlldata());	
  list<Reports.SortColumn> lstcolumns = new list<Reports.SortColumn>();
  lstcolumns.add(sortcolumn);
  rm.setSortBy(lstcolumns);
  Reports.ReportFactWithDetails factDetails =
    (Reports.ReportFactWithDetails)results.getFactMap().get('T!T');
  List<Reports.ReportDetailRow> allRows = factDetails.getRows();
  string s = (string)allRows.get(0).getDataCells().get(1).getValue();
  system.debug('@@@@@s '+s)

 
What is the recommended approach by Salesforce for mobile application authentication?
 - SAML based authentication?
 - OAuth User-Agent flow?
 - OAuth Username-Password Flow?
 - Session Id flow?

Here is my understanding:
SAML seems out question, because it involves identity provider and in most cases we don't utilize an ID provider.

In User-Agent flow we need a connected app and don't have to provide password at all and access token is sent to pre-defined callback URL. Which is not happening in all of the mobile applications available, such Salesforce1.

Salesforce1 app seems close to OAuth Username-Password Flow but we don't have to provide security token. We can login to any instance with a valid username and password and our IP is not white listed either. How can we authenticate without a security token? I believe there is a browser plugin running in the Salesforce1 app and we have to tap 'Allow' access after login and have to provide verification code as well (one time only). Tapping 'Allow' and verification code is alternative to security token?

I don't know anything anout session Id flow and didn't find any details.

I am NOT looking for code, just a general opinion about the best approach from the four option above.

Thanks in advance.

Usman
HI,
I have a simple requirement, get a list of custom object records from client side controller and display it on the page. Below is my code

I have a wrapper object created and used in the component attribute as type.

Wrapper Class:
public class DemoWrpClass {
    
    @AuraEnabled 
    public String compName;
    
    @AuraEnabled
    public String compType;
    
    public DemoWrpClass() {
    }
    
}

Component:
<aura:component >
    <aura:attribute name="items" type="DemoWrpClass[]" />
    
    <ui:button label="Press" press="{!c.onPress}" />
    
    <aura:iteration items="{!v.items}" var="item">
        <p>{!item.compName}</p>
        <p>{!item.compType}</p>
        <br />
    </aura:iteration>
    
</aura:component>

Controller:
({
    onPress : function(component) {
        var items = [];
        for(var i=0;i<5;i++){
            var item = [];
            item.compName = "Name"+i;
            item.compType = "Type"+i;
            items.push(item);
        }
        component.set("v.items", items);
    }
})

I am the getting the result when Locker Service is disabled, below is the image
Expected Output
But when the Locker service is enabled I am not getting any output, result is blank. Through Lightning CLI I got to know the error is Invalid SecureWindow API, now I dont understand how to implement SecureWindow in my code here. I want the expected output when Locker service is enabled. Please help. Thanks in advance.
Hi All,

How to deploy the Lightning components and Lightning Pages(Inside Community Builder) from Sandbox to Prod ?

Thanks & Regards
Abhilash

 
I have the following method returning a changed owner id when a person selects a new value from lightning component. I want to use this ownerId in a method of some other class. The value of the ownerId is available in this method. I am unable to use this return ownerId in a different class. It looks trivial but I don't know how to do it.
    @AuraEnabled
    public static Id getNewRecordOwner(Id ownerId) 
    { 
        System.debug('The value of Owner ID'+ownerId);
        
        leadConvertController lctr = new leadConvertController(ownerId);
        lctr.ownerIdChanged=ownerId;

        return ownerId; 
    }
How to navigate between Lightning components and pass value between them? Please explain with example.
I develop managed package, and I pack lightning page and lightning components into package. I have Mydomain established. It uploads successfully, but when I install it in target org, I get an error:
(MyLightningPage) Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "We couldn't retrieve the Mymanespace:Mycomponent component in the main region.".
I can install package with lightning page without that Mycomponent successfully. Component has only two inputs and button and it sends and receives data from Apex controller.
In development org component works absolutely alright.
How can I conditionally render a lightning component/app in my VF page based on a button click ? I have a lightning lookup component which I want to render when I click a search button on my VF page.
I am going through the premiere online course for Lightning Components right now and saw something I was curious about.  

The particular module that I am watching is showing how tag attributes can by dynamically set using expression syntax.  Nothing ground breaking there but what I found interesting was at instead of using a standard IF statement it used a ternary IF statment.  At first I thought it was just a style preference by the developer who wrote the example, but I am also curious if there is some sort of efficiency gain in the lightning framework by doing so.  

Basically he used:
class="{!(v.expenseSum != v.approvedSum ? 'alert-danger' : 'alert-success')}

Instead of:
class="{! IF(v.expenseSum != v.approvedSum , 'alert-danger' , 'alert-success')}

I totally understand they are the exact same thing, but from an efficiency standpoint is there any benefit to doing it this way? 
Hi Hive,

I am using Salesforce Communities with Napili Template and have requirement to configure Header search component (Search Publisher) to work globally ALWAYS even if I am any topic detail page suggestion box should return exact same result as it returns when we are on Home page.

We have configurable property named as "Topic ID" for Search Result page (will appear after clicking on Search button) but did not find it for Header Search Publisher component which shows suggestions before clicking on Search Button.

Is there something I can do by configuration ?
I have Created a lightning application , we don't have provision for lightning application Tab in both lightning and classic view so how can i make this app in use ....................
We are experiencing an issue where transcripts are not being saved to our salesforce org.  The chat functionality is working, but when the chat ends, the transcript is not appearing in the case related list.  This was working before 2/29, but just stopped working on all chat cases.  We do have a trigger on the chat transcript object and on the same day the chat transcripts stop being created we were getting a Apex error email 

Subject: Developer script exception from xxxxxx : LiveChatTranscriptTrigger : LiveChatTranscriptTrigger: execution of AfterInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times) Trigger.LiveChatTranscriptTrigger: line 20, column 1

 
Hi,

Can you please give me a technical difference for using Lightning over Angular. What are the pros and cons in each of the implementaion and How is lighting different from angular. 

I am not looking for general difference like lighinging is new and angular is tested etc., but more technical understanding of how lightning makes it easier compared to the same implementation done in Angular. what feature do we have in lightning componenets that are not in Angular etc., 

Any insight on this will be really helpful. Thanks!
Our Salesforce communities website has different hubs for each of our teams. Aside from the featured topics, each team has a separate Page. I would like to use a similar Feed function as if it was a topic page on these pages. I would create a Topic for the team, and have that Feed pull from the material tagged on that Topic. 

For example, "Training" is one of the pages. I would like a feed with issues and articles that will pull from a Topic that would be created with the same name. How is this possible? When using the new "Feed" lightning component, do I just need to enter in a certain ID in the RecordID section? 

Thanks! 
Hi,

  I am creating component dynamicaly usning code as below :
 
$A.createComponent(
                "c:M",yComponent",
                {},
                function(newCmp){
                    if (component.isValid()) {
                        var content = component.find("content");
                        content.set("v.body", newCmp);
                    }
                }
            );

Now i have couple of resources placed in app as below :
 
<!-- Bootstrap -->
    <ltng:require styles="/resource/resource/lib/bootstrap/css/bootstrap.min.css"/>
    
    
    <!-- Main CSS -->
    <ltng:require styles="/resource/resource/css/font.css"/>
    <ltng:require styles="/resource/resource/css/jquery.ui.css"/>
    <ltng:require styles="/resource/resource/css/jqtransform.css"/>
    <ltng:require styles="/resource/resource/css/screen.css"/>
    
    <!-- jQuery library -->
    <ltng:require scripts="/resource/resource/js/jquery-2.1.4.min.js,/resource/resource/js/jquery-ui.js,/resource/resource/js/hammer.min.js,/resource/resource/js/jquery.tablesorter.min.js,/resource/resource/js/jquery.jqtransform.js,/resource/resource/lib/bootstrap/js/bootstrap.min.js,/resource/scriptjs" />

When the component gets created in dom it's not able to use the script.It seems while the components gets created it can not use the scripts which are already loaded as part of app load.Any help will be great .
I keep getting: Challenge Not yet complete... here's what's wrong:
The validation rule does not reference the 'IsClosed' and 'CloseDate' fields correctly. Take a look at the requirements again.

When I test the formula it seems to work as the requirements indicate yet it does not pass the validation from trailhead. Here is the formula: AND( NOT(IsClosed) ,  CloseDate < TODAY() )
It is placed on the CloseDate field.
Hi,

We're trying to use Lightning Connect for a project that uses Analytics Engine and the Journey Engine but our data source does not support OData, only REST.

These links:
https://developer.salesforce.com/page/Apex_Code_Best_Practices
https://help.salesforce.com/apex/HTViewHelpDoc?id=platform_connect_add_external_data_source.htm&language=en_US
http://releasenotes.docs.salesforce.com/en-us/summer15/release-notes/rn_forcecom_external_data_apex_adapter.htm

suggest that it is possible for us to create our own adapter.

Am I reading that correctly?
Are there any scalability concerns?
Would this do the same thing that the native OData adapter does or does  the native adapter have additional functionality?
Is there a better way to do this?

Our alternate plan is to create a translation layer using Olingo but I'm worried about the performance implications of having a translation layer.