21

This is called a type cast. Apex has what is called a strict, static typing discipline, which means that all variables and values have a type, and that type is declared for variables at the time of compilation of the code. Type casting declares for the Apex compiler what type of value you expect to get back from a generic function, like JSON....


14

Please close the if condition with extra parens. if(!existingAccTeam.contains(accId.User__c)) { }


13

You are deploying to production. list.contains was added in spring 2018 and i dont think its deployed to production instances yet. Its only in preview instances(sandboxes).


12

Like others have said, you pretty much keep to Java standards. The only thing that is 'Apex Specific' I would say is line indentation. You want to always use tabs for indentation instead of spaces specifically in Apex/VF mainly because there is a character count for orgs. This may not seem like a big deal but you never know what may happen in the future ...


12

You can't update SObjects using a map. You have to do: update pipelines.values();


11

The second form is approximately twice as fast. I wrote a quick test to prove this. 10,000x records with two fields takes about 250ms using the SObject constructor, and 500ms by assigning actual fields. I presume the difference comes from house-keeping that has to occur between each full statement (e.g. checking for governor limits, garbage collection, etc),...


11

I believe you should be doing it this way: public class MyClass { final String someProperty; public MyClass() { /* some default logic */ } public MyClass(String someProperty) { this(); this.someProperty = someProperty; } } Call the constructor for the object and set the property on the object separately. ...


9

The problem is probably that "commit" is a reserved word in Apex so you can't use it as a method or variable name. (When the compiler encounters such a word it has special meaning, it's part of the language.) Use a different name in the code and in the Visualforce. Apex has a lot of these - see Reserved Keywords.


9

You can't ordinarily "see" private members, but you can add @TestVisible to allow the unit test to see the variable: @TestVisible DataToCompare compareAnIssue; Then, your unit test can do stuff with it: controller.compareAnIssue = new ControllerName.DataToCompare();


9

You can't directly do this. While Java does have a syntax for this via anonymous classes, that isn't a language feature of Apex Code. The closest you can get would be to write an interface and implement that in a class, like this: public class Myclass { public interface rollbackable { void doSomething(); } public static void rollback(...


9

You can get the Date value from a Datetime instance by calling the date() method: obj2.Date__c = obj1.CreatedDate.date(); If you're calling this method on a Datetime instance where you are not sure if it can be null, make sure to add a null check: obj2.Date__c = (obj1.CreatedDate == null) ? null : obj1.CreatedDate.date();


9

The list of primitives doesn't include Map, Set, or List. Therefore, what you have is a collection of non-primitives. They've also explicitly excluded Object, so you can't weasel your way around that limitation that way, either. You cannot include any nested collections at all, including things like List<List<String>> or Set<List<String>&...


9

This method was introduced in the Spring '18 Release, which likely hasn't been rolled out to your org yet. You can see the calendar for major releases at https://status.salesforce.com/status. For instance, if you were on na31, this release would be rolled out Friday night (Eastern Standard Time). PS - Make sure you use API Version 42.0 or greater if you ...


8

I think this speaks somewhat to Apex inheriting so much from Java. Try the same thing with a Java compiler: public class HelloDog { public static void main(String[] args) { Dog myDog = new Dog(); System.out.print(myDog); } } class Dog{ protected String name; public Dog(){ this("Fido").name="Arf"; //this.name="Arf"; ...


8

Looking in to this question, I realized that the type system is inherently broken. They have "fixed" Maps, it seems, but in a way that no longer lets you mix and match some types, especially Object. The list class is still broken, however: class c1 { } class c2 extends c1 { } c1[] s = new c2[0]; Generally speaking, unless/until they fix the problem, you ...


8

You need to add a preceding colon when using bind variables: Correct WHERE Field__c = :value Incorrect WHERE Field__c = value See also: Using Apex Variables in SOQL and SOSL Queries (emphasis mine): SOQL and SOSL statements in Apex can reference Apex code variables and expressions if they’re preceded by a colon (:). This use of a local code variable ...


7

You'll get this error if you try to call a non-static method in a static method in the same class. Consider the example below: public class TestClass { public TestClass() {} public void SomethingInstanced() {} public static void Something() { SomethingInstanced(); } } You'll get an error: Method does not exist or incorrect ...


7

When you have a Map<T1, T2> and call values, you get a List<T2>. So when you have a Map<T, List<T>>, you get a List<List<T>>. Basically, you're going to have to loop through if you want to flatten your map: List<Commission_line__c> records = new List<Commission_line__c>(); for (List<Commission_line__c> ...


7

The code you have posted does compile. To call it you will need to use the class name like this (because the method is static): String[] stringArray = test_clonestring.generateStringArray(5); This will not compile: String[] stringArray = new test_clonestring().generateStringArray(5);


7

To add to David's answer, the casts in this case are following the pattern of the expected JSON structure and how JSON.deserializeUntyped converts from JSON to Apex types. It looks like the JSON has this structure: [ {"replacement": "...", "name": "...", ...}, {...}, {...} ] A JSON array (the [ ]) is converted to an Apex list and a JSON object (...


7

You should be explicit when referencing the fields or types. Like you said yourself, Apex is case insensitive, so it's extra important to use the class name when referencing inner classes or static members and to use this when referencing instance variables or methods. Besides, I think it's good practice to avoid naming collisions like this whenever you can. ...


7

So the field juncRec.PDCN__c is a Lookup field, (Consider it as a text/ID field) where as in your loop pdcn is an object/record.(Not a text field) In your code juncRec.PDCN__c = pdcn; What you are trying to do is assign an Object to a Text/Id field and hence you get an error. You can solve this by using id juncRec.PDCN__c = pdcn.Id; for( PDCN__c pdcn : ...


7

cron expressions have 6 or 7 parts, and you've only included five. From the Apex Scheduler documentation (emphasis mine) The System.Schedule method takes three arguments: a name for the job, an expression used to represent the time and date the job is scheduled to run, and the name of the class. This expression has the following syntax: Seconds Minutes ...


6

You're using !== which besides the values also checks the same location in memory. You can use that only on references but not primitives - check this answer. You need to use != instead to compare values only.


6

You declared your return type as void. Change it to List<String>. Incorrect public static void generateStringArray Correct public static List<String> generateStringArray It would be worth your time to read up on Class Methods: To define a method, specify the following: Optional: Modifiers, such as public or protected. Required: ...


6

This is not valid syntax: SELECT ... FROM Proposal__Proposal__c in :trigger.new Instead, you likely want to filter based on Id: SELECT ... FROM Proposal__Proposal__c WHERE Id IN :trigger.new Also, your code uses one of the worst patterns you could possibly implement: to wrap your entire code in a try/catch, especially with an empty catch block. Never ...


6

Summary: Your initial assumption was (kind of) wrong. Which led to some interesting findings. Read on. Consider the following code: class DoNothing { Integer value; } Integer getInt() { return 5 / 0; } Object o; ((DoNothing)o).value = getInt(); We have a pending NullPointerException and a pending MathException. Which one happens first? In actuality, ...


6

You only have to read the first paragraph of the Apex Developer Guide documentation on Apex Class Definition to answer this question (emphasis mine): Apex Class Definition In Apex, you can define top-level classes (also called outer classes) as well as inner classes, that is, a class defined within another class. You can only have inner classes one ...


6

If you want to construct a List which contains a single element, you can use curly brackets ({}): ClassName.callout(new List<Id> { cnt.Id });


6

You need to wrap your string in single quote characters (''): someMethod('0063D00...') // ^ ^ You need these single quote characters You also need to look at the signature, it accepts an Opportunity instance, not Id. In your particular case, you just need to construct a record and pass it in: AddEmailProduct1.AddEmailAndSchedule(new ...


Only top voted, non community-wiki answers of a minimum length are eligible