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
bdardinbdardin 

retreiving custom object type from what field

I wanted to build a simple trigger on the Event that would make a checkbox true if the What field looked up to one of our custom objects, and false otherwise. But with my attempt at this, it keeps making the checkbox true regardless of what is related to the event and I don't understand why. Here's the code:

 

trigger WorkOrderCheckbox on Event (before insert, before update) {
 for(Event newEvent : trigger.new) {
  if(newEvent.What instanceof SVMXC__Service_Order__c) {
   newEvent.Work_Order__c = true;
  } else {
   newEvent.Work_Order__c = false;
  }
 }
}

What am I doing wrong here?

Best Answer chosen by Admin (Salesforce Developers) 
kirkevonphillykirkevonphilly

Try this:

 

trigger WorkOrderCheckbox on Event (before insert, before update) {
    for(Event newEvent : trigger.new) {
        Schema.SObjectType objType;
        string target = newEvent.whatID;
        Map<String, Schema.SObjectType> GD = Schema.getGlobalDescribe();
        string keyPrefix;          

        for(Schema.SObjectType describe: GD.values() ){
	        keyPrefix = describe.getDescribe().getKeyPrefix();
	        if(keyPrefix != null && target.startsWith(keyPrefix)){
		        objType = describe;
	            break; //no need to keep looking
            }
        }
        
        if(string.valueof(objType) == 'SVMXC__Service_Order__c') {
            newEvent.Work_Order__c = true;
        }
        else {
            newEvent.Work_Order__c = false;
        }
    }
}

 

All Answers

kirkevonphillykirkevonphilly

Try this:

 

trigger WorkOrderCheckbox on Event (before insert, before update) {
    for(Event newEvent : trigger.new) {
        Schema.SObjectType objType;
        string target = newEvent.whatID;
        Map<String, Schema.SObjectType> GD = Schema.getGlobalDescribe();
        string keyPrefix;          

        for(Schema.SObjectType describe: GD.values() ){
	        keyPrefix = describe.getDescribe().getKeyPrefix();
	        if(keyPrefix != null && target.startsWith(keyPrefix)){
		        objType = describe;
	            break; //no need to keep looking
            }
        }
        
        if(string.valueof(objType) == 'SVMXC__Service_Order__c') {
            newEvent.Work_Order__c = true;
        }
        else {
            newEvent.Work_Order__c = false;
        }
    }
}

 

This was selected as the best answer
bdardinbdardin

Thank you so much, this worked! Now to study this to figure out why this worked and the other didn't...

Mouse LiuMouse Liu

In order to avoid govern limit, it should be like this,

 

trigger WorkOrderCheckbox on Event (before insert, before update) {
    String keyPrefix = Account.SObjectType.getDescribe().getKeyPrefix();
    for (Event newEvent : trigger.new) {
        string target = newEvent.whatID;
        if (target.startsWith(keyPrefix)) {
            newEvent.Work_Order__c = true;
        }
        else {
            newEvent.Work_Order__c = false;
        }
    }
}
Mouse LiuMouse Liu

Sorry, Like this,

 

trigger WorkOrderCheckbox on Event (before insert, before update) {
    String keyPrefix = SVMXC__Service_Order__c.SObjectType.getDescribe().getKeyPrefix();
    for (Event newEvent : trigger.new) {
        string target = newEvent.whatID;        
        if (target.startsWith(keyPrefix)) {
            newEvent.Work_Order__c = true;
        }
        else {
            newEvent.Work_Order__c = false;
        }
    }
}