0

I'm trying to create a trigger to copy before insert a formula field (the account of the contact ""Contact__r.Account.Name"") into a lookup field.

I tried this but I have problem (Illegal assiggnement from List to List )

trigger CopyValue on SurveyTaker__c (before insert) {
    for(SurveyTaker__c survey : trigger.new)
    {
        List<id> query;
        query = Database.query('SELECT id FROM Account WHERE Name =      
 \''+survey.AccountName__c+'\'  ');
       id accId = query[0];
        survey.Account_Survey_Contact__c = query[0];
   }

}

Is there any way to change the record type to be able to copy this value?

Thanks,

  • first don't query in for loop. second change List<id> query to` List<account> query`. use map for bulkification. – Tushar Sharma Mar 14 '16 at 10:45
0

You can greatly simplify this by:

  • Creating a formula on the SurveyTaker__c object that references the Account Id. We'll call this AccountId__c
  • Simply update the Account_Survey_Contact__c field with the value in your new AccountId__c field, via a trigger.

I don't recall off the top of my head whether a lookup field can be updated via the Process Builder

Your trigger would then look like:

trigger CopyValue on SurveyTaker__c (before insert) {
    for(SurveyTaker__c survey : trigger.new)
    {
        survey.Account_Survey_Contact__c = survey.AccountId__c;
    }
}

As an FYI, there are a few issues with your current trigger:

  • SOQL query in for loop
  • SOQL query vulnerable to SOQL injection. Use List<Account> accs = [SELECT id FROM Account WHERE Name IN :survey.AccountName__c]; instead. Although the survey.AccountName__c would need to be replaced with a list of account names that you'd have to determine before entering that for loop.
  • querying accounts but storing them in a list of Ids.
0

Try something like this, it worked for me

List<Account> query;

 query = [SELECT id FROM Account WHERE Name =: survey.sked__Region__c];

id accId = query[0].id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.