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
jgreene.ax1178jgreene.ax1178 

Trigger Help

OK, so I know nothing about apex code.

 

I want to take this code below, though, and copy it to have the ability to make a 'second' contact record - I will just have to swap out some fields. 

 

However, I'm having a compilling error with this code at line 100 - and it has to do with the latest release of Sumer '13.  How can I make this code work for a SECOND record being created?

 

Thanks so much!

 

public with sharing class ContactExtenssion {
public Contact ContactObj {get;set;}

public ContactExtenssion(ApexPages.StandardController cont) {
Id ContactId = ApexPages.currentPage().getParameters().get('id');
ContactObj = (ContactId == null) ? new Contact() : [Select c.silverpop__Silverpop_RecipientID__c, c.silverpop__Silverpop_Lead_Score__c, c.silverpop__Silverpop_Lead_Rank__c, c.silverpop__Silverpop_Behavior_Score__c, c.silverpop__Lead_Score__c, c.silverpop__Lead_Rank__c, c.silverpop__CRMI_Modified_Date__c, c.Website__c, c.Tour_Scheduled__c, c.Tour_Registered_Date__c, c.Tour_Interview_date_completed__c, c.Tour_Interview_Completed__c, c.Title, c.SystemModstamp, c.Suffix__c, c.Student_Profile__c, c.Specialty__c, c.Special_Needs_Circumstances__c, c.Size_of_Practice__c, c.Secondary_Phone_Type__c, c.Secondary_Last_Name__c, c.Secondary_First_Name__c, c.Secondary_Contact_Relationship__c, c.School_Interested_In__c, c.Salutation, c.ReportsToId, c.Remove_from_Auto_Mailings__c, c.Relationship_with_School__c, c.Referred_by2_del__c, c.Referred_Student__c, c.Referred_By_del__c, c.Referred_By_Source_of_Contact__c, c.Referral_Value_Rating__c, c.Referral_Value_Rating_Indicator__c, c.RecordTypeId, c.Reason_for_inquiry_to_our_school__c, c.Professional_Type__c, c.Professional_Contact__c, c.Profession__c, c.Primary_Phone_Type__c, c.Primary_Phone_2__c, c.Primary_Phone_2_Type__c, c.Primary_Contact__c, c.Primary_Contact_Relationship__c, c.Primary_Contact_Prof_Secondary_Phone__c, c.Primary_Contact_Prof_Lifecycle_Status__c, c.Primary_Contact_Enrollment_Date__c, c.Previously_Referred__c, c.Phone, c.Partner__c, c.Parent_Zip__c, c.Parent_State__c, c.Parent_City__c, c.Parent_Address__c, c.OwnerId, c.OtherStreet, c.OtherState, c.OtherPostalCode, c.OtherCountry, c.OtherCity, c.Original_Campaign_Name__c, c.Number_of_Children_Registering__c, c.Name, c.MasterRecordId, c.MailingStreet, c.MailingState, c.MailingPostalCode, c.MailingCountry, c.MailingCity, c.Linkedin__c, c.Lifecycle_Status__c, c.Level_of_Interest__c, c.Lead_Record_Type__c, c.LeadSource, c.LastName, c.LastModifiedDate, c.LastModifiedById, c.LastCUUpdateDate, c.LastCURequestDate, c.LastActivityDate, c.IsDeleted, c.Interested_in__c, c.Interest_Level__c, c.Interest_Level_Indicator__c, c.Inquiry_Source__c, c.Information_Packet_Sent__c, c.Id, c.How_Did_You_Hear_About_Us__c, c.History_or_concerns__c, c.HasOptedOutOfEmail, c.Grade_Interested_In__c, c.Gender__c, c.FirstName, c.Father_s_Last_Name__c, c.Father_s_First_Name__c, c.Facebook__c, c.ExternalID__c, c.Event_Registered_For__c, c.Enrollment_Year__c, c.Enrollment_Month__c, c.EmailBouncedReason, c.EmailBouncedDate, c.Email, c.Do_Not_Contact__c, c.Division_Interest__c, c.Desires_Tour__c, c.Description, c.CreatedDate, c.CreatedById, c.Contact_Rating_II__c, c.Competitor__c, c.Comments__c, c.Child_s_Middle_Name__c, c.Child_s_Last_Name__c, c.Child_s_First_Name__c, c.Candidate_Potential__c, c.Board_Member__c, c.Birthdate__c, c.Birthdate, c.Affiliations__c, c.Additional_Needs__c, c.Actual_Tour_Time__c, c.Actual_Tour_Date__c, c.AccountId From Contact c where Id =: ContactId];

}

public PageReference Redirect(){
list<User> usr = [Select Id from User where ProfileId =: userinfo.getProfileId()];
PageReference ContactClone;
Contact ClonedContact = new Contact();
ClonedContact = ContactClone();
ContactObj = ResetContact();
try{
insert ClonedContact;
update ContactObj;
ContactClone = new PageReference('/' + ClonedContact.Id);
ContactClone.setRedirect(true);
return ContactClone;
}catch (Exception e) {
ContactClone = new PageReference('/' + ContactObj.Id);
ContactClone.setRedirect(true);
ContactObj.addError(e.getDmlMessage(0));
return ContactClone;
}
}

public Contact ContactClone(){
List<RecordType> RType = [Select r.SobjectType, r.Name, r.Id From RecordType r where SobjectType = 'Contact' and Name = 'Student'];
Contact ClonedContact = new Contact();
ClonedContact.ownerId = ContactObj.OwnerId;
ClonedContact.RecordTypeId = RType.get(0).id;
ClonedContact.AccountId = ContactObj.AccountId;
ClonedContact.Child_s_First_Name__c = ContactObj.Child_s_First_Name__c;
ClonedContact.Child_s_Last_Name__c = ContactObj.Child_s_Last_Name__c;
ClonedContact.FirstName = ContactObj.FirstName;
ClonedContact.LastName = ContactObj.LastName;
ClonedContact.Relationship_with_School__c = 'Registered';
ClonedContact.Lifecycle_Status__c = 'Registered';

if(ContactObj.Child_s_First_Name__c != null && ContactObj.Child_s_Last_Name__c != null){
ClonedContact.FirstName = ContactObj.Child_s_First_Name__c;
ClonedContact.LastName = ContactObj.Child_s_Last_Name__c;
}
ClonedContact.phone = ContactObj.phone;
ClonedContact.Primary_Phone_Type__c = ContactObj.Primary_Phone_Type__c;
ClonedContact.Primary_Phone_2__c = ContactObj.Primary_Phone_2__c;
ClonedContact.Primary_Phone_2_Type__c = ContactObj.Primary_Phone_2_Type__c;
ClonedContact.Additional_Needs__c = ContactObj.Additional_Needs__c;
ClonedContact.Email = '';

//Mailing Address
ClonedContact.MailingStreet = ContactObj.MailingStreet;
ClonedContact.MailingState = ContactObj.MailingState;
ClonedContact.MailingPostalCode = ContactObj.MailingPostalCode;
ClonedContact.MailingCountry= ContactObj.MailingCountry;
ClonedContact.MailingCity = ContactObj.MailingCity;
ClonedContact.Professional_Contact__c = ContactObj.Professional_Contact__c;
ClonedContact.Professional_Contact__c = ContactObj.Professional_Contact__c;
ClonedContact.Referred_by2_del__c = ContactObj.Referred_by2_del__c;
ClonedContact.Referred_By_Source_of_Contact__c = ContactObj.Referred_By_Source_of_Contact__c;


//Student Requirements
ClonedContact.Enrollment_Year__c = ContactObj.Enrollment_Year__c;
ClonedContact.Enrollment_Month__c = ContactObj.Enrollment_Month__c;
ClonedContact.Grade_Interested_In__c = ContactObj.Grade_Interested_In__c;
ClonedContact.School_Interested_In__c = ContactObj.School_Interested_In__c;
ClonedContact.Division_Interest__c = ContactObj.Division_Interest__c;
ClonedContact.Student_Profile__c = ContactObj.Student_Profile__c;
ClonedContact.History_or_concerns__c = ContactObj.History_or_concerns__c;
ClonedContact.Special_Needs_Circumstances__c = ContactObj.Special_Needs_Circumstances__c;
ClonedContact.Reason_for_inquiry_to_our_school__c = ContactObj.Reason_for_inquiry_to_our_school__c;
ClonedContact.Candidate_Potential__c = ContactObj.Candidate_Potential__c;
ClonedContact.Original_Campaign_Name__c = ContactObj.Original_Campaign_Name__c;
ClonedContact.Inquiry_Source__c = ContactObj.Inquiry_Source__c;
ClonedContact.How_Did_You_Hear_About_Us__c = ContactObj.How_Did_You_Hear_About_Us__c;
ClonedContact.Event_Registered_For__c = ContactObj.Event_Registered_For__c;
ClonedContact.Event_Registered_For__c = ContactObj.Event_Registered_For__c;
ClonedContact.Birthdate__c = ContactObj.Birthdate__c;
ClonedContact.Gender__c = ContactObj.Gender__c;
//Tour Information
ClonedContact.Actual_Tour_Date__c = ContactObj.Actual_Tour_Date__c;
ClonedContact.Actual_Tour_Time__c = ContactObj.Actual_Tour_Time__c;

return ClonedContact;
}

public Contact ResetContact(){
ContactObj.Special_Needs_Circumstances__c = '';
ContactObj.Student_Profile__c = '';
ContactObj.History_or_concerns__c = '';
ContactObj.Special_Needs_Circumstances__c = '';
ContactObj.Reason_for_inquiry_to_our_school__c = '';
ContactObj.Additional_Needs__c = '';
return ContactObj;
}
static testMethod void TestContactExtenssion() {
Account Acc = new Account(Name = 'Fareed');
insert Acc;
Contact newContact = new Contact(LastName = 'Test Contact', AccountId = Acc.id, Child_s_First_Name__c = 'Fareed', Child_s_Last_Name__c = 'Baig');
insert newContact;
ApexPages.StandardController con = new ApexPages.StandardController(newContact);
ApexPages.Pagereference pageRef = Page.ContactClone;
pageRef.getParameters().put('id', newContact.Id);
Test.setCurrentPage(pageRef);
ContactExtenssion controllerExtension = new ContactExtenssion(con);

Test.setCurrentPage(pageRef);
Test.startTest();
controllerExtension.Redirect();
Test.stopTest();

}


}

souvik9086souvik9086

Can you highlight for which line you are getting the error?

 

Thanks

jgreene.ax1178jgreene.ax1178

Hi there!  Thanks for responding!

 

It's at line 100, column 24.

 

-Jake

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Can you remove the test method written inside the class in the line 100. Please check th following code. Try running the apex after removing this code from the controller. So, you can make it as a seperate test class to cover the code.

 

static testMethod void TestContactExtenssion() {
Account Acc = new Account(Name = 'Fareed');
insert Acc;
Contact newContact = new Contact(LastName = 'Test Contact', AccountId = Acc.id, Child_s_First_Name__c = 'Fareed', Child_s_Last_Name__c = 'Baig');
insert newContact;
ApexPages.StandardController con = new ApexPages.StandardController(newContact);
ApexPages.Pagereference pageRef = Page.ContactClone;
pageRef.getParameters().put('id', newContact.Id);
Test.setCurrentPage(pageRef);
ContactExtenssion controllerExtension = new ContactExtenssion(con);

Test.setCurrentPage(pageRef);
Test.startTest();
controllerExtension.Redirect();
Test.stopTest();
}

 

Hope this will help you...!