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 fails

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 get the following error.
Unexpected Error	 	The package installation failed. Please provide the following information to the publisher:

Organization Name: xx
Organization ID: xx
Package: xx
Version: 1.0
Error Message: The post install script failed.
What am I doing wrong here? Please help. Thanks in advance.

Below given is my Post Install code
 
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
Frédéric TrébuchetFrédéric Trébuchet
Hi,

As posinstall runs as a special system user that represents your package (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_install_handler.htm#apex_install_handler_example) I suggets you to verify that all UserInfo methods return what's expected.
Again, I think one of them return a null value for a mandatory field or one of the UserInfo methods cannot work as expected with that special system user.

Hope this helps,
Fred
 

All Answers

Frédéric TrébuchetFrédéric Trébuchet
Hi,

Are you sure MyCustomObject__c is empty before you start with this script?
Else, is the a field field for which you must have unique value and are you sure the uniqueness is respected?

Hope this helps,
Fred
Sam AlexSam Alex

Thankyou for your response Frederic.

Yes. This is a fresh install. MyCustomObject__c also creats with this install. I am helpless now since the error is also not descriptive.

This problem is with the insert statement, because when I comment that lines and try install it installs without an error, but I dont need those lines to be commented.

Any Ideas? Any place to start debugging?

Please help me.

Thanks

Frédéric TrébuchetFrédéric Trébuchet
Are you sure all the mandatory fields are presents in your insert statement?
Did they all match with the validation rules?
What if you replace the calculated values by constant values (just for testing)?
Sam AlexSam Alex
Hi Frederic,

I install the package without the post install process. And then I run the post install script content though an trigger. then it worked. But when I run the post installer content though the installer, it fails...

And also i removed the calculated values as well. NO LUCK...
Frédéric TrébuchetFrédéric Trébuchet
Hi,

As posinstall runs as a special system user that represents your package (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_install_handler.htm#apex_install_handler_example) I suggets you to verify that all UserInfo methods return what's expected.
Again, I think one of them return a null value for a mandatory field or one of the UserInfo methods cannot work as expected with that special system user.

Hope this helps,
Fred
 
This was selected as the best answer
Sam AlexSam Alex
Hi Frederic,

You are 100% CORRECT. That was the problem. UserInfo return the Organization data but not the User Name and email. Thankyou for your guidance Frederic.

That means I am not able to access logged in user details in post install process right?

Thanks again.
Frédéric TrébuchetFrédéric Trébuchet
Great!
You're right, logged user is overridden by special system user.
So, in your case you have to replace the values not available for that user by constants or values stored in a configuration object for example. Then, you select the values and use them for your MyCustomObject__c.

If this answer helped you solve your problem, please, mark the question as Solved and kindly select the best answer ;)

Thanks,
Fred