1

I have tried to set the name field of the Volunteer Job object to the case number or an auto increment number and neither seems to be working. I have tried to use a trigger:

trigger AddCaseNumber on GW_Volunteers__Volunteer_Job__c (before insert) {

GW_Volunteers__Volunteer_Job__c.Name = lead.Volunteer_Job_Name_Increment__c;

}

and I have tried to set it in the APEX class like this:

GW_Volunteers__Volunteer_Job__c Volunteer_Job = new GW_Volunteers__Volunteer_Job__c(
    GW_Volunteers__Campaign__c = lead.Campaign__c,
    Location__c = lead.Location__c,
    Total_Hours_Due__c = lead.Total_Hours_Due__c,
    Number_of_Hours_Due_Per_Week__c = lead.Number_of_Hours_Due_Per_Week__c,
    GW_Volunteers__Skills_Needed__c = lead.Skills_Needed__c,
    Case_Number__c = newCase.Id,
    Primary_Point_of_Contact__c = contact.Id,
    Name= case.CaseNumber
            );
insert Volunteer_Job;

I have also tried to change the last line to:

Name= lead.Volunteer_Job_Name_Increment__c

where lead.Volunteer_Job_Name_Increment__c is an autonumber field

regardless which method I try, it will only set a random sequence of letters and numbers as the Name field.

This is the entire APEX class

public class ConvertToVolunteerJob_WithCase {

    private Lead lead;
    private Case newCase;
    private Contact contact;
    private Account account;

    public ConvertToVolunteerJob_WithCase(ApexPages.StandardController controller) {
        lead = [SELECT 
                    Id,
                    Status,
                    Existing_Contact_Record__c,
                    FirstName,
                    LastName,
                    Street,
                    City,
                    State,
                    PostalCode,
                    Country,
                    Email,
                    Phone,
                    Salutation,
                    Title,
                    Description,
                    Birthdate__c,
                    Emergency_Contact_Name__c,
                    Emergency_Contact_Phone_Number__c,
                    Gender__c,
                    Race__c,
                    Social_Security_Number__c,
                    LeadSource,
                    Barriers__c,
                    Referring_Agency_ID__c,
                    Disability__c,
                    Household_Veteran_Status__c,
                    Previous_Client__c,
                    Referral_Date__c,
                    Referring_Agency__c,
                    Referring_Counselor__c,
                    Service_Related_Disability__c,
                    Veteran_Status__c,
                    Program__c,
                    Intake_Type__c,
                    Enrolled_Programs__c,
                    Skills_Needed__c,
                    Campaign__c,
                    Location__c,
                    Total_Hours_Due__c,
                    Number_of_Hours_Due_Per_Week__c,
                    Primary_Point_of_Contact__c,
                   Volunteer_Job_Name_Increment__c,
                    Name

                FROM Lead  
                WHERE Id = :controller.getRecord().Id];
    }

    public PageReference cancel() {
        PageReference pg = new PageReference('/' + lead.Id);
        pg.setRedirect(true);
        return pg;
    }

    public PageReference process() {
        if (lead.Status == 'Closed - Converted to Case') {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'You cannot convert a Referral that has already been converted. Instead, clone this Referral, change the status to New, and then convert the new Referral to a Case.'));
            return null;
        }

        if (lead.Existing_Contact_Record__c != null) {
            contact = [SELECT Id, AccountId FROM Contact WHERE Id = :lead.Existing_Contact_Record__c];
            account = new Account(Id = contact.AccountId);
        } else {
            // do not create a new household Account record
            //account = new Account(Name = lead.FirstName + ' ' + lead.LastName + ' Household');
            //insert account;

            //find the bucket account Id which is stored in the Batch Settings
            causeview__BatchSettings__c settings = causeview__BatchSettings__c.getInstance('Default');
            if (settings==null) 
                system.debug('batch setting was not found');
            else {
               account = new Account(Id=settings.causeview__BucketAccountId__c);
               contact = new Contact(AccountId = settings.causeview__BucketAccountId__c);
            }
        }

/*      account.BillingStreet = lead.Street;
        account.BillingCity = lead.City;
        account.BillingState = lead.State;
        account.BillingPostalCode = lead.PostalCode;
        account.BillingCountry = lead.Country;
        update account;
*/   

        contact.FirstName = lead.FirstName;
        contact.LastName = lead.LastName;
        contact.MailingStreet = lead.Street;
        contact.MailingCity = lead.City;
        contact.MailingState = lead.State;
        contact.MailingPostalCode = lead.PostalCode;
        contact.MailingCountry = lead.Country;
        contact.Email = lead.Email;
        contact.Phone = lead.Phone;
        contact.Salutation = lead.Salutation;
        contact.Title = lead.Title;
        contact.Birthdate = lead.Birthdate__c;
        contact.Emergency_Contact_Name__c = lead.Emergency_Contact_Name__c;
        contact.Emergency_Contact_Phone_Number__c = lead.Emergency_Contact_Phone_Number__c;
        contact.Gender__c = lead.Gender__c;
        contact.Race__c = lead.Race__c;
        contact.Social_Security_Number__c = lead.Social_Security_Number__c;
        contact.LeadSource = lead.LeadSource;
        upsert contact;

        RecordType rc = [SELECT Id FROM RecordType WHERE Name = :lead.Program__c];
        newCase = new Case(
            RecordTypeId = rc.Id,
            Related_Referral_Record__c = lead.Id,
            Status = 'Open',
            Opened_Date__c = Date.Today(),

            Barriers__c = lead.Barriers__c,
            Referring_Agency_ID__c = lead.Referring_Agency_ID__c,
            Disability__c = lead.Disability__c,
            Household_Veteran_Status__c = lead.Household_Veteran_Status__c,
            Previous_Client__c = lead.Previous_Client__c,
            Referral_Date__c = lead.Referral_Date__c,
            Referring_Agency__c = lead.Referring_Agency__c,
            Referring_Counselor__c = lead.Referring_Counselor__c,
            Service_Related_Disability__c = lead.Service_Related_Disability__c,
            Veteran_Status__c = lead.Veteran_Status__c,
            Description = lead.Description,
            Enrolled_Programs__c = lead.Enrolled_Programs__c,

            ContactId = contact.Id,
            AccountId = account.Id
        );
        insert newCase;

        lead.Existing_Contact_Record__c = contact.Id;
        lead.Linked_Case__c = newCase.Id;
        lead.Status = 'Closed - Converted to Case';
        update lead;

        RecordType intake = [SELECT Id FROM RecordType WHERE Name = 'Intake'];
        Services_ASR__c service = new Services_ASR__c(
            RecordTypeId = intake.Id,
            Case__c = newCase.Id,
            Date__c = Date.Today(),
            Program_Type__c = lead.Program__c,
            Type__c = lead.Intake_Type__c
        );
        insert service;


        GW_Volunteers__Volunteer_Job__c Volunteer_Job = new GW_Volunteers__Volunteer_Job__c(
            GW_Volunteers__Campaign__c = lead.Campaign__c,
            Location__c = lead.Location__c,
            Total_Hours_Due__c = lead.Total_Hours_Due__c,
            Number_of_Hours_Due_Per_Week__c = lead.Number_of_Hours_Due_Per_Week__c,
            GW_Volunteers__Skills_Needed__c = lead.Skills_Needed__c,
            Case_Number__c = newCase.Id,
            Primary_Point_of_Contact__c = contact.Id,
            Name= lead.Referral_Volunteer_Number__c
                    );
        insert Volunteer_Job;



        PageReference pg = new PageReference('/' + newCase.Id);
        pg.setRedirect(true);
        return pg;
    }   


}
8
  • You might post more of your code. Also try changing your variable names to something other than that object names exactly, like ld instead of lead.
    – greenstork
    Oct 24, 2014 at 18:05
  • Are you sure if you dont have the name field as auto number already?
    – Prady
    Oct 24, 2014 at 18:14
  • I'm sure. It is one of the standard fields and is a text type. When I create record it produces a number that looks like this: a09J000000B7AgA Oct 24, 2014 at 18:16
  • Looks to me its setting up id as name.
    – Prady
    Oct 24, 2014 at 18:18
  • in your trigger what is lead there?
    – Prady
    Oct 24, 2014 at 18:19

1 Answer 1

1

It looks like lead.Referral_Volunteer_Number__c is null because it is not being queried in your lead query. Because it is null and because the Name field cannot be blank, it is getting assigned its own Id as a Name.

1
  • Awesome, I can't believe the whole issue was that I left the field off of the query. I've been staring at this screen for hours, I guess it helps to have a new set of eyes. Oct 24, 2014 at 18:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .