• Devanshu sood
  • NEWBIE
  • 275 Points
  • Member since 2017
  • signity solution


  • Chatter
    Feed
  • 8
    Best Answers
  • 0
    Likes Received
  • 3
    Likes Given
  • 3
    Questions
  • 57
    Replies
Hello All,
        I am new to salesforce and  i need to improve apex coding. can you provide some trigger examples? so that i can learn apex from that exapmles.
Req :Create the field called “Contact Relationship” checkbox on the Contact Object and create the object called “Contact Relationship” which is related list to the Contacts. (Look Up Relationship).
Now logic is when we create contact by checking contact relationship checkbox, then Contact Relationship will be created automatically for that contact.

Trigger :
trigger CreateChild on Contact (after insert) {
        set<id> conid = new set<id>();
        list<Contact_Relationship__c> con1 = new list<Contact_Relationship__c>();
        for (contact c : trigger.new){
        conid.add(c.id);
}
        List<contact> conlist = [select id,name from contact where id in : conid];
        for (contact c : conlist){
        if(c.Contact_Relationship__c == true){
        Contact_Relationship__c cr = new Contact_Relationship__c();
        cr.name = c.lastname;
        cr.contact__c= c.id;
        con1.add(cr);
}
}
        insert con1;
}
If the number of child records are more than 10, and if so avoid from creating one more child record.

Thanks adavance........
  • August 02, 2017
  • Like
  • 0
I am trying to pull the AccountID from Healthcloud, and display it on a visualforce page and receiving visualforce error :  List index out of bounds: 0 
An unexpected error has occurred. Your development organization has been notified.

Here is my class:

public with sharing class PatientAccountController {
    
    public String accountId{get;set;}
    public String accountrelatedlist{get;set;}
    
    public PatientAccountController(){
        String cId = apexpages.currentPage().getParameters().get('recId');
        accountrelatedlist = apexpages.currentPage().getParameters().get('relatedlist');
        List<Contact> accID = [Select id, AccountId From Contact where Id = :cid Limit 10];
        accountId = accId[0].AccountId;
    }
}

Here is my visualforce page:

<apex:page controller="PatientAccountController" > docType="html-5.0" showHeader="false sidebar="false" applyHtmlTag="false" applyBodyTag="false" standardStylesheets="true"
    <html>
        <head>
            <meta charset="utf-8"/>
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
          <style>
            html,
            body{
              height:100%;
              width:100%;
              background:#FFFFFF;
              font-family:ProximaNovaRegular, Arial, sans-serif;
              </style>    
                  
        </head>
        <body>
            <iframe id="Ifrmaccountlistview" src="/apex/DE_All_EMR_Data?id={!accountId}" width="100%" height="640px" scrolling="true" frameborder="false"></iframe>
        </body>
             
    </html>
</apex:page>


I have verified that there are active accounts in the org, Healthcloud is enabled, can someone explain why I am getting the error?
trigger Conrecords on Account (before insert)
 {
 List<String> name1 = new List<String>();
    for(Account a : trigger.new)
 {
    myname.add(a.name);
    } 
List<contact> contact1=[select id,name from contact where name in : name1];
 delete contact1;
}
trigger tocallout on Opportunity (after insert, after update) {
    Set<id> accountids = new Set<id>();
    
  for (Opportunity op : Trigger.new) {
  accountIds.add(op.AccountId);    
  }
  
  Map<Id, Account> accountMap = new Map<Id, Account>([SELECT ia_crm__IntacctID__c FROM Account WHERE Id IN :accountIds]);
    String sName;

    for (Opportunity op : Trigger.new){
    if(op.Account.ia_crm__IntacctID__c != null){
    sName = accountMap.get(op.AccountId).ia_crm__IntacctID__c;
}
    if (op.stagename == 'Contract Signed' && sName  != null && (op.CC__c != null || op.Routing_Number__c != null)) {
           if (OpportunityTriggerHandlerclass.ShowRun == true) {
            OpportunityTriggerHandlerclasshandler = new  OpportunityTriggerHandlerclass(); 
        handler.sendCardToTP(op);
      }
    }
  }
}

pls help for the test class...not able to cover..

sName = accountMap.get(op.AccountId).ia_crm__IntacctID__c;

{ if (OpportunityTriggerHandlerclass.ShowRun == true) { OpportunityTriggerHandlerclasshandler = new OpportunityTriggerHandlerclass(); handler.sendCardToTP(op); }

 
Notes / Attachment on opporutnity only then allow to create Opportunity Line Item
VFpage:
<apex:page controller="c1" sidebar="false" docType="Html-5.0" id="page1"  tabStyle="account">
<apex:form >
<apex:pageBlock >

<apex:inputText value="{!name}" />
<apex:commandButton value="search" action="{!search}"/>
<apex:pageblocktable value="{!acts}" var="a">
<apex:column value="{!a.name}"/>
<apex:column value="{!a.id}"/>
<apex:column value="{!a.type}"/>
</apex:pageblocktable>
</apex:pageBlock>

</apex:form>
</apex:page>




controller class:

public with sharing class c1 {
      public String name { get; set; }

    public list<account> acts { get; set; }

    public PageReference search() {
     acts=[select id,type,name from account where name like 'b%'];
        return null;
    }
}
@RestResource(urlMapping='/Outboundcall')
global class Outboundcall {

   @HttpPost
   global static String doPost() {
    
            string tonumber;
            string Fromnumber;
            string CallerName;
            string callSid;
            string Direction;
            string Callstatus;
            string outboundcallid;
            string res;
            string nondigits;
            string ext;
            
   RestRequest req = RestContext.request;
       system.debug(Callstatus);
   if(Callstatus =='initiated')
   { 
       tonumber=req.params.get('To');
       Fromnumber=req.params.get('From');
       CallerName=req.params.get('CallerName');
       callSid=req.params.get('callSid');
       Direction=req.params.get('Direction');
       Callstatus=req.params.get('Callstatus');
        nondigits = '[^0-9]';   
           User Usr = [SELECT Phone,extension,Id FROM User WHERE Did_Number__c = : tonumber limit 1 ];
           system.debug('==='+Usr);
           if(Usr!=null) 
           {
               ext= '1'+usr.extension.replaceAll(nondigits,'');
           }
           
           
           List<outbound_call__c> outboundcall_insert= new List<outbound_call__c>(); 
           outbound_call__c obc= new outbound_call__c();
           obc.Name=tonumber;
           obc.tonumber__c=tonumber;
           obc.Name=Fromnumber;
            
           obc.callSid__c=callSid;
           obc.Direction__c=Direction;
           obc.Callstatus__c=Callstatus;
           
           outboundcall_insert.add(obc);
           insert outboundcall_insert; 
          outboundcallid=outboundcall_insert[0].id;
          
     }
   else if(Callstatus=='in-progress')
   {
     /* outbound_call__c ld = [SELECT Read_Status__c,Callstatus__c,Extension__c,Id,Fromnumber__c FROM Incoming_Calls__c WHERE Extension__c =: ext AND Read_Status__c=false limit 1];
      ld.Callstatus__c=Callstatus;
      system.debug('==='+ld); 
      //ld.Read_Status__c=true;
      update ld;*/
      
   }
   else if(Callstatus=='completed')
   {
   
   }
    else if(Callstatus=='rejected')
   {
    
   }
   else
   {
   
   } 
       res='<?xml version="1.0" encoding="UTF-8" ?><Response></Response>';    
        return res;
    }
}
I am getting this error while enabling lightning experience
 
Challenge Not yet complete... here's what's wrong: 
Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.
Create an Apex trigger for Account that matches Shipping Address Postal Code with Billing Address Postal Code based on a custom field.
For this challenge, you need to create a trigger that, before insert or update, checks for a checkbox, and if the checkbox field is true, sets the Shipping Postal Code (whose API name is ShippingPostalCode) to be the same as the Billing Postal Code (BillingPostalCode).
The Apex trigger must be called 'AccountAddressTrigger'.
The Account object will need a new custom checkbox that should have the Field Label 'Match Billing Address' and Field Name of 'Match_Billing_Address'. The resulting API Name should be 'Match_Billing_Address__c'.
With 'AccountAddressTrigger' active, if an Account has a Billing Postal Code and 'Match_Billing_Address__c' is true, the record should have the Shipping Postal Code set to match on insert or update.
Check challenge
Here is My code :---

trigger AccountAddressTrigger on Account (before insert, before update) {
    
    for(Schema.Account a :trigger.new){
        if(a.match_billing_address__C == true && a.billingpostalcode !=Null){
               a.BillingPostalCode = a.ShippingPostalCode ;
              
        }
    }
}
 
public class MyClass {
     public MyClass() {
        if(a=='New') {
         //Do Something}
        else if(a=='Old'){
         //Do Something
 }
}
Hi All,

When i am deploying a class and test class i am getiing this error

line 1:0 no viable alternative at character ''

I have removed all sapecial characters and i added.... after also i am getting same error.

Thanks
Kumar
  • August 10, 2017
  • Like
  • 0
Hello All,
        I am new to salesforce and  i need to improve apex coding. can you provide some trigger examples? so that i can learn apex from that exapmles.
Req :Create the field called “Contact Relationship” checkbox on the Contact Object and create the object called “Contact Relationship” which is related list to the Contacts. (Look Up Relationship).
Now logic is when we create contact by checking contact relationship checkbox, then Contact Relationship will be created automatically for that contact.

Trigger :
trigger CreateChild on Contact (after insert) {
        set<id> conid = new set<id>();
        list<Contact_Relationship__c> con1 = new list<Contact_Relationship__c>();
        for (contact c : trigger.new){
        conid.add(c.id);
}
        List<contact> conlist = [select id,name from contact where id in : conid];
        for (contact c : conlist){
        if(c.Contact_Relationship__c == true){
        Contact_Relationship__c cr = new Contact_Relationship__c();
        cr.name = c.lastname;
        cr.contact__c= c.id;
        con1.add(cr);
}
}
        insert con1;
}
Hello,

how can i test following snippet?
 
if(ApexPages.currentPage().getParameters().get('aID') != null)
{
tempID = ApexPages.currentPage().getParameters().get('aID');


}else{
tempID = ApexPages.currentPage().getParameters().values()[1];
}
It works fine with an AccounID,....but how can i test it with an ContactID or else?

Those line belong to custom controller extension für a file upload. This Upload can be accessed from any object in our saleforce environment. But mainly from Accounts.

thanks
Peter
 
I am using following code of Attachment object in vf page
<td><apex:outputLink value="/{!N.OwnerId}" target="_blank">{!N.OwnerId}</apex:outputLink></td>

when displaying {!N.OwnerId} on actual page id is displayed.How can i display name instead

I can use apex:outputField but i am not able to give target="_blank" so the page is being refreshed in vf page section without being displayed in new window.Any suggestion for solving the problem?
If the number of child records are more than 10, and if so avoid from creating one more child record.

Thanks adavance........
  • August 02, 2017
  • Like
  • 0
I am trying to pull the AccountID from Healthcloud, and display it on a visualforce page and receiving visualforce error :  List index out of bounds: 0 
An unexpected error has occurred. Your development organization has been notified.

Here is my class:

public with sharing class PatientAccountController {
    
    public String accountId{get;set;}
    public String accountrelatedlist{get;set;}
    
    public PatientAccountController(){
        String cId = apexpages.currentPage().getParameters().get('recId');
        accountrelatedlist = apexpages.currentPage().getParameters().get('relatedlist');
        List<Contact> accID = [Select id, AccountId From Contact where Id = :cid Limit 10];
        accountId = accId[0].AccountId;
    }
}

Here is my visualforce page:

<apex:page controller="PatientAccountController" > docType="html-5.0" showHeader="false sidebar="false" applyHtmlTag="false" applyBodyTag="false" standardStylesheets="true"
    <html>
        <head>
            <meta charset="utf-8"/>
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
          <style>
            html,
            body{
              height:100%;
              width:100%;
              background:#FFFFFF;
              font-family:ProximaNovaRegular, Arial, sans-serif;
              </style>    
                  
        </head>
        <body>
            <iframe id="Ifrmaccountlistview" src="/apex/DE_All_EMR_Data?id={!accountId}" width="100%" height="640px" scrolling="true" frameborder="false"></iframe>
        </body>
             
    </html>
</apex:page>


I have verified that there are active accounts in the org, Healthcloud is enabled, can someone explain why I am getting the error?
Hi,
Can we fire Workflow Rule or Process builder on change in formula field..?
Hi All

I want to display a Field which should be displayed in Detail Page only ..no need to display in Edit page

is there any possible ways to do this?
VFpage:
<apex:page controller="c1" sidebar="false" docType="Html-5.0" id="page1"  tabStyle="account">
<apex:form >
<apex:pageBlock >

<apex:inputText value="{!name}" />
<apex:commandButton value="search" action="{!search}"/>
<apex:pageblocktable value="{!acts}" var="a">
<apex:column value="{!a.name}"/>
<apex:column value="{!a.id}"/>
<apex:column value="{!a.type}"/>
</apex:pageblocktable>
</apex:pageBlock>

</apex:form>
</apex:page>




controller class:

public with sharing class c1 {
      public String name { get; set; }

    public list<account> acts { get; set; }

    public PageReference search() {
     acts=[select id,type,name from account where name like 'b%'];
        return null;
    }
}

I'm writing a trigger for new opportunity products to populate a field Product_Category__c (on opportunity lineitem) getting the value from Product_Category_new__c (on Product).

This is my trigger:

trigger SetProductCategory on OpportunityLineItem (after insert) {

            for (OpportunityLineItem opplineitem: Trigger.new){

                         opplineitem.Product_Category__c= opplineitem.PricebookEntry.Product2.Product_Category_new__c;

              }     
}

I get this error message:

execution of AfterInsert caused by: System.FinalException: Record is read-only

I have tried with before insert;in this case there aren't errors but the value Product category it's not saved on Opportunity Line item.

 

I have found that:

In a after insert trigger is not allowed change field using trigger.new

 

Please can you suggest a solution for this? i would like to copy the value Product category (Product) on Product Category (Opportunity Product) after the insert of the new opportunity products.

Thanks in advantage for any advice.

Br