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
VibrateVibrate 

Unable to save lookup custom object id in based object after upsert

Hi,

 

I have an object that has a Master-Detail relation with one object and a lookup relation with another object (like a join table).  Now when I add a new record all objects are updated correctly.  That is the detail record of the master-detail relation is created, the record id of the detail record is stored in the join object, the lookup record is created and the record id of the lookup record is stored in the join object.  However, when I try to add new lookup records to the join object like thru a upsert the systm will not stored record ids for the lookup records in the join object.  I guess my question is; Is there a way to get SalesForce to automatically  store record ids for lookup records in the base object when you do an upsert?

 

Thanks,

Vibrate 

Avidev9Avidev9
Not clear!
Can you post some code ? with lil bit of explanation ?
VibrateVibrate

Avi,

 

This is a difficult one to explain but let me try.  I have a lookup object with a list of conditions.  Like a lookup list.  This object we can call special conditions lookup.  Next I have another object that references this lookup object.  This second object we can call special conditons.  finally I have a Mater-Detail object that is referenced by the second object as well and this detail object in the master-detail relationship we can call permits.  Now when we create a new permit we need to add one or more special conditons to that permit.  So when the permit is created the user get a list of special conditons to select from. When the user makes the selection these special conditons lookup rows form the special conditons look up object are linked to the special condtions object via the system saving the special conditons lookup recordid in the special conditons table.  please see the dample code below:

for

(up_SpecialConditionsLookUp up_sp_lookupObj: sc)

{

if(up_sp_lookupObj.isSelected)

{

for(UP_Special_Condition_LookUp__cup_sp_lookupsObj: up_sc_sObjLst)

{

if(up_sp_lookupsObj.id == up_sp_lookupObj.specialConditionsId) {

 

UP_Special_Condition__c up_sc = newUP_Special_Condition__c();

 

up_sc.

UP_Special_Condition_LookUp__c= up_sp_lookupsObj.id;

up_sc.Condition_Tect_del__c = up_sp_lookupObj.specialConditionsText;

up_sc.Utility_Permit3__c = up.ID;

lstUpSC.add(up_sc);

}

}

}

}

try

{

if(lstUpSC.size()>0)

{

database.

insert(lstUpSC);

addInfo(

'Record saved');

}

}

catch(DmlException e)

{

addError(

'Record was not saved: '+ e.getMessage());

 

returnnull;

}

 

This code works fine and saves all related objects including the special conditions lookup id into the special condtions object.  This is good because when I go to display the info on the view screen I can get all selected special conditions from the special condtitions lookup object via the id in the special condtions table.  The problem comes when the user needs to edit the permit special contitions to add or remove special condtions.  First I diasply all the previosuly selected special condtions with selected check boxes next to each than I dsiplay all unselected special condtions win un-selected check boxes next to each.  Here is a part of that code:

List<

UP_Special_Condition__c> SelectedConditions = new List<UP_Special_Condition__c>();

SelectedConditions = [

select id, Condition_Tect_del__c, Utility_Permit3__c, UP_Special_Condition_LookUp__r.id fromUP_Special_Condition__cwhereUtility_Permit3__c = :up.id];

List<

UP_Special_Condition_LookUp__c> AllConditions = new List<UP_Special_Condition_LookUp__c>();

AllConditions = [

select id, Condition_Tect__c, District__c, Special_Condition_Number__c fromUP_Special_Condition_LookUp__cwhereDistrict__c =:userDistrictNumber.Trim()];

 

WrapSPConditionList =

newList<WrapSPCondition>();

 

for(UP_Special_Condition_LookUp__cAllConditionItem : AllConditions)

{

 

boolean ItemIsSelected =

false;

WrapSPCondition WSC =

newWrapSPCondition();

 

for(UP_Special_Condition__cSelectedConditionItem:SelectedConditions)

{

if(SelectedConditionItem.UP_Special_Condition_LookUp__r.id == AllConditionItem.id)

{

//ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, SelectedConditionItem.Condition_Tect_del__c));

WSC.ConditionText = SelectedConditionItem.Condition_Tect_del__c;

WSC.IsSelected =

true;

WSC.specialConditionsId = SelectedConditionItem.Id;

ItemIsSelected =

true;

WrapSPConditionList.add(WSC);

 

}

}

if(ItemIsSelected == false)

{

WSC.ConditionText = AllConditionItem.Condition_Tect__c;

WSC.IsSelected =

false;

WrapSPConditionList.add(WSC);

}

//WrapSPConditionList.add(WSC);

}

return WrapSPConditionList;

 

As you can see, I am using the look up id in the special conditons object to determine which records were selected.  My problem is during the save process I don't know how to get the lookup id for the additional selected records so that I can save those to the special condtions object.  Here is a part of the code I am using tosave the edited records:

for(WrapSPCondition one_sc : WrapSPConditionList)

{

if(one_sc.IsSelected)

{

UP_Special_Condition__c up_sc = newUP_Special_Condition__c();

up_sc.Id = one_sc.specialConditionsId;

up_sc.Condition_Tect_del__c = one_sc.ConditionText;

up_sc.Utility_Permit3__c = up.ID;

lstUpSC.add(up_sc);

}

}

try

{

if( lstUpSC.size() > 0)

{

Database.

upsert(lstUpSC);

addInfo(

'Record Updated');

}

}

catch(DmlException e)

{

addError(

'Record was not saved: '+ e.getMessage());

 

returnnull;

}

As you can see the records will not be selected because I am unable to add it to the selected list when I try to get all selected records based on lookup id.  I hope this help shed light on my problem.  Any and all help is appreciated.

 

Thanks,

Vibrate