• AKum
  • NEWBIE
  • 20 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 3
    Questions
  • 10
    Replies
i am getting confussion, what is the real use of writing test classes and how it will work for apex classes and triggers?

Please clear my confussion,

Thanks in advance

Hi Guys,

  • We are having some weird issue while comparing two strings in apex class :
  • We have a list of strings in which we store csv file headers after parsing that in salesforce.
  • Then we compare each of these headers value in salesforce . In some situation we are getting same debug's for both string values but
  • The check of STRING.EQUALSIGNORECASE is failing.
  • I have assumed that the value in either of the string may contain some garbage or value of other type but not sure .
Thanks !
  • June 28, 2017
  • Like
  • 0

Hi Guys , 

By following some other posts I tried to submit a sample feed but its returning error "Invalid query string provided - Keys may not contain &lt" . I think its not related to signature but not sure what I am missing .

public with sharing class SendFeedToAmazon {
public static void ListProduct() {
        //Current time in GMT ISO 8601
        String timestamp = datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'');
        timestamp = EncodingUtil.urlEncode(timestamp,'UTF-8');
        
        //Amazon Variables
        String action = 'SubmitFeed';
        String FeedType ='_POST_PRODUCT_DATA_';
        String version = '2009-01-01';
        String signatureVersion = '2';
        String signatureMethod = 'HmacSHA256';
        String marketplaceId= 'Example';
        String sellerId = 'Example';
        String endpoint = 'https://mws.amazonservices.com/Feeds/2009-01-01';
        String accessKey = 'Example';
        String amazonSecretKey = 'Example';
    
        //Construct a query string with the query information
        String queryString = 'AWSAccessKeyId=' + accessKey + 
            '&Action=' + action  +
            '&FeedType=' + FeedType + 
            '&MarketplaceIdList.Id.1=' + MarketplaceId  +
            '&SellerId=' + SellerId +
            '&SignatureMethod=' + SignatureMethod  +
            '&SignatureVersion=' + SignatureVersion  +
            '&Timestamp=' + timestamp  +
            '&Version=' + version ;

        String stringtoSign = 'POST' + '\n' +
            'mws.amazonservices.com' + '\n' +
            '/Feeds/2009-01-01' + '\n' +
            queryString;
        
        //Covert query string to signature using Amazon secret key as key
        Blob mac = Crypto.generateMac('HMacSHA256', blob.valueof(stringtoSign),blob.valueof(amazonSecretKey));
        String signature = EncodingUtil.base64Encode(mac);
        signature = EncodingUtil.urlEncode(signature,'UTF-8');

        System.debug(signature);
     
        HttpRequest req = new HttpRequest(); 
        
        //Below is the MD5Value
        
        String xmlBody = '<?xml version="1.0" encoding="iso-8859-1"?>' +
'<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+
    'xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">'+
  '<Header>'+
    '<DocumentVersion>1.01</DocumentVersion>'+
    '<MerchantIdentifier>M_EXAMPLE_123456</MerchantIdentifier>'+
  '</Header>'+
  '<MessageType>Product</MessageType>'+
  '<PurgeAndReplace>false</PurgeAndReplace>'+
  '<Message>'+
    '<MessageID>1</MessageID>'+
    '<OperationType>Update</OperationType>'+
    '<Product>'+
      '<SKU>56789</SKU>'+
      '<StandardProductID>'+
        '<Type>ASIN</Type>'+
        '<Value>B0EXAMPLEG</Value>'+
      '</StandardProductID>'+
      '<ProductTaxCode>A_GEN_NOTAX</ProductTaxCode>'+
      '<DescriptionData>'+
        '<Title>Example Product Title</Title>'+
        '<Brand>Example Product Brand</Brand>'+
        '<Description>This is an example product description.</Description>'+
        '<BulletPoint>Example Bullet Point 1</BulletPoint>'+
        '<BulletPoint>Example Bullet Point 2</BulletPoint>'+
        '<MSRP currency="USD">25.19</MSRP>'+
        '<Manufacturer>Example Product Manufacturer</Manufacturer>'+
        '<ItemType>example-item-type</ItemType>'+
      '</DescriptionData>'+
      '<ProductData>'+
        '<Health>'+
          '<ProductType>'+
            '<HealthMisc>'+
              '<Ingredients>Example Ingredients</Ingredients>'+
              '<Directions>Example Directions</Directions>'+
            '</HealthMisc>'+
          '</ProductType>'+
        '</Health>'+
      '</ProductData>'+
    '</Product>'+
  '</Message>'+
'</AmazonEnvelope>';
        
        
        
        Blob targetBlob = Blob.valueOf(xmlBody);
        Blob hash = Crypto.generateDigest('MD5', targetBlob);    
        String contentMD5 = EncodingUtil.base64Encode(hash);
        contentMD5 =  EncodingUtil.URLENCODE(contentMD5,'UTF-8');   
        
        req.setEndpoint(endpoint +'?AWSAccessKeyId=' + accessKey +
            '&Action=' + action +
            '&FeedType=' + FeedType  +
            '&ContentMD5Value=' + ContentMD5+
            '&SellerId=' + sellerId +
            '&SignatureVersion=2' +
            '&Timestamp=' + timestamp +
            '&Version=' + version +
            '&Signature=' + signature +
            '&SignatureMethod='+ signatureMethod +
            '&MarketplaceIdList.Id.1=' + marketplaceId);
        
        req.setMethod('POST');
        req.setBody(xmlBody);
        
        
        Http http = new Http();
        try {
            HttpResponse res = http.send(req);
            System.debug(':::::' + res.getStatus());
            System.debug(':::::' + res.getStatusCode());
            System.debug(':::::' + res.getBody());
            } 
        catch(System.CalloutException e) {
            System.debug(e);
        }
	}
}
  • June 26, 2017
  • Like
  • 0
Hi Guys ,
Can we consume Amazon Marketplace Web Service from salesforce and create listings and fetch orders .
  • June 17, 2017
  • Like
  • 0
Hi Guys ,
Can we consume Amazon Marketplace Web Service from salesforce and create listings and fetch orders .
  • June 17, 2017
  • Like
  • 0
how to  remove element from map
User-added image<apex:page controller="locationController"> 
Location Table <apex:messages style="color:red"></apex:messages>
 <apex:form>
 <apex:pageblock>
 <apex:pageblocktable value="{!locationList}" var="locItem"> 
 <apex:column headervalue="Country">
 <apex:inputfield value="{!locItem.Country__c}"> 
 </apex:inputfield> 
 </apex:column> 
 <apex:column headervalue="State/Province"> <apex:inputfield value="{!locItem.State__c}"> 
 </apex:inputfield>
 </apex:column> <apex:column headervalue="City">
 <apex:inputfield value="{!locItem.City__c}"> </apex:inputfield>
 </apex:column>
 </apex:pageblocktable> 
 </apex:pageblock> 
 </apex:form> 
 </apex:page> - 
 
 
 public with sharing class locationController {
 public list locationList{get;set;}
 public locationController() {
 locationList = [Select ID, Country__c, State__c, City__c From student__c];
 } 
 }
I have an apex batch class that I was able to get workig in my sandbox enviorment and that is ready for deployment into my production enviroment. 

However when I went to write the test class for this I ran into some trouble, with an error message that says line 50 = Test Class Error: Argument must be an object that implements Database.Batchable.

Can you guys help me get over this hurdle or tell me if i need a new test class code in order to be able to deploy to production? Below are my batch class and my test class thanks. 

Batch Class
global class UpdateContactsVoter implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'Select ID,FirstName,LastName,MailingPostalCode from contact where contact.voter_file_id__c = null';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext info, List<contact> scope){
        Set<String> set_Str = new Set<string>();
                                Map<String,Voter_File_TX__c> mp_VoterFile;

        for(Voter_File_TX__c VoterFile : [Select ID,First_Name__c, Last_Name__c,Zipcode__c, Contact__c From Voter_File_TX__c] ){
                                                if(mp_VoterFile==null){
                                                                mp_VoterFile = new Map<String,Voter_File_TX__c>();
                                                }
                                                mp_VoterFile.put(VoterFile.First_Name__c +''+VoterFile.Last_Name__c +''+VoterFile.Zipcode__c,VoterFile);
                                }
                                for(Contact ContactList : scope){
                                                if(mp_VoterFile!=null && mp_VoterFile.containsKey(ContactList.FirstName+''+ ContactList.LastName+''+ ContactList.MailingPostalCode))
            
            {
                                                                mp_VoterFile.get(ContactList.FirstName +''+ ContactList.LastName +''+ ContactList.MailingPostalCode).Contact__c = ContactList.id;
                                                }
                                }
                                if(mp_VoterFile!=null && mp_VoterFile.values()!=null){
                                                update mp_VoterFile.values();
                                }
    }
    
    global void finish(Database.BatchableContext info){
        
    }
}


Test Class
 line 50 = Test Class Error: Argument must be an object that implements Database.Batchable.

@isTest 
public class TestClassUpdateContacts 
{
    static testMethod void testMethod1() 
    {
        List<Contact> lstContact= new List<Contact>();
        List<Voter_File_TX__c> lstVoterfile = new List<Voter_File_TX__c>();
        if(contact.voter_file_id__c == null && voter_file_tx__c.contact__c == null)
        {
        Contact cont = new Contact();
        cont.FirstName ='Test';
        cont.LastName = 'Test';
        cont.id = 'Test';
        cont.MailingPostalCode = 'test';
            
        insert cont;
            
        Voter_File_TX__c vf = new Voter_File_TX__c();
        vf.First_Name__c ='Test';
        vf.Last_Name__c ='Test';
        vf.RNC_ID__c = 'Test';
        vf.Zipcode__c = 'Test'; 
                                vf.First_Name__c = cont.FirstName ;
        vf.Last_Name__c = cont.LastName ;
        vf.Zipcode__c = cont.MailingPostalCode ;
        vf.Contact__c = cont.Id ;
        
        insert vf;
       
        cont.FirstName = vf.First_Name__c ;
        cont.LastName = vf.Last_Name__c ;
        cont.MailingPostalCode = vf.Zipcode__c;
        cont.RNC_ID__c = vf.RNC_ID__c ;
            
                                update cont;
        }
        else {
         contact cont1 = new Contact(); 
         Voter_File_TX__c vf1 = new Voter_File_TX__c();
         cont1.Voter_File_ID__c = null;
         vf1.Contact__c = null; 
          
        }
        insert lstContact;
        update lstContact;
        
        Test.startTest();

            TestClassUpdateContacts obj = new TestClassUpdateContacts();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}
 


 

i am getting confussion, what is the real use of writing test classes and how it will work for apex classes and triggers?

Please clear my confussion,

Thanks in advance
I am currently working on survey to display a random questions, For that i need to update random numbers to a sobject. But System.NullPointerException: occurred in my code.

Apex:
public class update_random_question 
{
public   List<Question__c> scope{get;set;}
public update_random_question()
{
    for(Question__c a : scope)
    {
        a.orderby__c = randomizer.getRandomNumber(100);            
    }
update scope;
} }

VF:​
<apex:page controller="update_random_question">
    <apex:form>
        <apex:commandButton Value="Begin" action="{!update_random_question}"/>
    </apex:form>
</apex:page>

Notes: randomizer.getRandomNumber
I'm trying to implement an OnClick Javacript Button to only allow users with a certain profile to use the button. However, I keep getting the following error. Unterminated String constant.

Any help would be appreciated.
if("{!User.ProfileId}" = "00e30000000tnOY")
{ 
alert("Nice try, think again."); 
} 
else{ 
window.location = "/006/e?retURL={!Account.Id} 
&accid={!Account.Id} 
&RecordType=012a0000001ZSiy 
&ent=Opportunity&opp3="Opportunity" 
&opp11="New" 
&opp9={!TODAY()+30}"; 
}



 
  • November 11, 2016
  • Like
  • 0
Hi
I am trying to bring Amazon orders into Salesforce using the Amazon webservice but I keep getting the same error message:
The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

But the signature I send is correct. I know because I have used Amazon MWS Scratchpad (this works perfectly) and when I use the timestamp from this in my program I get the same signature.  

I have no idea what else to do and I would appreciate any suggestions though I am 100% certain that I am using the correct information (access key, secret key etc.) from Amazon
My code is below
Thanks
Gráinne
 public static void Listorder() {

        //Current time in GMT ISO 8601
        String timestamp = datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'');
        timestamp = EncodingUtil.urlEncode(timestamp,'UTF-8');
        
        //Amazon Variables
        String action = 'ListOrders';
        String version = '2013-09-01';
        String signatureVersion = '2';
        String signatureMethod = 'HmacSHA256';
        String marketplaceId= 'amazon marketplace id';
        String fulfillmentChannel = 'AFN';
        String sellerId = 'amazon seller id';
        String endpoint = 'https://mws.amazonservices.com/Orders/2013-09-01';
        String accessKey = 'amazon access key';
        String amazonSecretKey = 'amazon secret key';
    
        //Construct a query string with the query information
        String queryString = 'AWSAccessKeyId=' + accessKey + 
            '&Action=' + action  +
            '&CreatedAfter=2016-09-30T23%3A00%3A00Z' + 
            '&MarketplaceId.Id.1=' + MarketplaceId  +
            '&SellerId=' + SellerId +
            '&SignatureMethod=' + SignatureMethod  +
            '&SignatureVersion=' + SignatureVersion  +
            '&Timestamp=' + timestamp  +
            '&Version=' + version ;

        String stringtoSign = 'POST' + '\n' +
            'mws.amazonservices.co.uk' + '\n' +
            '/Orders/2013-09-01' + '\n' +
            queryString;
        
        //Covert query string to signature using Amazon secret key as key
        Blob mac = Crypto.generateMac('HMacSHA256', blob.valueof(stringtoSign),blob.valueof(amazonSecretKey));
        String signature = EncodingUtil.base64Encode(mac);
        signature = EncodingUtil.urlEncode(signature,'UTF-8');

        System.debug(signature);
     
        HttpRequest req = new HttpRequest();    
        
        req.setEndpoint(endpoint +'?AWSAccessKeyId=' + accessKey +
            '&Action=' + action +
            '&SellerId=' + sellerId +
            '&SignatureVersion=2' +
            '&Timestamp=' + timestamp +
            '&Version=' + version +
            '&Signature=' + signature +
            '&SignatureMethod='+ signatureMethod +
            '&CreatedAfter=2016-09-30T23%3A00%3A00Z' +
            '&MarketplaceId.Id.1=' + marketplaceId);
        
        req.setMethod('POST');
        
        Http http = new Http();
        try {
            HttpResponse res = http.send(req);
            System.debug(res.getStatus());
            System.debug(res.getStatusCode());
            System.debug(res.getBody());
            } 
        catch(System.CalloutException e) {
            System.debug(e);
        }

   }


 
I am currently working on survey to display a random questions, For that i need to update random numbers to a sobject. But System.NullPointerException: occurred in my code.

Apex:
public class update_random_question 
{
public   List<Question__c> scope{get;set;}
public update_random_question()
{
    for(Question__c a : scope)
    {
        a.orderby__c = randomizer.getRandomNumber(100);            
    }
update scope;
} }

VF:​
<apex:page controller="update_random_question">
    <apex:form>
        <apex:commandButton Value="Begin" action="{!update_random_question}"/>
    </apex:form>
</apex:page>

Notes: randomizer.getRandomNumber