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
RajevlsRajevls 

Fetch the User details from the Assigned to field of Task

I want to fetch the username and id of the 'Assigned To' user(Owner) of the task  . This field is a Lookup(User,Calendar) field.

I want to extract the info so that i can insert the value into another User LookUp field.

 

Plz help.

 

 

sfdcfoxsfdcfox

If it's a lookup field, can't you just copy the ID?

 

That said, you can query for the user the same as you'd query for anything else. You haven't a code sample, or even a premise for what you're trying to do, so I can't really be more helpful at this time.

RajevlsRajevls

String TempObj = T.Owner.Id;
System.Debug('Task Owner........' +TempObj);
if(TempObj.substring(0,2)== '005'){
obj.User_1__c = TempObj;
System.Debug('Trigger.oldMap.get(T.Id).Owner....' + T.Owner);
}

 

obj is of the custom object and User_1_c is the field on the custom object where i am trying to insert the record. 

sfdcfoxsfdcfox

Try this, instead:

 

if( t.ownerid.getsobjecttype() == user.sobjecttype ) {
  obj.user_1__c = t.ownerid;
}

 

RajevlsRajevls

I am trying to get the opportunity number from the Realated Opportunity but its not working : PLz help

if(T.What.getsobjecttype() == Opportunity.sobjecttype ) {
  

String Oppnum == T.What.Opportunity_Number__c;

obj.Text__c = Oppnum;
}

 

I am not getting the oppty number from the releated opportunity

Vinit_KumarVinit_Kumar

Rajev,

 

Change this from

 

String Oppnum == T.What.Opportunity_Number__c;

 

to

 

String Oppnum = T.What.Opportunity_Number__c;

sfdcfoxsfdcfox

That won't work, Vinit. What is a polymorphic key, and so you can't just drill into it like that. Furthermore, related record data isn't automatically queried, so you need a query anyways:

 

trigger onTask on Task (before insert, before update) {
  map<id,opportunity> opps = new map<id,opportunity>();
  for(task t:trigger.new) {
    if(t.whatid!=null&&t.whatid.getsobjecttype()==opportunity.sobjecttype) {
    opps.put(t.whatid,null);
    }
  }
  opps.putAll([select id,opportunity_number__c from opportunity where id in :opps.keyset()]);
  for(task t:trigger.new) {
    if(opps.containskey(t.whatid)) {
      t.text__c = opps.get(t.whatid).opportunity_number__c;
    }
  }
}

 

Vinit_KumarVinit_Kumar

Hi sfdcfox,

 

I forgot to see that,good that you reminded.