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
svpsvp 

Urgent help

Hi,
I have a requirement like to create a new record based on the picklist value and associate new one to the  old
record via look up field on the same object, Is it possible, can someone help how to do this ?
Thanks a lot inadvance.

I have tried like this,but no luck getting errors :-(

 


sObject oldobj;
if(sObject.picklist_c == 'xxx')
  {

   sObject newobj = new sObject();
     newobj.Field1 = 'sometext';
     newobj.Field2 = 'sometext';

     // inser newobj.values( );
     insert newobj;
  }

   oldobj.LookupField__c = newobj.Id;
   update oldobj;

 

 

Thanks& Regards,

Best Answer chosen by Admin (Salesforce Developers) 
Hengky IlawanHengky Ilawan
Hi,

newObj variable is defined inside the if, it won't be available outside the if scope.

Regards,
Hengky

All Answers

Hengky IlawanHengky Ilawan

i think you should use the variable instead of the object name

 

if (oldobj.picklist__c == 'xxx')
...

 

Regards,

Hengky

 

svpsvp

Hi,

 

Thank you for your reply. 

It was a Typo mistake. Getting errors with the code in RED color only i.e., lookup  old record with new record ( association).

 

sObject oldobj;
if(oldobj.picklist_c == 'xxx')
  {

   sObject newobj = new sObject();
     newobj.Field1 = 'sometext';
     newobj.Field2 = 'sometext';

     // inser newobj.values( );
     insert newobj;
  }

   oldobj.LookupField__c = newobj.Id;
   update oldobj;

 

 

Best Regards,

Hengky IlawanHengky Ilawan
Hi,

newObj variable is defined inside the if, it won't be available outside the if scope.

Regards,
Hengky
This was selected as the best answer
svpsvp

Thank you very much. I missed that :-(.

 

Best Regards,

svp.