You need to sign in to do that
Don't have an account?

Trigger - Assign Lookup Field Value When Querying on Another Value
Hello,
I need help with a trigger. I am trying to assign the correct Id on a Lookup Field based on querying for a corresponding value on the related Custom Object. Here is my trigger, but all I get is "Expression cannot be assigned at line -1 column -1". Help would be greatly appreciated!
trigger objectConnect on Custom_Object_B__c (after insert) { Map<String, Custom_Object_B__c> cobMap = new Map<String, Custom_Object_B__c>(); for (Custom_Object_B__c cob : System.Trigger.new) { if (cob.ID_Field__c == null) { cob.ID_Field__c.addError('A valid ID is required.'); } else { skcMap.put(cob.ID_Field__c, cob); } } for (Custom_Object_A__c coa : [SELECT Id, ID_Field__c FROM Custom_Object_A__c WHERE SKID__c IN :cobMap.KeySet()]) { Custom_Object_B__c.Look_Up_Field__c = coa.Id; } }
Anyone know what my problem is? I have searched on the error, but have not found anyone with that same error.
What are you trying to do, can you explain your requirement?
Check, if this is what all you are trying to do..
trigger objectConnect on Custom_Object_B__c (before insert) //before insert is fine
{
Map<String, Custom_Object_B__c> cobMap = new Map<String, Custom_Object_B__c>();
for (Custom_Object_B__c cob : System.Trigger.new) {
if (cob.ID_Field__c == null) {
cob.ID_Field__c.addError('A valid ID is required.');
}
else {
cobMap.put(cob.ID_Field__c, cob);
}
}
list<Custom_Object_A__c> lstcoa = [SELECT Id, ID_Field__c FROM Custom_Object_A__c WHERE Id IN :cobMap.KeySet()];
for((Custom_Object_B__c c: Trigger.New)
{
for(Integer i = 0; i < lstcoa.size(); i++)
{
if(lstcoa[i].Id_Field__c == c.ID_Field__c)
c.Id_Field__c = lstcoa[i].Id_Field__c;
break;
}
}
}
If not, you should post your exact requirement.
All Answers
what is skc map? i assume that you have written it by mistake...instead of writing cobMap......ur cobMap.keyset() contains Id_Field__c...you are comparing it with SKID__c...probably thats the problem..
Yeah, I tried to change all of my Object names...late night. Here is what it should read:
Still getting the error
What are you trying to do, can you explain your requirement?
Check, if this is what all you are trying to do..
trigger objectConnect on Custom_Object_B__c (before insert) //before insert is fine
{
Map<String, Custom_Object_B__c> cobMap = new Map<String, Custom_Object_B__c>();
for (Custom_Object_B__c cob : System.Trigger.new) {
if (cob.ID_Field__c == null) {
cob.ID_Field__c.addError('A valid ID is required.');
}
else {
cobMap.put(cob.ID_Field__c, cob);
}
}
list<Custom_Object_A__c> lstcoa = [SELECT Id, ID_Field__c FROM Custom_Object_A__c WHERE Id IN :cobMap.KeySet()];
for((Custom_Object_B__c c: Trigger.New)
{
for(Integer i = 0; i < lstcoa.size(); i++)
{
if(lstcoa[i].Id_Field__c == c.ID_Field__c)
c.Id_Field__c = lstcoa[i].Id_Field__c;
break;
}
}
}
If not, you should post your exact requirement.
Modified the fields a little, but this was perfect! Thank you! :smileyhappy:
I am glad it worked for you. Though I did not understand the business goal here :)
The business goal was to assign the lookup value based on a different identifier.