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
bhanu_prakashbhanu_prakash 

issue on trigger to update lookup

Hi team,

I have written a code to when ever record id is not equal to 01230000000PVMN and stage is closed own. I need to update tempate name as Test template(It is record which is already stored on my custom template__c object)

trigger test on Opportunity (before insert,before update) {
         map<Id,Template__c> mapIds = new map<Id,Template__c>();
               for(Opportunity opp : trigger.new){
                    if(opp.recordid__c != '01230000000PVMN' && opp.StageName == 'Closed Won'){
                        Template__c mapIds = [select Id, Name ,Owner from Template__c where Id in: mapIds];;                     
                        mapIds.Name = opp.ffrrtemplate__c;
                        mapIds.put(opp.ffrrtemplate__c,temp);
                    }
            }
    if(!mapIds.isEmpty()){
        update mapIds.values();
    }
}


error :
Line 5 : unexpected syntax: 'mismatched input ';' expecting RCURLY'
line 6: Variable does not exist: Name
line 7 : Variable does not exist: temp
line 10 : unexpected syntax: 'missing EOF at 'if''

Thanks on advance
Glyn Anderson 3Glyn Anderson 3
I'm not sure this is what you're trying to do, but it fixes the errors you were getting.  Let me know if you need to do something different.

<pre>
trigger test on Opportunity ( before insert, before update )
{
    // query the record type so that the ID is not hard-coded
    RecordType oppRT =
    [   SELECT Id FROM RecordType
        WHERE sObjectType = 'Opportunity' AND DeveloperName = 'Record_Type_Name'
    ];

    List<Template__c> templatesToInsert = new List<Template__c>();
    for ( Opportunity opp : trigger.new )
    {
        if ( opp.recordid__c != oppRT.Id && opp.StageName == 'Closed Won' )
        {
            templatesToInsert.add( new Template__c( Name = opp.ffrrtemplate__c ) );
        }
    }
    insert templatesToInsert;
}
</pre>
 
bhanu_prakashbhanu_prakash
Thank Anderson,

Trigger got saved but facing error :
Apex trigger test caused an unexpected exception, contact your administrator: test: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject: ()

Use case: when is oppournity stage is closed won and record is not equal to 01230000000PVMN. need to update template (lookup field) as test tempate(is a record name already saved in template object).