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
SF_DEV_CDESF_DEV_CDE 

Trigger to update a Looup field ,with Text Field Value

Hi All

 

Need help on Trigger ,

 

My scenario is : I have a text field(jobNumber)  and a Lookup Field(Job),Job field to be updated by Job Number (Need to get  a name of the Number in the lookup)and It contains more than one value(normally it contains jobnumber1;jobnumber2;jobnumber3; etc),needs to update lookup field with the first value of Job Number.

 

 

Abhinav GuptaAbhinav Gupta

You can write a before update/insert trigger, that will query Job record for Job number and do the update.

Something like this

 

// Query the required job object for the number, please note the limit 1 to get one record only.
Job__c job = [Select Id from Job__c where Number__c =:jobNumber Limit 1 order by Number__c];

// update the lookup field
Sobject.Job__c = job.id;

 

Note : you need to bulkify this if using in Trigger.

SF_DEV_CDESF_DEV_CDE

Got the point , but I wil have bulk inserts and bulk updates and that text field contains more than one value like as 123455;123457;154332; etcso I need to get only the first value 123455 in to the lookup field , any idea to get that value and look for the name of that using SFDC ID and updating in lookup field

Navatar_DbSupNavatar_DbSup

Hi,


Store that text field value inside a string and use split method find that first value.Try the below code as reference:
String st='123455;123457;154332';

String[] st1=st.split(';');
System.debug('@@@@@@@@@@@@@'+ st1[0]);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

SF_DEV_CDESF_DEV_CDE

Hi

 

I splitted it but i need to split all the inserted records at a time and I need to get this first values's SFDC ID and update the lookup ,how can i do this?