You need to sign in to do that
Don't have an account?

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;
}
}
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;
}
}
I found the issue. It was here. 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
Code is checking for context.previousVersion().
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?
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?
But when I run it from the installer it says the same error.
I found the issue. It was here. 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.
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.