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
sean.gorman@ipc.comsean.gorman@ipc.com 

Associating two objects using a trigger

Hi,


I'm not a trained developer so please excuse me if this seems a bit obvious.


I have 2 custom ojects that are both related to Opportunity. Quote_Request__c (QER) and Quote__c (Q).


These are obviously related one to another. They both hold a field called Quote_Header_ID__c. When a quote is returned by our integration with our internal quoting system we would like to see to which QER the quote is related.


So: there is an additional field on the quote table called Quote_Estimate_Request__c which is a lookup relationship to the Quote_Request__c object.


Quote Header Ids in will be unique for the QER but there may be many quotes related.


So... What I did thus far:

 

trigger addQERassociation on Quote__c (before insert, before update) {

    Decimal dQERid;
    for (Quote__c q :Trigger.new)
    {
        dQERid= q.quote_header_id__c;
        if(dQERid!= '')
        {
            for (List<Quote_Request__c> qer :[Select ID from Quote_Request__c qr where (qr.Quote_Header_ID__c = 123456)]) //dQERid)])
            {
                if(qer.Size() > 0)
                {
                    q.Quote_Estimate_Request__c = qer[0];
                }
            }
        }
    }
}

 

The current issue is that I am getting a compile error: Error: Compile Error: Illegal assignment from SOBJECT:Quote_Request__c to Id at line 13 column 21

 

The other is that I cannot put the variable: dQERid into the select statement as it errors out with: Error: Compile Error: unexpected token: 'dQERid' at line 9 column 112 (Fixed: I added a colon to make :dQERid)

 

 

Does anyone have any advice? Please?

Best Answer chosen by Admin (Salesforce Developers) 
sean.gorman@ipc.comsean.gorman@ipc.com

I did it and it works:

 

My changes are highlighted in red

 

 

trigger addQERassociation on Quote__c (before insert, before update) {

    for (Quote__c q :Trigger.new)
    {
        decimal idQERid;
        idQERid= q.quote_header_id__c;
        if(idQERid != 0)
        {
            for (List<Quote_Request__c> qer :[Select ID from Quote_Request__c qr where (qr.Quote_Header_ID__c = :idQERid)])
            {
                if(qer.Size() > 0)
                {
                    q.Quote_Estimate_Request__c = qer[0].ID;
                }
            }
        }
    }
}