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
pdostapdosta 

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.

 

Best Answer chosen by Admin (Salesforce Developers) 
sforce2009sforce2009

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

Shailesh DeshpandeShailesh Deshpande

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..

pdostapdosta

Yeah, I tried to change all of my Object names...late night.  Here is what it should read:

 

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 {
            cobMap.put(cob.ID_Field__c, cob);
            }
    }

    for (Custom_Object_A__c coa : [SELECT Id, ID_Field__c FROM Custom_Object_A__c WHERE ID_Field__c IN :cobMap.KeySet()]) {
            Custom_Object_B__c.Look_Up_Field__c = coa.Id;
    }
}

 

Still getting the error

 

sforce2009sforce2009

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.

This was selected as the best answer
pdostapdosta

Modified the fields a little, but this was perfect!  Thank you! :smileyhappy:

 

sforce2009sforce2009

I am glad it worked for you. Though I did not understand the business goal here :)

pdostapdosta

The business goal was to assign the lookup value based on a different identifier.