0

I have a Master-Detail relationship with Person__c and Natural_Person__c

I need to update the field Person__c.Client_Name__c to the value of NaturalPerson__c.Surname__c + ', ' + NaturalPerson__c.Forename__c whenever these value change on editing the Natural_Person__c object

I have the following code:

public with sharing class NaturalPerson_ViewController {

public Natural_Person__c naturalPerson { get; set; }
public Person__c person { get; set; }

// get current screen record
public NaturalPerson_ViewController(ApexPages.StandardController stdController) {
    naturalPerson = (Natural_Person__c) stdController.getRecord();
}

// save new Natural_Person__c and Person__c records
public PageReference doSave() {
    try {
        // update Natural_Person__c
        naturalPerson.Name = naturalPerson.Client_Number__c + ' - ' + naturalPerson.Surname__c + ', ' + naturalPerson.Forename__c;
        update(naturalPerson);

        **Person__c person = new Person__c([SELECT Id, Name, Client_Name__c FROM Person__c WHERE Id =: naturalPerson.Client_Number__c]);**
        person.Client_Name__c = naturalPerson.Surname__c + ', ' + naturalPerson.Forename__c;
        update(person);

    } catch(System.DMLException e) {
        ApexPages.addMessages(e);
        return null;
    }

    //  After successful Save, navigate to the default view page
    PageReference redirectSave = new ApexPages.StandardController(naturalPerson).view();
    return(redirectSave);
}

public PageReference doCancel()
{
    //  navigate to the default view page
    PageReference redirectCancel = new ApexPages.StandardController(naturalPerson).cancel();
    return(redirectCancel);
}

}

On compiling the code I get an error Type requires name=value pair construction: Person__c

Any assistance would be greatly appreciated.

Changed code to following:-

Person__c person = [SELECT Id, Name, Client_Name__c FROM Person__c WHERE Name =: naturalPerson.Client_Number__c LIMIT 1];

This enabled the SOQL to obtain the only record as a singleton into the Person__c object.

3
  • 1
    Try adding a LIMIT 1 to the query and Do Person__c person = SOQL qyery and not Person__c person = new Person__c(SOQL Query);
    – d_k
    Commented Apr 18, 2017 at 4:59
  • If the solution worked for you, please post it as an answer and close this thread, so that system doesn't consider it as unanswered one. Commented Apr 18, 2017 at 6:24
  • Please mark the comment as useful if it was helpful @Mark
    – d_k
    Commented Apr 18, 2017 at 12:16

0

You must log in to answer this question.

Browse other questions tagged .