+ Start a Discussion
sandeep@devsandeep@dev 

Test class for Trigger -- urgent

Hi,

 

I need a test class for below Trigger. Need to move it to production urgently. Any help is much appreciated.

 

trigger AutogenarateUsernamepassword on Account (before insert, before update)
{
Private Account[] acc = Trigger.new;
Private Contact c = new Contact();
Public Account accnt;
Public string fromAddress{get;set;}
Private List<Contact_Commitment__c> concomm = new List<Contact_Commitment__c>();
Private List<Contact_Commitment__c> concomm1 = new List<Contact_Commitment__c>();
List<String> characters = new List<String>{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'};
Public String txtdata='';


public Integer randomNumber(){
Integer random = Math.Round(Math.Random()* characters.Size());
if(random == characters.size()){
random--;
}
return random;
}

public String getChar1()
{
txtdata='';
for(integer i=0;i<6;i++)
{
txtdata+=characters[randomNumber()];

}
return txtdata.trim();
}

if(acc[0].Notify_Client__c=='Send Username/Password to Client')
{

for(Contact con : [Select id,Name,Email from Contact Where Id in (Select Contact_Name__c from Contact_Commitment__c Where Contact_Role__c = 'ACT - Client Primary Contact' and company_Name__c=:acc[0].Id)])
{
List<Login__c> check1= new List<Login__c>();
check1=[select Id,Account__c,Password__c,UserName__c,Contact__c from Login__c where Contact__c=: con.Id and Account__c =: acc[0].Id and Name='Client'];
if(check1.size()>0)
{
if(acc[0].Notify_Client__c=='Send Username/Password to Client')
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<EmailTemplate> emailTemp=[Select Subject, Name, HtmlValue, FolderId, DeveloperName, Body From EmailTemplate where developername =:'Password_retrieval'];
string sBody='';
if(emailTemp.size() > 0)
{
sBody=emailTemp[0].Body;
if(sBody != '' || sBody != '')
{
sBody=sBody.replace('FullName', con.Name);
sBody=sBody.replace('$UName$',check1[0].UserName__c);
sBody=sBody.replace('$PWD$',check1[0].Password__c);
}
}

String[] toAddresses= new String[] {con.Email};
List<OrgWideEmailAddress> orgEmailAdd = [Select Id, DisplayName, Address From OrgWideEmailAddress where DisplayName =:'Stanford ACT' limit 1];
if(orgEmailAdd.size()>0)
{
fromAddress = orgEmailAdd[0].Id;
}
// Assign the addresses for the To list to the mail object.
mail.setToAddresses(toAddresses);
mail.setOrgWideEmailAddressId(fromAddress);
if(emailTemp.size() > 0)
mail.SetSubject(emailTemp[0].subject);
mail.setUseSignature(false);
mail.setPlainTextBody(sBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

accnt = acc[0];
accnt.Notify_Client__c = 'Username/Password Sent';
}
}
else
{

if(acc[0].Notify_Client__c=='Send Username/Password to Client')
{
Login__c check= new Login__c();
check.Name='Client';
check.Account__c=acc[0].Id;
check.Contact__c=con.Id;
check.UserName__c=con.Email;
check.Password__c=getChar1();
insert check;

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<EmailTemplate> emailTemp=[Select Subject, Name, HtmlValue, FolderId, DeveloperName, Body From EmailTemplate where developername =:'Client_New_Credentials'];
string sBody='';
if(emailTemp.size() > 0)
{
sBody=emailTemp[0].Body;
if(sBody != '' || sBody != '')
{
sBody=sBody.replace('FullName', con.Name);sBody=sBody.replace('$UName$',check.UserName__c);sBody=sBody.replace('$PWD$',check.Password__c);

}
}


List<OrgWideEmailAddress> orgEmailAdd = [Select Id, DisplayName, Address From OrgWideEmailAddress where DisplayName =:'Stanford ACT' limit 1];
if(orgEmailAdd.size()>0)
{
fromAddress = orgEmailAdd[0].Id;
}
String[] toAddresses= new String[] {con.Email};
// Assign the addresses for the To list to the mail object.
mail.setOrgWideEmailAddressId(fromAddress);
mail.setToAddresses(toAddresses);
if(emailTemp.size() > 0)
mail.SetSubject(emailTemp[0].subject);
mail.setUseSignature(false);
mail.setPlainTextBody (sBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

accnt = acc[0];
accnt.Notify_Client__c = 'Username/Password Sent';
}
}
}
}

}

 

Thanks in Advance