function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sam AlexSam Alex 

Post install script not performing as intended

Hi,

I have a post install script. All I want to do is add a record to a custom object (which comes with the package). In the record that i want to add automatically, I want to get details from the logged in User, and generate a random number.

After install I cannot see my the record which should be added with the post install script. The Test I did with test classes says its PASS. Here is my Post Install class. What am I doing wrong here? Please help. Thanks in advance.


global class PostInstallClass implements InstallHandler {
    global void onInstall(InstallContext context) {
        if(context.previousVersion() == null) {
            string userEmail = UserInfo.getUserEmail();
            string firstName = UserInfo.getFirstName();
            string lastName  = UserInfo.getLastName();
            string orgName   = UserInfo.getOrganizationName();
            string recName  = 'MyRec1';
            string uniqueId   = generateRandomString(40);
            
           MyCustomObject__c c = new MyCustomObject__c(UniqueID = uniqueId, Your_Company__c = orgName, Name = recName, First_Name__c = firstName, Last_Name__c = lastName, Email__c = userEmail);
            insert c;
       }
    }

public static String generateRandomString(Integer len) {
        final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        String randStr = '';
        while (randStr.length() < len) {
           Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), 62);
           randStr += chars.substring(idx, idx+1);
        }
        return randStr;
    }

}
Best Answer chosen by Sam Alex
Sam AlexSam Alex
Hi Bhawini,

I found the issue. It was here.
string userEmail = UserInfo.getUserEmail();
string firstName = UserInfo.getFirstName();
string lastName  = UserInfo.getLastName();
I am trying to get details from UserInfo object. But when installing they says that they do the installing process from a special user. Since the special user does not have the Email, FirstName and Last Name, they returns NULL where I am expecting a string.

I have to find a way to access logged in user information from the post install script.

Thanks for your help and guidance.
 

All Answers

Bhawani SharmaBhawani Sharma
In the first look, dont see anything wrong with the code. Are you installing the package in a fresh org or is this a upgrade?
Code is checking for context.previousVersion().
Sam AlexSam Alex
Thanks for your response Bhawani.

Fresh Install... what I expect from that code is, If the install is fresh I want to add m record. but if it is a update I dont want the code to do anything.

One more thing. In the install I have given a custom page as well. I created a Visual Force page and after install I see the "configure" button. when I click on that button it opens my Visual Force page. Does that make any effect on this?
Sam AlexSam Alex

Hi Bhawani, I missed one thing. In App I didnt specify what is the post install script. I did it.

Now im getting a Unexpected error. (below given is my error)


Unexpected Error         The package installation failed. Please provide the following information to the publisher:

Organization Name: xxx
Organization ID: xxx
Package: xxx
Version: 1.0
Error Message: The post install script failed.

What should I do? how should I start debugging?

Bhawani SharmaBhawani Sharma
You can try installing your package first without post install script. Once installation is completed, try to run the same script from developer console and then, debug it.
Sam AlexSam Alex
Hi Bhawini, I did it... No Luck. Without the post install script the install works, and then I add my post install content to a trigger and ran it. It worked.

But when I run it from the installer it says the same error.
Bhawani SharmaBhawani Sharma
Can you try adding a try catch block and in the catch, send the complete exception message to your email id ?
Sam AlexSam Alex
Hi Bhawini,

I found the issue. It was here.
string userEmail = UserInfo.getUserEmail();
string firstName = UserInfo.getFirstName();
string lastName  = UserInfo.getLastName();
I am trying to get details from UserInfo object. But when installing they says that they do the installing process from a special user. Since the special user does not have the Email, FirstName and Last Name, they returns NULL where I am expecting a string.

I have to find a way to access logged in user information from the post install script.

Thanks for your help and guidance.
 
This was selected as the best answer
Bhawani SharmaBhawani Sharma
It looks surprising if they do not have email address on user record. Email is required information and shouldn't be null. FirstName can be nulll, but not the email and lastname.
Sam AlexSam Alex
It is null not for the logged in user. The post installation runs by a special system user. Even though I am logged in, My data will not be used for that special system user.
 
Bhawani SharmaBhawani Sharma
Ohh... I got what you are saying, but if this is teh case, you can query this user first from database. right?
Sam AlexSam Alex
Hi Bhawani,

If I query the user from database, for what criteria i can use? because at that moment I have no idea of who the user is. Do you have any idea on that?

Thanks again.
Bhawani SharmaBhawani Sharma
I would say there will be atleast one system admin. So you can query like: List users = [Select FirstName, LastName, Email from User where Profile.Name = 'System Administrator' AND IsActive = true limit 1]; if(users.size() > 0) { //perform your logic. } ​