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
Kristi GrayKristi Gray 

Rich Text Trigger

Hi! I am REALLY new to coding. I cannot seem to get this simple trigger to work. I want the rich text from my Service__c.Deliverables__c to be copied to Selected_Service__c.Custom_Description__c. What am I doing wrong? It gives me the error 'A value cannot be stored to Custom_Description__c in type Selected_Service__c

trigger updtrichtextfld on Selected_Service__c (before insert) {

for(Selected_Service__c u:trigger.new){
Selected_Service__c.Custom_Description__c=Service__c.Deliverables__c;
}
}
Best Answer chosen by Kristi Gray
riyaviriyavi

If the trigger is meant only for the above mentioned field update, you can use process builder rather than a trigger.
If not,  Assuming Service__c is an object related to Selected_Service__c, and Service__c is the lookup field on Selected_Service__c  object, below should help

trigger updtrichtextfld on Selected_Service__c (before insert) {
Set<Id> serviceIds = new set<Id>();
Map<Id, string> serviceMap = new ​Map<Id, string>();
for(Selected_Service__c  ss : Trigger.new)
{
     serviceIds.add(ss.Service__c); //to fetch related Id
}
//Description field needs to be queried else will result in null
for(Service__c ser : [select id, Deliverables__c from Service__c where id in: serviceIds])
{
     serviceMap.put(ser.id, ser.Deliverables__c );
}

for(Selected_Service__c u : trigger.new)
{
    //Selected_Service__c.Custom_Description__c=Service__c.Deliverables__c; This is wrong
     u.Custom_Description__c = serviceMap.get(u.Service__c);
}

All Answers

riyaviriyavi

If the trigger is meant only for the above mentioned field update, you can use process builder rather than a trigger.
If not,  Assuming Service__c is an object related to Selected_Service__c, and Service__c is the lookup field on Selected_Service__c  object, below should help

trigger updtrichtextfld on Selected_Service__c (before insert) {
Set<Id> serviceIds = new set<Id>();
Map<Id, string> serviceMap = new ​Map<Id, string>();
for(Selected_Service__c  ss : Trigger.new)
{
     serviceIds.add(ss.Service__c); //to fetch related Id
}
//Description field needs to be queried else will result in null
for(Service__c ser : [select id, Deliverables__c from Service__c where id in: serviceIds])
{
     serviceMap.put(ser.id, ser.Deliverables__c );
}

for(Selected_Service__c u : trigger.new)
{
    //Selected_Service__c.Custom_Description__c=Service__c.Deliverables__c; This is wrong
     u.Custom_Description__c = serviceMap.get(u.Service__c);
}

This was selected as the best answer
Kristi GrayKristi Gray
When I try the following, it gives me the error 'variable does not exist: Deliverables__c'

trigger updtrichtextfld on Selected_Service__c (before insert) {

for(Selected_Service__c  selected :trigger.new){
selected .Custom_Description__c=selected .Deliverables__c;
   }
}



If I try the following, it gives me the error 'extra ';', at Service__c.Deliverables__c' and the error ' Illegal assignment from Selected_Service__c to String'

trigger updtrichtextfld on Selected_Service__c (before insert) {

for(Selected_Service__c  selected :trigger.new){
selected .Custom_Description__c=selected Service__c.Deliverables__c;
   }
}
Kristi GrayKristi Gray
riyavi - Thanks for the code! However, when I put this in there, it is still removing the rich text formating. For example, the Service__c.Deliverables__c has bullets within it but when it is inputted into the selected .Custom_Description__c it no longer has the bullets. Any suggestions? 
Kristi GrayKristi Gray
Nevermind! So Sorry! I had never deactivated my workflow that wasn't working properly! Thanks so much!