+ Start a Discussion
darcusmietzdarcusmietz 

Apex Trigger return User name instead of Unique ID

I have a trigger on a custom object that creates a new opportunity. Everything is working perfectly, except the Market Developer field is showing up with the user' unique IDs, rather than their names. I think I need to create a map and use a select statement from the user object to return the name value, but I'm unsure how to do this. Could you please assist me? I bolded the line that needs to return the name instead of ID.

trigger MDwinning2 on MD_Meeting__c (after update) {
List <Opportunity> oppToInsert = new List <Opportunity> ();
    for (MD_Meeting__c m : Trigger.new) {
        MD_Meeting__c oldMeet = Trigger.oldMap.get(m.Id);
        if (oldMeet.SAL__c != m.SAL__c) {      
        Opportunity o = new Opportunity ();  
        o.OwnerId = m.Sales_Director__c;
        o.Name = m.Name;
        o.StageName = 'Generate Opportunity';
        o.Market_Developer__c = m.Market_Developer__c;
        o.AccountId = m.Account__c;
        o.Type = 'Sales - New Business';
    o.CloseDate = System.Today()+150;
    o.MeetingLookup__c = m.Id;
        oppToInsert.add(o);
        }//end if
    }//end for o
//try {
     insert oppToInsert;
//    } catch (system.Dmlexception e) {
//       system.debug (e);
//    }
}
Best Answer chosen by darcusmietz
SarfarajSarfaraj
yeah I forgot to write that. this is the full code,
trigger MDwinning2 on MD_Meeting__c (after update) {
List <Opportunity> oppToInsert = new List <Opportunity> ();
List<Id> marketDevelopers = new List<Id>();
for (MD_Meeting__c m : Trigger.new)
  marketDevelopers.add(m.Market_Developer__c);
Map<Id, User> users = new Map<Id, USer>([Select Id, Name From User Where id in :marketDevelopers]);

    for (MD_Meeting__c m : Trigger.new) {
        MD_Meeting__c oldMeet = Trigger.oldMap.get(m.Id);
        if (oldMeet.SAL__c != m.SAL__c) {    
        Opportunity o = new Opportunity ();
        o.OwnerId = m.Sales_Director__c;
        o.Name = m.Name;
        o.StageName = 'Generate Opportunity';
        o.Market_Developer__c = users.get(m.Market_Developer__c).Name;
        o.AccountId = m.Account__c;
        o.Type = 'Sales - New Business';
    o.CloseDate = System.Today()+150;
    o.MeetingLookup__c = m.Id;
        oppToInsert.add(o);
        }//end if
    }//end for o
//try {
     insert oppToInsert;
//    } catch (system.Dmlexception e) {
//       system.debug (e);
//    }
}

All Answers

Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

You create lookkup relationship betwwen opportunity and custom object??
SarfarajSarfaraj
Hi

I am assuming that MD_Meeting__c object has a look up field Market_Developer__c to user. Also Opportunity object has a look up field Market_Developer__c to User. I have bolded my modifications.

trigger MDwinning2 on MD_Meeting__c (after update) {
List <Opportunity> oppToInsert = new List <Opportunity> ();
List<Id> marketDevelopers = new List<Id>();
for (MD_Meeting__c m : Trigger.new)
  marketDevelopers.add(m.Market_Developer__c);
Map<Id, User> users = new Map<Id, USer>([Select Id, Name From User Where id in :marketDevelopers]);

    for (MD_Meeting__c m : Trigger.new) {
        MD_Meeting__c oldMeet = Trigger.oldMap.get(m.Id);
        if (oldMeet.SAL__c != m.SAL__c) {     
        Opportunity o = new Opportunity (); 
        o.OwnerId = m.Sales_Director__c;
        o.Name = m.Name;
        o.StageName = 'Generate Opportunity';
        o.Market_Developer__c = users.get(m.Market_Developer__c);
        o.AccountId = m.Account__c;
        o.Type = 'Sales - New Business';
    o.CloseDate = System.Today()+150;
    o.MeetingLookup__c = m.Id;
        oppToInsert.add(o);
        }//end if
    }//end for o
//try {
     insert oppToInsert;
//    } catch (system.Dmlexception e) {
//       system.debug (e);
//    }
}
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

Opportunity object has lookup retaionship with Market_Developer__c ??
Ankit Gupta@ DeveloperAnkit Gupta@ Developer

Hi,

Try the below code snippet

o.Market_Developer__c = users.get(m.Market_Developer__c).name;

SarfarajSarfaraj
yeah I forgot to write that. this is the full code,
trigger MDwinning2 on MD_Meeting__c (after update) {
List <Opportunity> oppToInsert = new List <Opportunity> ();
List<Id> marketDevelopers = new List<Id>();
for (MD_Meeting__c m : Trigger.new)
  marketDevelopers.add(m.Market_Developer__c);
Map<Id, User> users = new Map<Id, USer>([Select Id, Name From User Where id in :marketDevelopers]);

    for (MD_Meeting__c m : Trigger.new) {
        MD_Meeting__c oldMeet = Trigger.oldMap.get(m.Id);
        if (oldMeet.SAL__c != m.SAL__c) {    
        Opportunity o = new Opportunity ();
        o.OwnerId = m.Sales_Director__c;
        o.Name = m.Name;
        o.StageName = 'Generate Opportunity';
        o.Market_Developer__c = users.get(m.Market_Developer__c).Name;
        o.AccountId = m.Account__c;
        o.Type = 'Sales - New Business';
    o.CloseDate = System.Today()+150;
    o.MeetingLookup__c = m.Id;
        oppToInsert.add(o);
        }//end if
    }//end for o
//try {
     insert oppToInsert;
//    } catch (system.Dmlexception e) {
//       system.debug (e);
//    }
}
This was selected as the best answer