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
venkateshvenkatesh 

Please help want regarding Lookup field update problem


Hi,

 

      Help!! Help!! Please help me if anyone knows how to update the lookup field within the Object according to associeted Id from another object..

 

trigger updateLookup on Sales_Order__c (after insert, before update) {

// Store all changed sales order object
Map<Id,Id> salesOrder = new Map<Id,Id>();
for(Sales_Order__c s : Trigger.new){
salesOrder.put(s.Id, s.OwnerId);
}

// Create a map of all of the log created according to triggered Sales Order.
Map<Id,Id> createdLog = new Map<Id,Id>();
for(ConnectCompiere_Log__c log : [select Id, Name from ConnectCompiere_Log__c where Id in :salesOrder.values()]) {
createdLog.put(log.Id, log.Name);
}

// Update the sales order lookup field.

for (Sales_Order__c sales: Trigger.new){
sales.Lookup_Log__c = createdLog.get(sales.Id);
}
}

 

I am getting the error like updateLookup: execution of AfterInsert caused by: System.Exception: Record is read-only: Trigger.updateLookup: line 19, column 13

 

This error is in the last  for loop.

 :smileymad:

Please help me.

 

 

Please help me anyone who knows Lookup field update.

 

Is that Lookup field is read-only?

 

Why I can't update the Lookup field with the corresponding Id of the another object?

 

Thanks in advance.

:smileysad: :smileysad:
Message Edited by venkatesh on 02-21-2009 01:44 AM
Message Edited by venkatesh on 02-21-2009 02:56 AM
Message Edited by venkatesh on 02-21-2009 02:57 AM
Best Answer chosen by Admin (Salesforce Developers) 
NaishadhNaishadh

Hi,

 

Here is the solution.

 

Object One :- ParentObject : Contains Ref field of child object : Field API Name :- ChildObjectRef__c

Object Two :- ChildObject

 

Trigger on ParentObject

trigger TestRef on ParentObject__c (before insert,before update) {

List<ParentObject__c> parentList = new List<ParentObject__c> ();

for(ParentObject__c po : Trigger.new) {
parentList.add(po);
}

ParentObjectClass.processParentList(parentList);

}

 ParentObejctClass

 

public class ParentObjectClass {
private static List<ChildObject__c> childList = new List<ChildObject__c> ();

public static void processParentList(List<ParentObject__c> poList) {
Integer i = 0;
for(Integer j = 0; j<poList.size(); j++) {
ChildObject__c co = new ChildObject__c (
name = 'test' + i
);

i++;

childList.add(co);
}

insert childList;

i = 0;
for(ParentObject__c po : poList) {
po.ChildObjectRef__c = childList.get(i).Id;
i++;
}
}
}

 

 

All Answers

NaishadhNaishadh

Hi,

 

logic is same for both the condition i.e. after insert and before  update?

 

If yes

 

you can directly put the value in salesOrder field no need to call update on salesOrder.

 

i.e. salesOrder.ConnectCompiere_Log__c = log.id

venkateshvenkatesh

I did not get your point. There salesOrder is an object, not a field. As per your suggestion code won't work.
But logic is same for both conditions. Can u suggest me in what way I can update the lookup field. It would be better if you provide some code snippsets.

 

:smileysad: :smileysad: :smileysad:

Message Edited by venkatesh on 02-21-2009 03:56 AM
venkateshvenkatesh

Please help me anyone who knows updating the lookup field in one object with associated Id of record in another object.

 

Thanks in advance.

SuperfellSuperfell
In an after insert trigger, the trigger.new records are read-only, its too late, you've already missed the boat to make db changes, you should be fine for before update though. for after insert, you'll probably need to either re-think your approach (what's creating these ConnectCompaiere_Log_C records ?), or create a new set of Sales_Order__c object instances set their ids from the trigger.new, and call update and that (being careful about loops, that update call would cause the before update trigger to fire again).
venkateshvenkatesh

If I remove the after insert, that will work fine. But still I am not getting the result what I expect. Below I will explain what I need. Please give me the solution.

 

1) I have to trigger the  Sales_Order__c object which contains lookup field with including other field.

2) I have to create the new log records in  ConnectCompaiere_Log_C (log custom object) bsed on some field missing conditions in Sales_Order__c . (This process is already done).

3) After that I have to update the newly created log record(Name or Id) to the lookup field in corresponding Sales_Order__c record.

 

Please provide some details, how I can update the lookup field of Sales_Order__c object.

 

Thanks for your previous reply.

 

 

Please Please Please help me for my above scenario...........

 

:smileymad: :smileymad: :smileysad::smileysad:

 

I am  keep watching for any kind of suggestion or help........................

 

Please Please!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Message Edited by venkatesh on 02-22-2009 09:45 PM
Message Edited by venkatesh on 02-22-2009 10:29 PM
Message Edited by venkatesh on 02-22-2009 10:30 PM
Message Edited by venkatesh on 02-22-2009 11:13 PM
Message Edited by venkatesh on 02-23-2009 12:41 AM
NaishadhNaishadh

Hi,

 

Here is the solution.

 

Object One :- ParentObject : Contains Ref field of child object : Field API Name :- ChildObjectRef__c

Object Two :- ChildObject

 

Trigger on ParentObject

trigger TestRef on ParentObject__c (before insert,before update) {

List<ParentObject__c> parentList = new List<ParentObject__c> ();

for(ParentObject__c po : Trigger.new) {
parentList.add(po);
}

ParentObjectClass.processParentList(parentList);

}

 ParentObejctClass

 

public class ParentObjectClass {
private static List<ChildObject__c> childList = new List<ChildObject__c> ();

public static void processParentList(List<ParentObject__c> poList) {
Integer i = 0;
for(Integer j = 0; j<poList.size(); j++) {
ChildObject__c co = new ChildObject__c (
name = 'test' + i
);

i++;

childList.add(co);
}

insert childList;

i = 0;
for(ParentObject__c po : poList) {
po.ChildObjectRef__c = childList.get(i).Id;
i++;
}
}
}

 

 

This was selected as the best answer
venkateshvenkatesh

Thanks you very very much Naishadh. Since I was trying from Last 4-5 days. But I could not figured out. You have resolved my problem. This will helps lot many peoples. Thanks for your great help!!!!!!!

 

:smileytongue: :smileyvery-happy: