We have one requirement we wants to be update Lookup field.

Let say we have a one Object called "Project_Task" (TASKRAY__Project_Task__c). In the same object we have lookup field called as "Project"(TASKRAY__Project__c).This field is Lookup to(TaskRay Project).Whenever we are select Support Request Team = AE Suport Request, TASKRAY__Project__c(LookupField) should be update "AE NA" Based on Pole Field (User Object) Means (if pole = AE NA ).

here TASKRAY__Project_Task__c(Child),TASKRAY__Project__c(Master).

Please Find the Below trigger which we are tried please update where we made the mistakes.

Trigger TaskRayUpdate on  TASKRAY__Project_Task__c(before insert,before update){  
list<TASKRAY__Project__c> Tasklist=new List<TASKRAY__Project__c>();
for(TASKRAY__Project_Task__c  t : Trigger.new){
 if(t.Type_of_Request__c == 'Support Request'){         
         if(t.createdby.Pole__c == 'North America')            
             t.TASKRAY__Project__c = '18 digit id';            
         else if(t.createdby.Pole__c == 'China')
             t.TASKRAY__Project__c = 'AE China';
         else if(t.createdby.Pole__c == 'APAC')
             t.TASKRAY__Project__c = 'AE APAC';
         else if(t.createdby.Pole__c == 'EMEA')
             t.TASKRAY__Project__c = 'AE EMEA';
         else if(t.createdby.Pole__c == 'LATAM')
             t.TASKRAY__Project__c = 'AE LATAM';
 update Tasklist;

Thanks in Advance


I think you need a map here.

Also I would suggest to use List CustomSetting, or CustomMetadata or a map to store the matching pairs.

Below I show example of how Map can be used to store the matching pairs.

Trigger TaskRayUpdate on  TASKRAY__Project_Task__c(before insert,before update){  
    Map<String, String> matchingPairsMap = new Map<String, String>{
        'North America' => 'AE NA',
         'China' => 'AE China',
         'APAC' => 'AE APAC',
         'EMEA' => 'AE EMEA',
         'LATAM' => 'AE LATAM'
    Map<String, TASKRAY__Project__c> projectsMap = new Map<String, TASKRAY__Project__c>();
    for ( TASKRAY__Project__c p: [ SELECT Id, Name FROM TASKRAY__Project__c WHERE Name IN :matchingPairsMap.values()] ) {
        projectsMap.put( p.Name, p );
    Set<Id> userIds = new Set<Id>();
    for(TASKRAY__Project_Task__c  t : Trigger.new){

    Map<Id, User> usersMap = new Map<Id, User>([ SELECT Id, Pole__c FROM User WHERE Id IN: userIds ]);
    for(TASKRAY__Project_Task__c  t : Trigger.new){
        if(t.Type_of_Request__c == 'Support Request'){         
            User u = usersMap.get( t.OwnerId );
            String pole = u.Pole__c;
            if( !String.isBlank( pole ) && matchingPairsMap.keySet().contains( pole ) ) {
                TASKRAY__Project__c p = projectsMap.get( matchingPairsMap.get( pole ) );
                if ( p != null )
                    t.TASKRAY__Project__c = p.Id;
  • Hi Patlatus, Thanks for your reply, I have updated your logic in my trigger but still not working. Can you please check one more time Thanks – Devi Sep 13 '16 at 9:27
  • Could you please be more precise what exactly is not working? What result do you expect? What do you get? Which are the case conditions? – Patlatus Sep 13 '16 at 10:33
  • Project field is not updating. Whenever we can select Support request = "Support request" Project field should be update Like "AE NA,AE EMEA". I have tried your logic which are mentioned above but not working , and also we made some changes in the map showing error like this "Error: Compile Error: Method does not exist or incorrect signature: [Id].values() at line 9 column 63.Please find below code which we are tried " – Devi Sep 13 '16 at 10:39
  • Not able to copy entire code.Please find bit of logic:Map<String,ID> projectsMap = new Map<String,ID>();if(t.createdby.Pole__c == 'North America') t.TASKRAY__Project__c = projectsMap.get('AE NA').values(); – Devi Sep 13 '16 at 10:44
  • I thought you are trying to populate lookup field on TASKRAY__Project_Task__c. Are you saying that you are trying to populate some field on related project TASKRAY__Project__c? Which field do you want to populate and with which value? – Patlatus Sep 13 '16 at 12:49

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.