1

I have two methods in my Apex controller that create/update a record

// Creating a new item
@AuraEnabled
public static Item__c newItemDB(Item__c item) {
            
    insert item;
    return item;
}

// Updating item
@AuraEnabled
public static Item__c updateItemDB(Item__c item) {
            
    update item;
    return item;
}

The problem I have is when they are executed, two of the fields are not displaying on the page because they are lookup relationships. When I refresh the page they do display.

How do I get lookup relationship data on these methods?

Update:

I did as you suggested and it works!

The problem now is my test classes are failing

System.AssertException: Assertion Failed:

Expected: TouchpointDev__Whiteboard_Item__c:{Name=Test Item 1, TouchpointDev__Item_Due_Date__c=2016-08-18 00:00:00, TouchpointDev__AssignedTo__c=005d0000006FYTHAA4, TouchpointDev__High_Priority__c=false, TouchpointDev__PersonalItem__c=true, TouchpointDev__Completed__c=false, Id=a00d000000k2s6iAAA},

Actual: TouchpointDev__Whiteboard_Item__c:{Id=a00d000000k2s6iAAA, Name=Test Item 1, TouchpointDev__PersonalItem__c=true, TouchpointDev__AssignedTo__c=005d0000006FYTHAA4, TouchpointDev__Completed__c=false, TouchpointDev__High_Priority__c=false, TouchpointDev__Item_Due_Date__c=2016-08-18 00:00:00}

All the data matches, but I think it's failing cause it's not in the order it needs be now. How do I fix that?

3
  • How are you showing the child records initially?
    – Praveen
    Commented Aug 18, 2016 at 17:30
  • I'm calling them through another method in my Apex Controller and using in the SOQL a AssignedTo__r.Name
    – Tyler Zika
    Commented Aug 18, 2016 at 17:36
  • The same way you need to query the Item__c again to get it's Parent's Name.
    – Praveen
    Commented Aug 18, 2016 at 17:39

1 Answer 1

0

After saving the Item__c you need to query the record to get the Parent's Name field.

@AuraEnabled
public static Item__c upsertItemDB(Item__c item) {
    if(item.id != NULL){
        update item;
    }
    else{
        insert item;
    }

    return [SELECT Id,Field1,...,Parent__r.Name FROM Item__c WHERE Id = :item.id];
}

If there is no complex calculation/manipulation, before doing insert/update like something you have posted in the question.You could use, upsert instead of having two separate method to update and insert / checking for id to switch DML.

@AuraEnabled
public static Item__c upsertItemDB(Item__c item) {
    upsert item; // If id is present Update, else Insert is done
    return [SELECT Id,Field1,...,Parent__r.Name FROM Item__c WHERE Id = :item.id];
}
1
  • check my update.
    – Tyler Zika
    Commented Aug 18, 2016 at 21:36

You must log in to answer this question.

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