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 

update lookup field with help of another

Hi,

Iam trying to update field on oppournity, where recordid(lookup) is equal to 00528000001KyAT, need to update account id(lookup ) as 0012800000lIenH.
I have written a trigger

trigger updateif on Opportunity (before insert,before update) {
if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){ 
          list<string> lstStr = new list<string>();
      for(opportunity opp : trigger.new){
          if(opp.record_id__c = '00528000001KyAT'){
              opp.update__c = '0012800000lIenH';
}
}
}
}

Help me to  fix this
Best Answer chosen by bhanu_prakash
Glyn Anderson 3Glyn Anderson 3
I suggest spending a couple of queries so that you don't have to hard-code IDs at all.  Especially since these hard-coded values won't work in your sandboxes.

<pre>
trigger updateif on Opportunity ( before insert, before update )
{
    if ( trigger.isBefore && (trigger.isInsert || trigger.isUpdate) )
    {
        User specialUser =
        [   SELECT  Id
            FROM    User
            WHERE   UserName = 'put.correct@username.here'
        ];

        Account specialAccount =
        [   SELECT  Id
            FROM    Account
            WHERE   Name = 'Put Correct Account Name Here'
        ];

        for ( Opportunity opp : Trigger.new )
        {
            if ( opp.Record_Id__c = specialUser.Id ) opp.Update__c = specialAccount.Id;
        }
    }
}
</pre>
 

All Answers

bhanu_prakashbhanu_prakash
Is need to be update when stage is closed won
Nithesh NNithesh N
Try Using 'ID' datatype... and '==' for equal condition check.
Id rId = '00528000001KyAT';
Id uId = '0012800000lIenH';
if(opp.record_id__c == rId){
   opp.update__c = uId;
}

Best,
Nithesh
PawanKumarPawanKumar
Hi,
Actually you are not comparing value for record_id__c in your code so added equalignorecase(). Please try below code and please let me know if it works for you.

-------------------------------------------------------

trigger updateif on Opportunity(before insert, before update) {
 if (trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
  list < string > lstStr = new list < string > ();
  for (opportunity opp: trigger.new) {
   if ((opp.record_id__c).equalsignorecase('00528000001KyAT')) {
    opp.update__c = '0012800000lIenH';
   }
  }
 }
}

--------------------------------------------

Regards,
Pawan Kumar
Glyn Anderson 3Glyn Anderson 3
I suggest spending a couple of queries so that you don't have to hard-code IDs at all.  Especially since these hard-coded values won't work in your sandboxes.

<pre>
trigger updateif on Opportunity ( before insert, before update )
{
    if ( trigger.isBefore && (trigger.isInsert || trigger.isUpdate) )
    {
        User specialUser =
        [   SELECT  Id
            FROM    User
            WHERE   UserName = 'put.correct@username.here'
        ];

        Account specialAccount =
        [   SELECT  Id
            FROM    Account
            WHERE   Name = 'Put Correct Account Name Here'
        ];

        for ( Opportunity opp : Trigger.new )
        {
            if ( opp.Record_Id__c = specialUser.Id ) opp.Update__c = specialAccount.Id;
        }
    }
}
</pre>
 
This was selected as the best answer
Glyn Anderson 3Glyn Anderson 3
And correct line 19 to use the comparison operator, "==":

<pre>
 if ( opp.Record_Id__c == specialUser.Id ) opp.Update__c = specialAccount.Id;
</pre>