• Starhunter
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 3
    Replies
I am trying to obtain a request token from twitter by using the following code:
 
public void makeCall(){
        //String callBack= EncodingUtil.urlEncode('https://testCallback.com', 'UTF-8');
        String callBack = EncodingUtil.urlEncode('https://login.salesforce.com/','UTF-8');
        String consumerKey = '***';
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        String timestamp = String.valueOf(DateTime.now().getTime()/1000);
        String nounce = String.valueOf(Crypto.getRandomLong())+String.valueOf(Crypto.getRandomLong())+String.valueOf(Crypto.getRandomLong())+String.valueOf(Crypto.getRandomLong());
        //String nounce = 'eicbejsnoiajsfiohadlf89u89mnasmd';
        system.debug(timestamp+'   '+nounce);
        String paramString = EncodingUtil.urlencode('oauth_callback','UTF-8')    +'='+EncodingUtil.urlencode(callBack,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_consumer_key','UTF-8')+'='+EncodingUtil.urlencode(consumerKey,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_nonce','UTF-8')+'='+EncodingUtil.urlencode(nounce,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_signature_method','UTF-8')+'='+EncodingUtil.urlencode('HMAC-SHA1','UTF-8')+'&'+EncodingUtil.urlencode('oauth_timestamp','UTF-8')+'='+EncodingUtil.urlencode(timestamp,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_version','UTF-8')+'='+EncodingUtil.urlencode('1.0','UTF-8');
        req.setEndpoint('https://api.twitter.com/oauth/request_token');
        String basestring = 'POST'+'&'+EncodingUtil.urlencode('https://api.twitter.com/oauth/request_token','UTF-8')+'&'+EncodingUtil.urlencode(paramString,'UTF-8');
        system.debug('**basestring'+basestring);
        String signkey = EncodingUtil.urlencode('##','UTF-8')+'&';
        Blob OAuth = Crypto.generateMac('hmacSHA1' , Blob.valueOf(baseString) , blob.valueOf(signkey));
        String final_signature = EncodingUtil.urlEncode(EncodingUtil.base64Encode(Oauth), 'UTF-8');
        string authheader = 'OAuth oauth_callback ='+callBack+',oauth_nonce='+nounce+',oauth_signature='+final_signature+',oauth_signature_method="HMAC-SHA1",oauth_timestamp='+timestamp+',oauth_consumer_key='+consumerKey+',oauth_version="1.0"' ;
        req.setMethod('POST');
        req.setHeader('Authorization', authheader);
        HttpResponse res = h.send(req);
        system.debug(res);
    }

** is my consumer key while ## is the consumer secret. But i am receiving status 401 error(authorization required)
Below is a class where three string constants are declared. 
public with sharing class testconstants{
public static string str1 ='firststring';
public string str2='secondstring';
public string str3='thirdstring';
}
I wrote a test class for this:
@isTest
public class testcontants_testclass{
static testmethod void method1()
{
testconstants t = new testconstants();
test.starttest();
system.assertEquals(testconstants.str1,'firststring');
test.stoptest();
}
}
Now the coverage for this class should only be 33% but the coverage is touching 100%.  So it remains 100% even if i keep one assert or two asserts for the three strings.
I have a few queries related to test class in general and mixed DML error in specific.
1. What does running a test class in system mode mean?
Does it mean running the test class in the context of the current user and is that same as saying :
System.runAs(new User((Id=UserInfo.getUserId()) )){ //test code}.
2. Following is the code snippet of the test class I have written:
static testmethod void method1()
{
    User u1 = DataFactory.createUser('testUser','test@user.com','permissionSetAPIname');  

    system.runAs(new User(Id=UserInfo.getUserId()))
    {
    Obj1 rec1 = DataFactory.createrec1('Record1');
    insert rec1;

    Obj2 rec2 = DataFactory.createrec2('Rec2',rec1,system.today()-10, system.today()+10 );
    insert rec2; 

    Obj3 rec3 = DataFactory.createrec3(rec2,'Tester',u1,'US');
    insert Role;

    test.starttest();   

    //testing code

    test.stoptest();
    }

}

On removing the System.runAs(), the test class fails giving the mixed DML error. 

3.  The following code works without any error:
static testmethod void method1()
{
    User u1 = new User(Alias = 'testRole',
                  Email             = 'role@test.com',
                  EmailEncodingKey  = 'UTF-8',
                  LastName          = 'user',
                  FirstName         = 'test',
                  LanguageLocaleKey = 'en_US',
                  LocaleSidKey      = 'en_US',
                  ProfileId         = [select Id from Profile where Name = 'Basic User' limit 1].Id,
                  TimeZoneSidKey    = 'America/Chicago',
                  Username          = 'stakeholder@test' + Math.round(Math.random() * 10000) + '.com',
                  UserRoleId        = null,
                  Country           = 'Great Britain');
    insert u1;    

    
    Obj1 rec1 = new Obj1(name='Record1');
    insert rec1;

    Obj2 rec2 = new Obj2(name='Rec2',field2__c=rec1.id,start_date__c=system.today()-10, end_date__c=system.today()+10 );
    insert rec2; 

    Obj3 rec3 = new Obj3(field3__c=rec2.id,field22__c='Tester',user__c=u1.id,field222__c='US');
    insert rec3;

    test.starttest();   

    //testing code

    test.stoptest();
    

}

I mean if I consider that Mixed DML error occurs because salesforce prevents updating setup object(user in this case) and non set up object in the same transaction., how is the insert operations in the above case part of different transactions.
I am new to salesforce. Please explain in simpler terms.
I want the search button to be disabled on load of page. Only after the user has entered two characters should the search button be enabled to click. I want to achieve this using jquery,something I am not used to. I googled a bit and came to the following :
<script>
$(document).ready(function(){
              $("#btnDisabled").attr("disabled", true);
}

$('#searchbox').on("keyup", disableaction);

function disableaction() 

if($("#btnDisabled").val().length>1)
      $("#btnDisabled").attr("disabled", false);
}
</script>

Where searchbox is the id of the apex inputtext field and btnDisabled is that of the command button
The button is not even getting disabled on page load.
I want the search button to be disabled on load of page. Only after the user has entered two characters should the search button be enabled to click. I want to achieve this using jquery,something I am not used to. I googled a bit and came to the following :
<script>
$(document).ready(function(){
              $("#btnDisabled").attr("disabled", true);
}

$('#searchbox').on("keyup", disableaction);

function diableaction()
{
if($("#btnDisabled").val().length>1)
      $("#btnDisabled").attr("disabled", false);
}
</script>

Where searchbox is the id of the apex inputtext field and btnDisabled is that of the command button
The button is not even getting disabled on page load.
 
I am writing a test class where I have to set certain URL parameters. I want to toggle between various values of the same parameter in one test method. Is it possible. Or else I have to create separate test method for this purpose. Below is a snippet , where I want to set the param value to 'value2' in the same function.
static testmethod void demomethod(){
System.runAs(demoUser)
        {
            test.starttest();
            ApexPages.currentPage().getParameters().put('param','value1');
            DemoControllerExtension Ext = new DemoControllerExtension ();
            test.stoptest();
        }
}
 
I am working on an app and there are the following profiles and permission sets:
1. Profile TypeAlpha
2. Profile TypeBeta (This has two different permission sets: PermA and PermB)
On clicking on a tab, I am successfully able to jump to the expected page if I am logged in as TypeAlpha user or as PermA user in TypeBeta Profile. However on trying to click the tab when logged in as PermB user , I am getting the error 
Invalid selectOptions found. Use SelectOption type in Apex.
I understand the above issue may arise if <apex:selectoption is used in place of <apex:selectoptions. But i checked the code and it seems fine. Further how does the issue happen with one permission set and work fine with other.

 
I do not have jquery knowledge , but i need to implement jquery hide funcitonality to hide '--None--' value from a picklist.
I googled and found the following code:
       $("#FIELDID option[value='PICKLIST VALUE TO BE HIDDEN']").hide();
However the same piece of code is not working in my case. I tried putting this inside the ready function
j$(document).ready(function(){
              j$("#roleName option[value='']").hide();
 });
Where roleName is the inputfield id and the value is equated to null as the '--None--' picklist value shows null value with alert.
Kindly let me know the mistake I am making. 
Further I have to ensure that the hide functionality works all the time unlike the above case where(I understand) the hide function will only be fired when page loads.
 
Is it possible to use render condition inside html class. Intention is to conditionally alter the bootstrap column class
<div class="form-group col-md-3">
Can the above be converted into something like : 
<div class = {!IF(!isCondition,"form-group col-md-3","form-group col-xs-3")}">
Of course the rendering can be done by including two different div block(one for md-3 and other for xs-3).But am curious to know if the same can be achieved via one div block.
 
I am trying to obtain a request token from twitter by using the following code:
 
public void makeCall(){
        //String callBack= EncodingUtil.urlEncode('https://testCallback.com', 'UTF-8');
        String callBack = EncodingUtil.urlEncode('https://login.salesforce.com/','UTF-8');
        String consumerKey = '***';
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        String timestamp = String.valueOf(DateTime.now().getTime()/1000);
        String nounce = String.valueOf(Crypto.getRandomLong())+String.valueOf(Crypto.getRandomLong())+String.valueOf(Crypto.getRandomLong())+String.valueOf(Crypto.getRandomLong());
        //String nounce = 'eicbejsnoiajsfiohadlf89u89mnasmd';
        system.debug(timestamp+'   '+nounce);
        String paramString = EncodingUtil.urlencode('oauth_callback','UTF-8')    +'='+EncodingUtil.urlencode(callBack,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_consumer_key','UTF-8')+'='+EncodingUtil.urlencode(consumerKey,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_nonce','UTF-8')+'='+EncodingUtil.urlencode(nounce,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_signature_method','UTF-8')+'='+EncodingUtil.urlencode('HMAC-SHA1','UTF-8')+'&'+EncodingUtil.urlencode('oauth_timestamp','UTF-8')+'='+EncodingUtil.urlencode(timestamp,'UTF-8')+'&'+EncodingUtil.urlencode('oauth_version','UTF-8')+'='+EncodingUtil.urlencode('1.0','UTF-8');
        req.setEndpoint('https://api.twitter.com/oauth/request_token');
        String basestring = 'POST'+'&'+EncodingUtil.urlencode('https://api.twitter.com/oauth/request_token','UTF-8')+'&'+EncodingUtil.urlencode(paramString,'UTF-8');
        system.debug('**basestring'+basestring);
        String signkey = EncodingUtil.urlencode('##','UTF-8')+'&';
        Blob OAuth = Crypto.generateMac('hmacSHA1' , Blob.valueOf(baseString) , blob.valueOf(signkey));
        String final_signature = EncodingUtil.urlEncode(EncodingUtil.base64Encode(Oauth), 'UTF-8');
        string authheader = 'OAuth oauth_callback ='+callBack+',oauth_nonce='+nounce+',oauth_signature='+final_signature+',oauth_signature_method="HMAC-SHA1",oauth_timestamp='+timestamp+',oauth_consumer_key='+consumerKey+',oauth_version="1.0"' ;
        req.setMethod('POST');
        req.setHeader('Authorization', authheader);
        HttpResponse res = h.send(req);
        system.debug(res);
    }

** is my consumer key while ## is the consumer secret. But i am receiving status 401 error(authorization required)
Is it possible to use render condition inside html class. Intention is to conditionally alter the bootstrap column class
<div class="form-group col-md-3">
Can the above be converted into something like : 
<div class = {!IF(!isCondition,"form-group col-md-3","form-group col-xs-3")}">
Of course the rendering can be done by including two different div block(one for md-3 and other for xs-3).But am curious to know if the same can be achieved via one div block.
 

Hello,

 

Integrating salesforce with twitter through apex code am facing error like

 

"Failed to validate oauth signature and token"

 

 

Am passing twitter Oauth token, secretkey... in string format like

 

string Stringval='Oauth_callback="'+EncodingUtil.urlEncode('https://c.ap1.visual.force.com/apex/twitterForcepage';, 'UTF-8')+'",oauth_consumer_key="eIGzjmMGeclmpSzxmjnKhQ", oauth_nonce="3de317379683f90d4f80da8879472036", oauth_signature="fiyY68Llzy2yREjUeJw0o%2Fdqcns%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1365504459", oauth_token="354671694-MrReLR18pmpDZulKaRVZR3CifKpI9ZFo1XUofyJn", oauth_version="1.0"';

 

string bVal=EncodingUtil.Base64Encode(blob.valueof(Stringval)); 
String authorizationHeader = 'Bearer :' + bval;
req.setHeader('Authorization',bVal);

 

 

How to integrate salesforce with twitter through apex? how to authenticate with twitter? or acess responce from twitter?

 

Can anyone suggest my issue where it is problem?




Thank you