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
mdonnellymdonnelly 

Apex code to change record types

Hi. I'm looking to create some Apex code that changes the record type of an opportunity to 'Multi' when brands/products from more than one Product Family are added to a single opportunity.

 

So the workflow is this:

 

1. User clicks New button to create a new opportunity

2. User chooses a record type for that opty -- Media, Events, etc.

3. User fills in opportunity info and chooses various products/brands to attach to that opty. Each brand/product has a Product Family.

4. Apex code is triggered when brands/products are added to the opty that are from more than one Product Family (not necessarily just two or more products, which could all be from the same Product Family) -- this would trigger the record type for that opty to be a) changed to Multi and b) the record type set to read-only. (optional) If the products/brands for that opty are changed again, the record type would revert to the original record type.

 

I'm not an Apex developer and I'm not sure where to start here. I'm hoping someone can point me in the right direction. Thanks in advance.

 

Ispita_NavatarIspita_Navatar

Yes recordtype can be easily be assigned through code, please refer to the code below:-

 

trigger CaseSetupTrigger on Case (before insert){
    for (Case c: Trigger.new) {
        c.RecordType = [select Id from RecordType where Name = 'Canada' and SobjectType = 'Case'];

       
    }
}

mdonnellymdonnelly

Hi. Thanks for your reply. So this code is what I'd use to change the record type to Multi after some other code to determine multiple Product Families (see example above)?

imzeyimzey

Actully that will not work. You need to do the following:

 

RecordType rt = [select Id from RecordType where Name = 'Canada' and SobjectType = 'Case' limit 1];

c.RecordType.Id = rt.Id;

 

You need to specify the Id. Else it will not work.

anil.ax822anil.ax822

Hi ,

 

I have similar scenorio here,but  iam not able to assign the record types here ,plz help me

 

Here is the code 

 

trigger CaseSetupTrigger on Contact(before update)

{
        Contact con=[select id,name,RecordTypeId,status__c  from contact where id in:Trigger.new];   

if(con.status__c=='Prospect')  {       for(Contact c :Trigger.new)   

    {     

          RecordType rt = [select Id,name from RecordType where Name ='Duplicate1' and SobjectType = 'Contact'];     

  system.debug('record found------------------------'+rt); 

        if(rt.Id!=null) 

        {   

                         con.RecordTypeId=rt.id;   

         System.debug('id...'+c.RecordType.id);            

          
  }
  }   update con;    }   

 

       }

 

 

MicrobeMicrobe

imzey wrote:

Actully that will not work. You need to do the following:

 

RecordType rt = [select Id from RecordType where Name = 'Canada' and SobjectType = 'Case' limit 1];

c.RecordType.Id = rt.Id;

 

You need to specify the Id. Else it will not work.



A quick correction to your code: To assign record type to object c, you assign the record type ID to field "RecordTypeId" not "RecordType.Id". Corrected code:

 

RecordType rt = [select Id from RecordType where Name = 'Canada' and SobjectType = 'Case' limit 1];

c.RecordTypeId = rt.Id;

 

FDImarzFDImarz

I found the code below that automatically closes any open tasks when you close a case.  I want to only run this trigger for some record types.  For example, if(c.isCLosed) and (Case Record Type = Releases or Case Record = Fulfillment) then ...close all the tasks.  I have tried a thousand variations, but I cannot get this code to work.  Can someone help?

 

trigger CaseCloseTrigger on Case (after update) {
Case[] cse = Trigger.new;

// Get case id's for closed cases
Set<ID> cseIds = new Set<ID>();
for (Case c:cse)
if(c.isClosed)

cseIds.add(c.Id);

// Get tasks on cases being closed, update and close if still open
for (Task t : [select isclosed, status from task where whatid in :cseIds for update])
if (!t.isclosed){
t.description = 'Case closed';
t.status = 'Completed';
update t;
}
}

roysmith601.3493067652527424E1roysmith601.3493067652527424E1

I have to add record type to my record. for example, if user click on event and we have 4 record type but we want to this functionlaity to work for only one event record.

 

I enter the if statement but i am getting the following error:

Error: Compile Error: Variable does not exist: t.Subject at line 4 column 5

 

my trigger is as follow:

 

trigger eventafter on Event (after insert,after update) {
List<Opportunity> opps = new List<Opportunity>{};

if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType && t.RecordType.Name = 'Opportunity Event')


if(trigger.isInsert)
{
for (Event t : trigger.new)
{
if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}
}
}
else
{
for(Event t : trigger.new)
{
if(t.Subject != null
&& t.WhatId.getSObjectType() == Opportunity.sObjectType
&& t.Subject != trigger.oldMap.get(t.Id).Subject)
{
opps.add(new Opportunity(Id = t.WhatId, StageName = t.Subject));
}

}
}

if(opps != null && !opps.isEmpty()){
Database.update(opps);
}
}

 

 Please help that why i am getting this error and how can i reslove this.Also,If i have to use this for Bulk record then how can i use that in my current trigger.

i m new to salesforce so please help me

thanks

 

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi..

Basically below line throws error. 

You need to declare a variable 't' for Event. so use variable after declaring.

 

if (t.Subject != null && t.WhatId.getSObjectType() == Opportunity.sObjectType && t.RecordType.Name = 'Opportunity Event')

 

 

So, your new code looks like:

 

for(Event t: trigger.New){

if(t.subject!=null && t.whatid.getSObjectType() == Opportunity.sObjectType && t.RecordTypeName=='Opportunity Event'){

 // Write your own code.........

}

}

........................

 

 

If this is work for you then mark it as a solution.......

 

 

 

 

 

 

 

 

 

sunny522sunny522
Hi,
 Please Go through an example in the link.http://salesforceglobe4u.blogspot.in/2016/06/how-to-get-recordtypeid-in-test-class.html (http://salesforceglobe4u.blogspot.in/2016/06/how-to-get-recordtypeid-in-test-class.html" target="_blank)