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
The new LearnerThe new Learner 

updating fields on event

Hi Experts,

i have requriemnt where i need to update fields on event record, i wrote the code but it has some isslues like below.

1.e.location=sordermap.get(e.whatid).S__Site__c;: i am not able to populate the S__Site__c value  the event field called location its populating only id(S__Site__c is lookup field for another object called location__C)

2. e.Subject =sordermap.get(e.whatid).Subject__c;:
a) here i need to trim the subject value for first 20 characters 
b)subject filed already there is value coming from another logic so i need to append my subject value to it ( EG: A + test , here A is already value coming from another logic and test is the subject which i am adding through my logic but for me its showing as A +A+test , it means i am seeing A value two times i dont know why.
can anyone help me . Thanks in advance.


trigger sitesubjectupdateonevent on Event (before insert) 
{
  set<id> soidset = new set<id>();
    for(event e : trigger.new)
     {
       if(e.whatid!=null) 
        {
          soidset.add(e.whatid);
        }
     }

Map<id,S_Order__c> sordermap= new Map<id,S_Order__c>([select id, S__Site__c,Subject__c  from  S_Order__c where id in: soidset limit 50000 ]);
  
 for( event e: Trigger.new)
   {
    if(e.whatId!=null && sordermap.containskey(e.whatid))
    {
    
      e.location=sordermap.get(e.whatid).S__Site__c;
   
      e.Subject =sordermap.get(e.whatid).Subject__c;
    }
  }
    
}
Best Answer chosen by The new Learner
Glyn Anderson 3Glyn Anderson 3
Event.Location is a String, so you'll have to get the Name of the S__Site__c record across the parent relationship (I assume it has one - if not, pick a different field).

<pre>
trigger sitesubjectupdateonevent on Event (before insert) 
{
    set<id> soidset = new set<id>();
    for(event e : trigger.new)
    {
        if(e.whatid!=null && e.whatid.getSObjectType()==S_Order__c.sObjectType)
        {
            soidset.add(e.whatid);
        }
    }

    Map<id,S_Order__c> sordermap= new Map<id,S_Order__c>
    (   [   select  id, S__Site__r.Name, Subject__c
            from S_Order__c
            where id in: soidset
            limit 50000
        ]
    );

    for( event e: Trigger.new)
    {
        if(sordermap.containskey(e.whatid))
        {
            e.location=sordermap.get(e.whatid).S__Site__r.Name;
            e.Subject = (e.Subject + sordermap.get(e.whatid).Subject__c).left(20);
        }
    }
}
</pre>