+ Start a Discussion
prasanth puvvada 4prasanth puvvada 4 

please help for simple future method.

i am trying to save this program and this is saying error. please some one help me to solve this error.  

Error is:-  Error: Compile Error: line breaks not allowed in string literals at line 9 column -1

 
public class myclass {
    @future
    public static void insertUserWithRole() 
    {

        Profile p = [SELECT Id FROM Profile WHERE Name='Contract Manager'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
        // Create new user with a non-null user role ID 
        User u = new User(alias = 'ramu', email='prasanthssn@gmail.com', emailencodingkey='UTF-8', lastname='rampandu,  languagelocalekey='en_US',  localesidkey='en_US', profileid = p.Id, userroleid = r.Id, timezonesidkey='America/Los_Angeles', username='saiprasanth@gmail.com');
        insert u;
    }
}

 
surasura
your last name is not closed with  a  closing quote , try below code
 
public class myclass {
    @future
    public static void insertUserWithRole() 
    {

        Profile p = [SELECT Id FROM Profile WHERE Name='Contract Manager'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
        // Create new user with a non-null user role ID 
        User u = new User(alias = 'ramu', email='prasanthssn@gmail.com', emailencodingkey='UTF-8', lastname='rampandu',  languagelocalekey='en_US',  localesidkey='en_US', profileid = p.Id, userroleid = r.Id, timezonesidkey='America/Los_Angeles', username='saiprasanth@gmail.com');
        insert u;
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
 
public class myclass 
{
    @future
    public static void insertUserWithRole() 
    {

        Profile p = [SELECT Id FROM Profile WHERE Name='Contract Manager'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
 
        User uObj = new User();
		uObj.alias = 'ramu';
		uObj.email='prasanthssn@gmail.com'; 
		uObj.emailencodingkey='UTF-8'; 
		uObj.lastname='rampandu';
		uObj.languagelocalekey='en_US';  
		uObj.localesidkey='en_US'; 
		uObj.profileid = p.Id;
		uObj.userroleid = r.Id;
		uObj.timezonesidkey='America/Los_Angeles'; 
		uObj.username='saiprasanth@gmail.com';
        insert uObj;
    }
}

Please check more detail of future method on below blog:-

http://amitsalesforce.blogspot.in/2015/02/future-methods-in-salesforce.html


NOTE :-
1) Methods with the future annotation must be static methods
2) can only return a void type
3) The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types
4) Methods with the future annotation cannot take sObjects or objects as arguments.
5) You can invoke future methods the same way you invoke any other method. However, a future method can’t invoke another future method
6) No more than 50 method calls per Apex invocation
7) Asynchronous calls, such as @future or executeBatch, called in a startTest, stopTest block, do not count against your limits for the number of queued jobs
8) The maximum number of future method invocations per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater
9) To test methods defined with the future annotation, call the class containing the method in a startTest(), stopTest() code block. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously

IMP:-
The reason why sObjects can’t be passed as arguments to future methods is because the sObject might change between the time you call the method and the time it executes. In this case, the future method will get the old sObject values and might overwrite them.  To work with sObjects that already exist in the database, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record. The following example shows how to do so with a list of IDs

Please let us know if this will help you

 
prasanth puvvada 4prasanth puvvada 4
i just trying to add string us{set;get;}    only  i am getting error. please tell me why i am getting this error.

Error: Compile Error: Variable does not exist: us at line 13 column 9


 
public class futureexample1 {

public string us{set;get;}
    @future
    public static void insertUserWithRole() 
    {

        Profile p = [SELECT Id FROM Profile WHERE Name='Contract Manager'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
        // Create new user with a non-null user role ID 
        User u = new User(alias = 'ramu', email='prasanthssn@gmail.com', emailencodingkey='UTF-8', lastname='rampandu',  languagelocalekey='en_US',  localesidkey='en_US', profileid = p.Id, userroleid = r.Id, timezonesidkey='America/Los_Angeles', username='saiprasanth@gmail.com');
        insert u;
        us='hello';
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
NOTE :- In static method you can use only Static variable.

Please try below code :-
public Static string us{set;get;}

or you can declair this valiable inside method only.Please try below code:-
public class futureexample1 {

    @future
    public static void insertUserWithRole() 
    {
		String us ;

        Profile p = [SELECT Id FROM Profile WHERE Name='Contract Manager'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
        // Create new user with a non-null user role ID 
        User u = new User(alias = 'ramu', email='prasanthssn@gmail.com', emailencodingkey='UTF-8', lastname='rampandu',  languagelocalekey='en_US',  localesidkey='en_US', profileid = p.Id, userroleid = r.Id, timezonesidkey='America/Los_Angeles', username='saiprasanth@gmail.com');
        insert u;
        us='hello';
    }
}

Please mark this as solution if this will help you

Thanks,
Amit Chaudhary
prasanth puvvada 4prasanth puvvada 4
Amith.     could u solve this error please..... (same program i implemented mixed dml operation)  Thanks in advance.


https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BMn3IAG