+ Start a Discussion
trailhead solutions 10trailhead solutions 10 

Need help in rearrange the Order/sequence of lines in trigger

Hello

The below trigger is working fine if not putting or differentiating with   record types. Please help whether this is in right format to get fire?

Thanks in Advance
SF Aspirant


trigger sendDatatoWiseApi on Opportunity (after update) {
 
    Trigger_Setting__mdt    triggr = [Select Trigger_Name__c,Active__c from Trigger_Setting__mdt where Trigger_Name__c ='iWise Trigger' ];

  if(triggr.Active__c == TRUE){
    set<id> oppIds = new set<id>();
    if(shouldIRun.canIRun())
   {
    for(Opportunity op : trigger.new){
        if(op.recordtype.Name=='H'){
        if(op.StageName=='Closed Won'  || 
           (trigger.oldMap.get(op.Id).Response_from_iWise_System__c==null && op.Response_from_iWise_System__c!=null && 
            op.Response_from_iWise_System__c.containsIgnoreCase('Error'))){
            oppIds.add(op.Id);
        }
    
    if(oppIds.size()>0)
        
        calliwisehos.sendBoToIwise(oppIds); 
    }
        if(op.recordtype.Name=='I'){
        if(op.StageName=='Closed Won'  || 
           (trigger.oldMap.get(op.Id).Response_from_iWise_System__c==null && op.Response_from_iWise_System__c!=null && 
            op.Response_from_iWise_System__c.containsIgnoreCase('Error'))){
            oppIds.add(op.Id);
        }
    
    if(oppIds.size()>0)
        callIwiseAPI.sendBoToIwise(oppIds); 
       //calliwisehos.sendBoToIwise(oppIds);
    }  
    }
   }
  }
}
 
Best Answer chosen by trailhead solutions 10
Maharajan CMaharajan C
Hi,

From trigger.new you can't access the Record Type name. Only you can access the record type Id.

So you have to use any one of the below schema map to access the record type name or record type developer name.
1. Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Opportunity.getRecordTypeInfosById();
2. Map<Id,Schema.RecordTypeInfo> rtMap = Opportunity.sobjectType.getDescribe().getRecordTypeInfosById();

 
trigger sendDatatoWiseApi on Opportunity (after update) {
    
    Trigger_Setting__mdt    triggr = [Select Trigger_Name__c,Active__c from Trigger_Setting__mdt where Trigger_Name__c ='iWise Trigger' ];
    Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Opportunity.getRecordTypeInfosById();
	
    if(triggr.Active__c == TRUE){
        set<id> oppIds = new set<id>();
        if(shouldIRun.canIRun())
        {
            for(Opportunity op : trigger.new){
				if(op.RecordTypeId != null){
					if(rtMap.get(op.RecordTypeId).getName() == 'H'){
						if(op.StageName=='Closed Won'  || 
						   (trigger.oldMap.get(op.Id).Response_from_iWise_System__c==null && op.Response_from_iWise_System__c!=null && 
							op.Response_from_iWise_System__c.containsIgnoreCase('Error'))){
								oppIds.add(op.Id);
							}
						
						if(oppIds.size()>0)
							
							calliwisehos.sendBoToIwise(oppIds); 
					}
				}
				if(op.RecordTypeId != null){
					if(rtMap.get(op.RecordTypeId).getName() == 'I'){
						if(op.StageName=='Closed Won'  || 
						   (trigger.oldMap.get(op.Id).Response_from_iWise_System__c==null && op.Response_from_iWise_System__c!=null && 
							op.Response_from_iWise_System__c.containsIgnoreCase('Error'))){
								oppIds.add(op.Id);
							}
						
						if(oppIds.size()>0)
							callIwiseAPI.sendBoToIwise(oppIds); 
						//calliwisehos.sendBoToIwise(oppIds);
					}
				}				
            }
        }
    }
}

If you are checking based on the Record type developer name. then use dev name in below if.
if(rtMap.get(op.RecordTypeId).getDeveloperName() == 'H'){

Thanks,
Maharajan.C

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi, 
If your trigger is working fine and you are getting the desired output. 
Then what's the issue you are facing.
Coming to the order for everyone the order of any logic can be different.

Please mark it as the best answer if you find it useful.
Thank and Regards,
Suraj Tripathi

trailhead solutions 10trailhead solutions 10
Hello Suraj

Trigger not firing when I added the code with the two record types. If not added with if clause of Recordtypes, then seperately calling the method, it is working.

Please help to work this.

Thanks
SF Asp
 
Maharajan CMaharajan C
Hi,

From trigger.new you can't access the Record Type name. Only you can access the record type Id.

So you have to use any one of the below schema map to access the record type name or record type developer name.
1. Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Opportunity.getRecordTypeInfosById();
2. Map<Id,Schema.RecordTypeInfo> rtMap = Opportunity.sobjectType.getDescribe().getRecordTypeInfosById();

 
trigger sendDatatoWiseApi on Opportunity (after update) {
    
    Trigger_Setting__mdt    triggr = [Select Trigger_Name__c,Active__c from Trigger_Setting__mdt where Trigger_Name__c ='iWise Trigger' ];
    Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Opportunity.getRecordTypeInfosById();
	
    if(triggr.Active__c == TRUE){
        set<id> oppIds = new set<id>();
        if(shouldIRun.canIRun())
        {
            for(Opportunity op : trigger.new){
				if(op.RecordTypeId != null){
					if(rtMap.get(op.RecordTypeId).getName() == 'H'){
						if(op.StageName=='Closed Won'  || 
						   (trigger.oldMap.get(op.Id).Response_from_iWise_System__c==null && op.Response_from_iWise_System__c!=null && 
							op.Response_from_iWise_System__c.containsIgnoreCase('Error'))){
								oppIds.add(op.Id);
							}
						
						if(oppIds.size()>0)
							
							calliwisehos.sendBoToIwise(oppIds); 
					}
				}
				if(op.RecordTypeId != null){
					if(rtMap.get(op.RecordTypeId).getName() == 'I'){
						if(op.StageName=='Closed Won'  || 
						   (trigger.oldMap.get(op.Id).Response_from_iWise_System__c==null && op.Response_from_iWise_System__c!=null && 
							op.Response_from_iWise_System__c.containsIgnoreCase('Error'))){
								oppIds.add(op.Id);
							}
						
						if(oppIds.size()>0)
							callIwiseAPI.sendBoToIwise(oppIds); 
						//calliwisehos.sendBoToIwise(oppIds);
					}
				}				
            }
        }
    }
}

If you are checking based on the Record type developer name. then use dev name in below if.
if(rtMap.get(op.RecordTypeId).getDeveloperName() == 'H'){

Thanks,
Maharajan.C
This was selected as the best answer
trailhead solutions 10trailhead solutions 10
Hello Maharajan

Thanks a  ton! It is working now.

Regards
SF Asp