+ Start a Discussion
WesCooperWesCooper 

Trigger - Create a Case Issue

Hey guys - I am having an issue and I just cannot figure it out. My trigger is made to create a new case and set certain fields of the new Case object. I am able to set all these fields, except for one, the Account field.

 

The Account field is a lookup field and for some reason, I am unable to set it. I have provided my code below. Any help would be greatly appreciated. 

 

 

trigger ProgDetailTriggerCase on BLND_DFDT_Project__c (after insert) {

    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;
    
    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, parentid 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).parentid,
            Subject = 'New Program Detail Created',
            Priority = 'Medium',
            Status = 'New',
            Reason = 'Implementation',
            Description = 'Testing the trigger',
            RecordTypeId = rtID,
            Program_Detail__c = pdt.Id,
            Opportunity__c = pdt.BLND_Opportunity_Link__c
            ); 
            
            newpdCase.setOptions(dmlOpts);
            insert newpdcase;
            
            system.debug(newpdCase.Account);
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
jd123jd123

try this 

 

i think it will work

 

Case newpdCase = new Case(
		            AccountId= acc.id,
		            Subject = 'New Program Detail Created',
		            Priority = 'Medium',
		            Status = 'New',
		            Reason = 'Implementation',
		            Description = 'Testing the trigger',
		            RecordTypeId = rtID,
		            Program_Detail__c = pdt.Id,
		            Opportunity__c = pdt.BLND_Opportunity_Link__c
		            ); 

All Answers

jd123jd123

hi

 

i think you are getting  parentid is null that's y the account id is not inserting in the Case.

try like this

 

 Account = accMaps.get(pdt.BLND_Account_Link__c).id,
WesCooperWesCooper

JD - Thank you for the feed back. I should have mentioned that this line of code, actually throws an error.

 

            Account = accMaps.get(pdt.BLND_Account_Link__c).parentid,

 

 

Throws: 

Description Resource Path Location Type
Save error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type) ProgDetailTriggerCase.trigger /SF/src/triggers line 21 Force.com save problem

 

Initially, before I tried setting it by parent Id, i was just using:

 

Map<Id, Account> accMaps = new Map<Id, Account>([SELECT Id, name FROM Account WHERE Id IN :accID]);

 

 

and

 

        Case newpdCase = new Case(
            Account = accMaps.get(pdt.BLND_Account_Link__c),
            Subject = 'New Program Detail Created',

 

I'm not sure why I'm getting an error. Thanks again for the help.

 

jd123jd123

Hi

 

have you seen the accid is coming with values or not you can check in debuglog or developer console 

 

or you can see in visualforce page with get methods.

 

Please let me know if have any issues 

SFFSFF

Whenever you are using a map, you have to know that there is a value in the map that you can use index by that value you are using. Since there are very few things in this world that we really know, you should try something like this:

 

map<Id, Account> accMaps = new Map<Id, Account>(
   [SELECT Id, parentid FROM Account WHERE Id IN :accID]);
Account a;
for (BLND_DFDT_Project__c pdt : Trigger.new)
{
   if (pdt.BLND_Account_Link__c != null)
   {
      a = accMaps.get(pdt.BLND_Account_Link__c);
      if (a != null)
      {
         Case newpdCase = new Case(
            Account = a.Id,
            Subject = 'New Program Detail Created',
            Priority = 'Medium',
            Status = 'New',
            Reason = 'Implementation',
            Description = 'Testing the trigger',
            RecordTypeId = rtID,
            Program_Detail__c = pdt.Id,
            Opportunity__c = pdt.BLND_Opportunity_Link__c); 
            newpdCase.setOptions(dmlOpts);
            insert newpdcase;
      }
   }
}

I can't tell without running this through the debugger what's missing - but something is, so try setting up debug() lines and step through the code.

 

Good luck!

WesCooperWesCooper

Guys - Thanks for all the assistance.

 

I've updated the code to the following and put some Debug lines in there to see if values are coming across for the Account obj.

 

    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, parentid FROM Account WHERE Id IN :accID]);
    Account acc;
    
    for (BLND_DFDT_Project__c pdt : Trigger.new){
    	if(pdt.BLND_Account_Link__c != null){
    		acc = accMaps.get(pdt.BLND_Account_Link__c);
    		if(acc != null){
	    		system.debug('Acc. ID: ' + acc.Id);
	    		system.debug('Parent ID: ' + acc.parentid);
		        Case newpdCase = new Case(
		            Account = acc,
		            Subject = 'New Program Detail Created',
		            Priority = 'Medium',
		            Status = 'New',
		            Reason = 'Implementation',
		            Description = 'Testing the trigger',
		            RecordTypeId = rtID,
		            Program_Detail__c = pdt.Id,
		            Opportunity__c = pdt.BLND_Opportunity_Link__c
		            ); 
		            
		            newpdCase.setOptions(dmlOpts);
		            insert newpdcase;
    		}
    	}
    }

 

This doesn't toss an error, but it also doesn't set the Account either. 

 

The two debug lines return the following:

08:48:19:368 USER_DEBUG [24]|DEBUG|Acc. ID: 001c0000004g6sxAAA

08:48:19:368 USER_DEBUG [25]|DEBUG|Parent ID: null

 

It seems that there is an ID for the Account Obj, but not for the Account ParentID. Could this be the issue?

jd123jd123

try this code

 

yeah you are right.

 

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, parentid FROM Account WHERE Id IN :accID]);
    Account acc;
    
    for (BLND_DFDT_Project__c pdt : Trigger.new){
    	if(pdt.BLND_Account_Link__c != null){
    		acc = accMaps.get(pdt.BLND_Account_Link__c);
    		if(acc != null){
	    		system.debug('Acc. ID: ' + acc.Id);
	    		system.debug('Parent ID: ' + acc.parentid);
		        Case newpdCase = new Case(
		            Account = acc.id,
		            Subject = 'New Program Detail Created',
		            Priority = 'Medium',
		            Status = 'New',
		            Reason = 'Implementation',
		            Description = 'Testing the trigger',
		            RecordTypeId = rtID,
		            Program_Detail__c = pdt.Id,
		            Opportunity__c = pdt.BLND_Opportunity_Link__c
		            ); 
		            
		            newpdCase.setOptions(dmlOpts);
		            insert newpdcase;
    		}
    	}
    }
WesCooperWesCooper

JD - Thanks for all the assitance. 

 

The update you made I have already tried, but I get the following error:

 

Description Resource Path Location Type
Save error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type) ProgDetailTriggerCase.trigger /SF/src/triggers line 27 Force.com save problem

 

I cannot set the object by ID, I have to set it using another account object or through a query. This actually confuses me a bit since I can set other objects by Id, but not the Account object.

 

Thanks again,

 

Wes Cooper

jd123jd123

try with hardcoding like

 

Account = '001c0000004g6sx'  or any other Account id

Check out is it working or not
WesCooperWesCooper

Jd, the compiler throws an error when trying your suggestion:

 

Account = '001c0000004g6sxAAA',

Description	Resource	Path	Location	Type
Save error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type)	ProgDetailTriggerCase.trigger	/SF/src/triggers	line 29	Force.com save problem

 

I also tried doing it this way:

 

Account accTest = new Account();
accTest.Id = '001c0000004g6sxAAA';

This throws:
Description	Resource	Path	Location	Type
Save error: Field is not writeable: Account.Id	ProgDetailTriggerCase.trigger	/SF/src/triggers	line 25	Force.com save problem

 

After this, I updated the code a bit. I removed parentid from the query and added Name. I also added two debug lines to test the values of the Case object.

 

    Map<Id, Account> accMaps = new Map<Id, Account>([SELECT Id, Name FROM Account WHERE Id IN :accID]);
    Account acc;
    
    
    for (BLND_DFDT_Project__c pdt : Trigger.new){
    	if(pdt.BLND_Account_Link__c != null){
    		acc = accMaps.get(pdt.BLND_Account_Link__c);
    		if(acc != null){
	    		system.debug('Acc. ID: ' + acc.Name);
	    		system.debug('Parent ID: ' + acc.Id);
		        Case newpdCase = new Case(
		            Account = acc,
		            Subject = 'New Program Detail Created',
		            Priority = 'Medium',
		            Status = 'New',
		            Reason = 'Implementation',
		            Description = 'Testing the trigger',
		            RecordTypeId = rtID,
		            Program_Detail__c = pdt.Id,
		            Opportunity__c = pdt.BLND_Opportunity_Link__c
		            ); 
		            
		            newpdCase.setOptions(dmlOpts);
		            insert newpdCase;
		            system.debug(newpdCase.Account.Name);
		            system.debug(newpdCase.Account.Id);
    		}
    	}
    }

 

The Case debug lines return exactly what I need. So they are correctly being set in the case object.

 

10:44:49:209 USER_DEBUG [41]|DEBUG|Test Studios

10:44:49:209 USER_DEBUG [42]|DEBUG|001c0000004d7UkAAI

 

Thanks again for all the assistance, guys. It is greatl appreciated.

 

Wes Cooper

jd123jd123

try this 

 

i think it will work

 

Case newpdCase = new Case(
		            AccountId= acc.id,
		            Subject = 'New Program Detail Created',
		            Priority = 'Medium',
		            Status = 'New',
		            Reason = 'Implementation',
		            Description = 'Testing the trigger',
		            RecordTypeId = rtID,
		            Program_Detail__c = pdt.Id,
		            Opportunity__c = pdt.BLND_Opportunity_Link__c
		            ); 
This was selected as the best answer
WesCooperWesCooper

Wow!!! Jd, yes it worked, and after all that, I just want to give myself a FACEPALM, but I think it would be better suited for me to just laugh a bit and slam my head on the keyboard a bit.

 

Thank you for all the assitance.

 

Wes Cooper