How can you use an Apex class from the hello.apex script - salesforce

How can you use an Apex class from the hello.apex script in a newly created standard project with VS Code?
This is probably really simple, I thought I should use some 'using' or 'include' statement but I cannot find the information. This is what I did:
I am working from VS Code.
sfdx create project.
chosen: Standard.
In the directory scripts/apex I open the file hello.apex.
Log in to org, vscodeOrg.
sfdx Execute Anonymous Apex with Editor contents, this succeeds.
sfdx Create Apex Class, called NewClass with static function
right under the constructor I add a static method, the complete class code is now:
public with sharing class NewClass {
public NewClass() {
}
public static void Test() {
System.debug('call from apex class');
}
}
The file is located in: ...\NewStandardProject\force-app\main\default\classes\NewClass.cls
Go back to hello.apex, execute still succeeds.
I add in the hello.apex file:
NewClass.Test();
Making the script code this:
string tempvar = 'Enter_your_name_here';
System.debug('Hello World!');
System.debug('My name is ' + tempvar);
NewClass.Test();
The editor gives me 'Intelligent code completion', so it seems my new class is known.
I try to execute again with 'Execute Anonymous Apex with Editor contents', it fails with this feed back:
Error: Variable does not exist: NewClass
If I replace the static call with e.g.
NewClass nc = new NewClass();
It gives the same error.
If I replace public with global in NewClass, that also does not help.
What am I doing wrong here?

Answering my own question then to make this complete:
Install sfdx
Install vs sfdx plug in
Press Ctrl-Shift P to call the sfdx from vs code
sfdx create project -> Standard
In the directory scripts/apex, open the file hello.apex
Log in to org, vscodeOrg (or if you have no standard org yet call control shift p, sfdx create a default scratch org)
sfdx Execute Anonymous Apex with Editor contents, this succeeds
sfdx Create Apex Class, called NewClass with static function
right under the constructor I add a static method, the complete class code is now:
public with sharing class NewClass {
public NewClass() {
}
public static void Test() {
System.debug('call from apex class');
}
}
In the left pain click on the file in the project directory and choose 'sfdx deploy source to org'.
(And that step as a developer customed to working with local executables, I forgot).
Add in the hello.apex file: NewClass.Test();
Making the code there:
// Use .apex files to store anonymous Apex.
// You can execute anonymous Apex in VS Code by selecting the
// apex text and running the command:
// SFDX: Execute Anonymous Apex with Currently Selected Text
// You can also execute the entire file by running the command:
// SFDX: Execute Anonymous Apex with Editor Contents
string tempvar = 'Enter_your_name_here';
System.debug('Hello World!');
System.debug('My name is ' + tempvar);
NewClass.Test();
I try to execute again with 'Execute Anonymous Apex with Editor contents'.
In the output amongst a few other lines you should see: 'call from apex class'.

Related

How can I add a custom allure report with jbhave

I am trying to use allure report with jbehave on Java using maven. So there is a package that has a java class that we can use to create the allure report.
I only imported that class and I added the class as a reportBuilder for the configuration of Jbehave, just like the code below.
...
import io.qameta.allure.jbehave.AllureJbehave
...
public class RunStories extends JUnitStories {
private AllureJbehave allureJBehave;
...
...
#Override
public Configuration configuration() {
// set path of results
System.setProperty("allure.results.directory", "build/allure-results")
// create AllureJbehave instance
allureJBehave = new AllureJbehave();
StoryReporterBuilder reporterBuilder = new StoryReporterBuilder()
.withReporters(allureJBehave)
.withCodeLocation(codeLocationFromClass(this.getClass()));
// create and return configuration instance
Configuration jBehaveConfiguration = new MostUsefulConfiguration();
jBehaveConfiguration
.useStoryReporterBuilder(reporterBuilder);
return jBehaveConfiguration;
}
}
But I am not getting the information for trends graphs, how can I add those values or add more information to the allure report ?
You might want to check the plugin file (allure.yml) you are using for Allure. in case there is none, you can add your own with the list of plugins you are interested to use and add it to your configuration.
You can also check the read access of your files. If you are using Jenkins (for example), sometimes there is some access rights that are inherited and that can cause some issues when read.

SSIS 2008 Script task compile failure: Cannot load script for execution

I'm running SSIS 2008, and I'm getting 'Cannot load script for execution.' error when running the script below. I'm able to build this script when editing script task, but it crashes when I run a package. I've tried multiple variations of this script, for example, instead of declaring c# variable dtss below, I've used DTS.Variables default object, it gave me the same issue. Please note I'm not .NET expert, but I need to create folder programmatically only if it doesn't exist, so I need to use script component. As I can see, it's a very "popular" issue, so I've tried some solutions suggested on this forum, but nothing worked for me.
using System;
using System.IO;
using System.Data;
using Microsoft.SqlServer.Dts;
using Microsoft.SqlServer.Dts.Runtime;
class Test
{
/*protected static Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel CurrDtsContext;*/
protected static Microsoft.SqlServer.Dts.Runtime.Variables dtss;
public static void Main()
{
// Specify the directory you want to manipulate.
string path = (string)(dtss["ArchivePath"].Value + "\\" + dtss["FacilityName"]);
try
{
// Determine whether the directory exists.
if (!Directory.Exists(path))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: ", e.ToString());
}
finally { }
}
}
I've eventually used File System task with property UseDirectoryIfExist=TRUE, this way it doesn't overwrite existing folder and doesn't seem to throw any error if folder already exist.

Entity Framework - how to create sql script with "GO" statements

In EF 6.1 you can run the command:
update-database -script
This generates the full SQL script for one or many migrations. The problem we found is that it does not generate the required "GO" statements for sql server. This is causing the SQL script to fail when it would otherwise succeed if run directly without the "-script" parameter.
Has anyone else run into this?
You can override the script generation behavior by creating a wrapper class around the SqlServerMigrationSqlGenerator class. This class contains an overloaded Generate() method which takes in arguments that represent the type of script block it's generating. You can override these methods to add "GO" statements before starting new script blocks.
public class ProdMigrationScriptBuilder : SqlServerMigrationSqlGenerator
{
protected override void Generate(HistoryOperation insertHistoryOperation)
{
Statement("GO");
base.Generate(insertHistoryOperation);
Statement("GO");
}
protected override void Generate(CreateProcedureOperation createProcedureOperation)
{
Statement("GO");
base.Generate(createProcedureOperation);
}
protected override void Generate(AlterProcedureOperation alterProcedureOperation)
{
Statement("GO");
base.Generate(alterProcedureOperation);
}
protected override void Generate(SqlOperation sqlOperation)
{
Statement("GO");
base.Generate(sqlOperation);
}
}
You will also need to set this class as the Sql Generator in your Configuration class constructor.
public Configuration()
{
AutomaticMigrationsEnabled = false;
// Add back this line when creating script files for production migration...
SetSqlGenerator("System.Data.SqlClient", new ProdMigrationScriptBuilder());
}
One gotcha to this approach is that while it works great for creating re-usable script files for SQL Server Enterprise Manager, the GO statements do not work when executing migrations locally. You can comment out the SetSqlGenerator line when working locally, then simply add it back when you are ready to create your deployment scripts.
If you are trying to alter a view using Sql('Alter View dbo.Foos As etc'), then you can avoid the
should be the first statement in a batch file error
without adding GO statements by putting the sql inside an EXEC command:
Sql(EXEC('Alter View dbo.Foos As etc'))
Reference:
https://stackoverflow.com/a/20352867/150342

Apex error: "Save error: Method does not exist or incorrect signature"

I'm currently learning apex (using the Force.com IDE), and I'm running into some trouble when writing a test for a custom controller.
The controller class is as follows:
public with sharing class CustomController {
private List<TestObject__c> objects;
public CustomController() {
objects = [SELECT id, name FROM TestObject__c];
}
public List<TestObject__c> getObjects() {
return objects;
}
}
and the test class is:
#isTest
private class ControllerTest {
static testMethod void customControllerTest() {
CustomController controller = new CustomController();
System.assertNotEquals(controller, null);
List<TestObject__c> objects;
objects = controller.getObjects();
System.assertNotEquals(objects, null);
}
}
On the objects = controller.getObjects(); line I'm getting an error which says:
Save error: Method does not exist or incorrect signature: [CustomController].getObjects()
Anyone have an idea as to why I'm getting this error?
A nice shorthand:
public List<TestObject__c> objects {get; private set;}
It creates the getter/setter for you and looks cleaner imo. As for your current issue, yes - it's hard saving code directly into production - especially with test classes in separate files.
Best to do this in a sandbox/dev org then deploy to production (deploy to server - Force.com IDE). But if you must save directly into production then I'd combine test methods with your class. But in the long run, having #test atop a dedicated test class is the way to go. It won't consume your valuable resources this way.

Silverlight 4 and COM Interop

I've created a ComVisible-class:
[Guid("73a3f91f-baa9-46ab-94b8-e526c22054a4"), ComVisible(true)]
public interface ITest
{
void Foo();
}
[Guid("99f72d92-b302-4fde-89bb-2dac899f5a48"), ComVisible(true)]
public class Class1 : ITest
{
public void Foo() { }
}
and registered it via
regasm ComClassTest.dll /tlb:ComClassTest.tlb
into the registry.
When I try to call it in my Silverlight 4 out-of-browser, elevated trust application like this:
var foo = AutomationFactory.CreateObject("ComClassTest.Class1");
I get an exception "{System.Exception: Failed to create an object instance for the specified ProgID."
However, I am able to call AutomationFactory.CreateObject("Word.Application") without an Exception and to call Activator.CreateInstance(Type.GetTypeFromProgID("ComClassTest.Class1")) in a normal C#-console application if I copy the ComClassTest.dll into the bin-directory.
What have I forgotton?
First thing to do is test that you can create the object from somewhere else such as VBScript. Create a .vbs file with the content:-
o = CreateObject("ComClassTest.Class1")
If that doesn't generate an error then there is something specifically that SL OOB is upset with otherwise your problem isn't really related to Silverlight at all.
Consider making the following changes to your COM code.
Its often easier to specify ComVisible(true) at the assembly level. You can do that from the application tab of the project properties on the Assembly Information dialog. You can also get Visual Studio to register the assembly with COM and build time using the option found on the build tab of the project properties.
Its a good idea to be specific about the ComInterfaceType you want to expose.
Things get really messy if you expose the class interface directly generally you only want the interface you have defined to be used and that this be the default interface for the class. In addition it probably better to stick to COM naming conventions for the default interface of a class.
Finally (and possibly crucially in your case) its a good idea to be explicit about the ProgId to use for the class.
Applying the above and we get:-
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("73a3f91f-baa9-46ab-94b8-e526c22054a4")]
public interface _Class1
{
void Foo();
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("99f72d92-b302-4fde-89bb-2dac899f5a48")]
[ProgId("ComClassTest.Class1")]
public class Class1 : _Class1
{
public void Foo() { }
}

Resources