+ Start a Discussion
WesCooperWesCooper 

New to Triggers

Hey guys, I am relatively new to writing triggers in SaleForce and so I am hitting a brick wall that I cannot seem to climb over. I've look on the net for the answer, but cannot seem to find it. 

 

I need to create a case after and insert is done from another object. I have no issue here; it works. Thep roblem I am having is pulling a Lookup value from the object. I'm not sure how to do this and I'm having trouble figuring it out.

 

Here is my trigger:

 

trigger MyTrigger on MyObject (after insert) {
    private String Acc;
    private String Opp;
    private String pd;

    AssignmentRule aRule = new AssignmentRule();
    aRule = [SELECT id FROM AssignmentRule WHERE SobjectType = 'Case' AND Active = true limit 1];
    
    Database.DMLOptions dmlOpts = new Database.DMLOptions();
    dmlOpts.assignmentRuleHeader.assignmentRuleId = aRule.id;
    
    Id rtID = [select Id, name from RecordType where name = 'Implementation' and SObjectType = 'Case' limit 1].Id;
    
    Case newpdCase = new Case(
        Subject = 'New Test Case Created',
        Priority = 'Medium',
        Status = 'New',
        Reason = 'Implementation',
        Description = 'Testing the trigger',
        RecordTypeId = rtID
        );
        
        newpdCase.setOptions(dmlOpts);
        insert newpdcase;
    
}

 

 

Any help would be greatly appreciated.

 

Thank you,

 

Wes

liron169liron169

Hi,

 

the value in the lookup fields is a text field, just this text is ID to other record (and salesforce validate that).

 

if for example, in Object A you have field that is lookup to object B,

then this field must contain ID of existing object from type B.

 

 

 

By the way, it will be good idea to amend your code to support bulk records.

means, work on list instead of single object.

WesCooperWesCooper

Hi, thank you for your assistance. As I am really new to the world of Apex, I am having some trouble with using IDs and Mappings. I've updated my code below, but it is still not working as intended. Not sure what I am doing wrong here.

 

I get no errors, but I also do not get the result I am looking for. The object with the trigger has a Lookup field called "Account Name" which is linked to Account. Under the Case object, there is a field called Account, which is also a lookup value. When the trigger executes, it doesn't seem to copy the value of Account from BLND_DFDT_Project__c to

the Case object.

 

Not sure what I am doing wrong here, but any help would be greatly appreciated. 

 

trigger ProgDetailTriggerCase on BLND_DFDT_Project__c (after insert) {
    private String Acc;
    private String Opp;
    private String pd;

    AssignmentRule aRule = new AssignmentRule();
    aRule = [SELECT id FROM AssignmentRule WHERE SobjectType = 'Case' AND Active = true limit 1];
    
    Database.DMLOptions dmlOpts = new Database.DMLOptions();
    dmlOpts.assignmentRuleHeader.assignmentRuleId = aRule.id;
    
    Id rtID = [select Id, name from RecordType where name = 'Test' and SObjectType = 'Case' limit 1].Id;
    
    Set<Id> accId = new Set<Id>();
    
    for (BLND_DFDT_Project__c pdt : Trigger.new){
            accId.add(pdt.BLND_Account_Link__c);
    }

    Map<Id, Account> accMaps = new Map<Id, Account>([SELECT Id, Name FROM Account WHERE Id IN :accID]);
    
    for (BLND_DFDT_Project__c pdt : Trigger.new){
        Case newpdCase = new Case(
            Account = accMaps.get(pdt.BLND_Account_Link__c),
            Subject = 'New PD Detail',
            Priority = 'Medium',
            Status = 'New',
            Reason = 'Testing',
            Description = 'Testing the trigger',
            RecordTypeId = rtID
            );
            
            newpdCase.setOptions(dmlOpts);
            insert newpdcase;
    }
}

 

WesCooperWesCooper

I can't seem to figure out what the issue is here. When I run a debug on "newpdCase", it shows me that it contains the right ID and Account Name. Not sure why upon the insert It remains blank.

 

16:07:20:393 USER_DEBUG [39]|DEBUG|Account:{Name=Test, Id=001c0000004g6sxAAA}