• Tiffany Shabazz 9
  • NEWBIE
  • 5 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies
I am working to integrate FormAssembly forms in Salesforce and need assistance with writing the test class for the code. I would think that this would be something I could easily find since this seems like a common use case, but I am not able to find anything. Has anyone written a test class associated with FormAssembly integration in Salesforce? Essentially, the code is working in the sandbox. I am just trying to display the form using a visualforce page. Here is the code. I used the instructions written by FormAssembly, but those did not include how to create the test class.

Controller:

public class SuggestionBoxController{

    public HTTPResponse res {get; set;}
    public String resBody {get; set;}
    public String endpoint {get; set;}

    public SuggestionBoxController() {
        PageReference pageRef = ApexPages.currentPage();

        HttpRequest req = new HttpRequest();
        req.setMethod('GET');

        if(pageRef.getParameters().get('tfa_next') == null){
          //Replace 'FORM_ID' with your form's ID number
          endpoint = 'https://app.formassembly.com/rest/forms/view/4604281';
        }else{
          endpoint = 'https://app.formassembly.com/rest' + pageRef.getParameters().get('tfa_next');
        }
        req.setEndpoint(endpoint);

        Http http = new Http();
        try {
                //Execute web service call here
                res = http.send(req);
                resBody = res.getBody();
        } catch(System.CalloutException e) {
            //Exception handling goes here....
            System.debug(e);
        }
    }
}

Visualforce Page:

<apex:page controller="SuggestionBoxController">
<apex:outputText value="{!resBody}" escape="false" />
</apex:page>
Hi - I am using the following code to allow users to upload documents on a new custom object. I am having issues developing the test to cover it as it is apparently only covering 6%. Any help is greatly appreciated. Here is what I have so far:

Code:

public class UploadAttachmentControllerReviews {
    
    public String selectedType {get;set;}
    public Boolean selectedAwesomeness {get;set;}
    public String description {get;set;}
    private Opportunity opportunity {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    
    public UploadAttachmentControllerReviews(ApexPages.StandardController controller) { 
        this.opportunity = (Opportunity)controller.getRecord();
    }   
    
    // creates a new Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Business_Reviews__c obj = new Business_Reviews__c();
        obj.opportunity__c = opportunity.Id; 
        obj.description__c = description;
        // fill out cust obj fields
        return Database.insert(obj);
    }
    
    // create an actual Attachment record with the Attachment__c as parent
    private Database.SaveResult saveStandardAttachment(Id parentId) {
        Database.SaveResult result;
        
        Attachment attachment = new Attachment();
        attachment.body = this.fileBody;
        attachment.name = this.fileName;
        attachment.parentId = parentId;
        // inser the attahcment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf(' ');
        return result;
    }
    
    /**
    * Upload process is:
    *  1. Insert new Attachment__c record
    *  2. Insert new Attachment with the new Business_Reviews__c record as parent
    *  3. Update the Attachment__c record with the ID of the new Attachment
    **/
    public PageReference processUpload() {
        try {
            Database.SaveResult customAttachmentResult = saveCustomAttachment();
        
            if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));
                return null;
            }
        
            Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
        
            if (attachmentResult == null || !attachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));            
                return null;
            } else {
                // update the custom attachment record with some attachment info
                Business_Reviews__c customAttachment = [select id from Business_Reviews__c where id = :customAttachmentResult.getId()];
                customAttachment.name = this.fileName;
                customAttachment.Attachment__c = attachmentResult.getId();
                update customAttachment;
            }
        
        } catch (Exception e) {
            ApexPages.AddMessages(e);
            return null;
        }
        
        return new PageReference('/'+opportunity.Id);
    }
    
    public PageReference back() {
        return new PageReference('/'+opportunity.Id);
    }     

}

Test:

@isTest 
public class ExtensionTestActivityClass 
{
 static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account';
 insert testAccount;

 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
  UploadAttachmentControllerActivity testOppPlan = new UploadAttachmentControllerActivity(sc);

  PageReference pageRef = Page.Cross_Selling_Activity_Page; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);
  
  //testOppPlan.save(); //call all your function here
  // creates a new Attachment__c record
        Cross_Selling_Activity__c obj = new Cross_Selling_Activity__c();
        obj.account__c = obj.Id; 
        obj.description__c = 'test';
        }
    
    // create an actual Attachment record with the Attachment__c as parent
    private Database.SaveResult saveStandardAttachment; {
        Database.SaveResult result;
        
        Attachment attachment = new Attachment();
        attachment.name = 'this.fileName';
        attachment.parentId = attachment.id;
        // inser the attahcment
        result = Database.insert(attachment);    
 Test.StopTest();
  }
  }
I am working to integrate FormAssembly forms in Salesforce and need assistance with writing the test class for the code. I would think that this would be something I could easily find since this seems like a common use case, but I am not able to find anything. Has anyone written a test class associated with FormAssembly integration in Salesforce? Essentially, the code is working in the sandbox. I am just trying to display the form using a visualforce page. Here is the code. I used the instructions written by FormAssembly, but those did not include how to create the test class.

Controller:

public class SuggestionBoxController{

    public HTTPResponse res {get; set;}
    public String resBody {get; set;}
    public String endpoint {get; set;}

    public SuggestionBoxController() {
        PageReference pageRef = ApexPages.currentPage();

        HttpRequest req = new HttpRequest();
        req.setMethod('GET');

        if(pageRef.getParameters().get('tfa_next') == null){
          //Replace 'FORM_ID' with your form's ID number
          endpoint = 'https://app.formassembly.com/rest/forms/view/4604281';
        }else{
          endpoint = 'https://app.formassembly.com/rest' + pageRef.getParameters().get('tfa_next');
        }
        req.setEndpoint(endpoint);

        Http http = new Http();
        try {
                //Execute web service call here
                res = http.send(req);
                resBody = res.getBody();
        } catch(System.CalloutException e) {
            //Exception handling goes here....
            System.debug(e);
        }
    }
}

Visualforce Page:

<apex:page controller="SuggestionBoxController">
<apex:outputText value="{!resBody}" escape="false" />
</apex:page>
Hi - I am using the following code to allow users to upload documents on a new custom object. I am having issues developing the test to cover it as it is apparently only covering 6%. Any help is greatly appreciated. Here is what I have so far:

Code:

public class UploadAttachmentControllerReviews {
    
    public String selectedType {get;set;}
    public Boolean selectedAwesomeness {get;set;}
    public String description {get;set;}
    private Opportunity opportunity {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    
    public UploadAttachmentControllerReviews(ApexPages.StandardController controller) { 
        this.opportunity = (Opportunity)controller.getRecord();
    }   
    
    // creates a new Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Business_Reviews__c obj = new Business_Reviews__c();
        obj.opportunity__c = opportunity.Id; 
        obj.description__c = description;
        // fill out cust obj fields
        return Database.insert(obj);
    }
    
    // create an actual Attachment record with the Attachment__c as parent
    private Database.SaveResult saveStandardAttachment(Id parentId) {
        Database.SaveResult result;
        
        Attachment attachment = new Attachment();
        attachment.body = this.fileBody;
        attachment.name = this.fileName;
        attachment.parentId = parentId;
        // inser the attahcment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf(' ');
        return result;
    }
    
    /**
    * Upload process is:
    *  1. Insert new Attachment__c record
    *  2. Insert new Attachment with the new Business_Reviews__c record as parent
    *  3. Update the Attachment__c record with the ID of the new Attachment
    **/
    public PageReference processUpload() {
        try {
            Database.SaveResult customAttachmentResult = saveCustomAttachment();
        
            if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));
                return null;
            }
        
            Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
        
            if (attachmentResult == null || !attachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));            
                return null;
            } else {
                // update the custom attachment record with some attachment info
                Business_Reviews__c customAttachment = [select id from Business_Reviews__c where id = :customAttachmentResult.getId()];
                customAttachment.name = this.fileName;
                customAttachment.Attachment__c = attachmentResult.getId();
                update customAttachment;
            }
        
        } catch (Exception e) {
            ApexPages.AddMessages(e);
            return null;
        }
        
        return new PageReference('/'+opportunity.Id);
    }
    
    public PageReference back() {
        return new PageReference('/'+opportunity.Id);
    }     

}

Test:

@isTest 
public class ExtensionTestActivityClass 
{
 static testMethod void testMethod1() 
 {
 Account testAccount = new Account();
 testAccount.Name='Test Account';
 insert testAccount;

 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
  UploadAttachmentControllerActivity testOppPlan = new UploadAttachmentControllerActivity(sc);

  PageReference pageRef = Page.Cross_Selling_Activity_Page; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
  Test.setCurrentPage(pageRef);
  
  //testOppPlan.save(); //call all your function here
  // creates a new Attachment__c record
        Cross_Selling_Activity__c obj = new Cross_Selling_Activity__c();
        obj.account__c = obj.Id; 
        obj.description__c = 'test';
        }
    
    // create an actual Attachment record with the Attachment__c as parent
    private Database.SaveResult saveStandardAttachment; {
        Database.SaveResult result;
        
        Attachment attachment = new Attachment();
        attachment.name = 'this.fileName';
        attachment.parentId = attachment.id;
        // inser the attahcment
        result = Database.insert(attachment);    
 Test.StopTest();
  }
  }